I'm writing an Android application in which I'd like to programmatically bond to a custom BLE device. I have the manual bonding working in which the user enters the PIN using the standard Android Bluetooth pairing dialog, but I have not been able to find any information on how to automatically bond a BLE device programatically, without user intervention. Is that possible? If so, what's the process?
Create a new project Open Android Studio and you should be greeted with the following screen. Select Import an Android code sample. On the next screen select the sample Bluetooth Le Gatt under Connectivity. This project will set us up with a framework to build off of for our application.
With a device instance in hand, open a BluetoothSocket and connect() . If the devices are not already bonded, this will initiate pairing and may show some system UI for a PIN code. Once paired, your device will show up in the bonded devices list until the user goes into settings and removes it.
Once your app has permission to use Bluetooth, your app needs to access the BluetoothAdapter and determine if Bluetooth is available on the device. If Bluetooth is available, the device will scan for nearby BLE devices.
I managed to do this - see my answer here.
The TL;DR is: forget about ACTION_BOND_STATE_CHANGED
; you don't need it. Instead listen to ACTION_PAIRING_REQUEST
, and set the priority high. In the broadcast receiver when you get ACTION_PAIRING_REQUEST
, call setPin()
with your PIN and then abortBroadcast()
to prevent the system showing the notification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With