How to programmatically pair a bluetooth device on Android

后端 未结 10 1944
轻奢々
轻奢々 2020-11-30 23:27

For my application I\'m trying to programmatically pair a bluetooth device. I\'m able to show the pairing dialog for the device I want to pair and I can enter a pincode. Whe

10条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 23:34

    Reflection is DODGY, different manufacturers can change these underlying methods as they wish! I have tested many different apps on our 10 devices here and these reflection method only works fully on roughly 75% of devices. If you want an app that works for everyone be very careful when using reflection - try some cloud testing to test your app on 100+ devices and check the failure rate.

    In this case reflection is not needed at all since API 19 (KitKat 4.4)

    BluetoothDevice has new method CreateBond.

     private void pairDevice(BluetoothDevice device) {
    
                device.createBond();
        }
    

    developer.android.com/reference/android/bluetooth/BluetoothDevice.html

提交回复
热议问题