Search code examples
android-ble

Android BluetoothDevice: obtain paired device type when Bluetooth Low Energy


Here is the problem. The user has run my app previously and discovered and paired with a Bluetooth Low Energy health device. That's all the app works with is health devices. I keep track of that device so the app is able to reconnect automatically to that device as long as the app is running.

If the user exits the app or power cycles the Android and restarts the app, I would like to set up automatic reconnection to those paired devices. In classic using HDP, this is done automatically by Android. Not so in BTLE! So what I have to do is go through the list of paired devices and add them to my list of known devices.

But here is the problem. There is no way in BTLE to know what the device is. When I examine BluetoothDevice.getBluetoothClass().getDeviceClass() or .getMajorDeviceClass() they all give the same result: 0x1F00 which means 'UNCATEGORIZED'. So if someone has paired with a BTLE headphone, I will include that in my list.

Is there a way to get the BTLE device class or category without having to save a bunch of stuff to a file myself? Saving BT addresses won't work since BTLE devices can use random addressing (though most don't).


Solution

  • I hate to say this but I have come to the conclusion that there is no way...so I have to do it myself. When the user of my app accepts the pairing of a device I keep its friendly name and save it to a shared preference. I thought about keeping the address but BTLE addresses can be resolvable private addresses so I ditched that thought. I was too lazy to serialize the BluetoothDevice object and save it but that would probably be the most robust.

    On a reconnect, simply check each paired device (saved by the Android OS) against my saved list, and if they match, start a pending connection to the device. It works but seems depressingly low-tech. At least I don't start a pending connect a pair of headphones.

    If I am using a scan-approach instead of a pending-connect approach to solve the reconnect problem, I simply add the paired device to the list of known devices.

    If someone has a better solution, please post!!