Android:如何以编程方式配对蓝牙设备?

shr*_*ari 4 android android-source

请任何人帮助我将我的Android手机与其他已发现的手机以编程方式配对?

shr*_*ari 6

使用反射找到解决方案,我现在正在执行此操作,如下所示,它对我有用:

//For Pairing
private void pairDevice(BluetoothDevice device) {
    try {
        Log.d("pairDevice()", "Start Pairing...");
        Method m = device.getClass().getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
}


//For UnPairing
   private void unpairDevice(BluetoothDevice device) {
    try {
        Log.d("unpairDevice()", "Start Un-Pairing...");
        Method m = device.getClass().getMethod("removeBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("unpairDevice()", "Un-Pairing finished.");
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你必须找出来,就像我很久以前做过这段代码一样. (2认同)