Programmatically bonding to BLE device on Android

2019-06-19 00:59发布

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?

3条回答
成全新的幸福
2楼-- · 2019-06-19 01:33

I was able to make this work MOST OF THE TIME by registering a BroadcastReceiver to receive the BluetoothDevice.ACTION_BOND_STATE_CHANGED intent and then calling BluetoothDevice.setPin after receiving the BluetoothDevice.BOND_BONDING message. As is the case with most BLE things in Android, this seems to act slightly differently depending on the device and Android version. Unfortunately, I can't seem to stop Android from also receiving the bluetooth intent, so the PIN entry screen still pops up for a second before the bonding is completed.

   private final BroadcastReceiver mReceiver = new BroadcastReceiver()
   {
       @Override
       public void onReceive(Context context, Intent intent)
       {
           final String action = intent.getAction();
           Logger("Broadcast Receiver:" + action);

           if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
           {
               final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);

               if(state == BluetoothDevice.BOND_BONDING)
               {
                   Logger("Bonding...");
                    if (mDevice != null) {
                        mDevice.setPin(BONDING_CODE.getBytes());
                        Logger("Setting bonding code = " + BONDING_CODE);
                    }
               }
               else if(state == BluetoothDevice.BOND_BONDED)
               {
                   Logger("Bonded!!!");
                   mOwner.unregisterReceiver(mReceiver);
               }
               else if(state == BluetoothDevice.BOND_NONE)
               {
                   Logger("Not Bonded");
               }
           }
       }
   };
查看更多
Juvenile、少年°
3楼-- · 2019-06-19 01:37

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.

查看更多
Anthone
4楼-- · 2019-06-19 01:40

All you can do to avoid user interaction is to force Just Works pairing. To do that, program the peripheral to accept pairing with NoInputNoOutput IO Capability.

查看更多
登录 后发表回答