0

In my application, I implemented the classic Bluetooth according to the official documentation: https://developer.android.com/guide/topics/connectivity/bluetooth. By default startDiscovery() will scan for all the nearby bluetooth devices. But in many situations user/developer already knows the desired category of devices. In my case, you need to find a specific device that implements the method:

bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(SERVICE_NAME, MY_UUID_INSECURE);

Ideally, you need to implement the search and connect two devices in one click. How can I select the desired device from the list of found devices? This is for the convenience of using the application, so you do not have to choose from a variety of unnecessary devices. It will be a kind of filter like in BLE. But we are talking about Bluetooth Classic. How can I use SERVICE_NAME, MY_UUID_INSECURE, BluetoothClass to accomplish this task?

EDIT:

I found several solutions to this problem. But they're all not perfect. For now, I just exclude from the list of devices those that have a device.getType() == 2 (DEVICE_TYPE_LE).

Option 1

Search for a device that broadcasts the service with the same UUID as mine. To do this, use the fetchUuidsWithSdp() method for the found devices. Example implementation: https://stackoverflow.com/a/37070600

The disadvantage of this method. Time. You should wait until you receive BluetoothAdapter.ACTION_DISCOVERY_FINISHED before you make any calls to fetchUuidsWithSdp(). It will take 12-18 seconds. In addition to this one must wait for each subsequent call to fetchuuidsWithSdp() to complete, and then give a call to this method for another device. It will take about 3 seconds per device. In total, it can take a very long time to find the right device.

Option 2

Change the device name to a private key or a special name that the client can use to identify the device.

bluetoothAdapter.setName(name);

The main thing is not to forget to return the device name to the original.

saveName = bluetoothAdapter.getName();

Example implementation: https://stackoverflow.com/a/40138077/4716092

The disadvantage of this method. Changing the device name is not so easy in reality. If you do not return the old name of the device, the user may be upset.

denri11
  • 63
  • 5
  • What about looking for a specific device Address? – Alpha-Centauri Jul 22 '19 at 19:15
  • The second device (client) does not know the specific address from the list of available devices. In addition, it is impossible to know your address of the local Bluetooth adapter. The method `bluetoothAdapter.getAddress()` is not working properly today. To solve the problem, it would be enough to add some description for the device to be able to select it from the list of available ones. – denri11 Jul 23 '19 at 06:52
  • Well, I do not understand what exactly you talk about, but you can either sort the List by DeviceName, DeviceAddress or BOND_STATE. Everything else would need a already existing connection I think. But I think that the Device BluetoothAddress is unique and you can use this in firstplace. – Alpha-Centauri Jul 23 '19 at 06:57
  • Here is an example of the situation. Two users want to use my app and connect to each other via Bluetooth. The challenge is to help them do this very simply and not use the Bluetooth settings in the phone to pair. After scanning the available devices, each of them saw about 20 different devices with different names and addresses. This is a very large list to select the desired device. In addition, it often happens that the name of the Bluetooth is the same for several devices. – denri11 Jul 23 '19 at 09:12

0 Answers0