Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically bonding to BLE device on Android

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?

like image 539
David Avatar asked Aug 06 '15 14:08

David


People also ask

How do Android apps communicate with custom BLE?

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.

How do I bond my Android device?

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.

How do I enable BLE on Android?

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.


1 Answers

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.

like image 131
Timmmm Avatar answered Nov 13 '22 04:11

Timmmm