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.