2012-03-07 67 views
29

该项目是使用我的android手机与我的arduino设备连接。但我如何取消配对。我发现似乎配对列表存储在bluetoothadapter可以随时检索的地方。如何在android上以编程方式取消配对或删除配对的蓝牙设备?

PS: 1,我知道长按配对设备会解除配对。
但这里的问题是如何以编程方式使这发生?

2,我查了bluetoothdevice和bluetoothAdapter类,没有实现这个功能。

谢谢。

+0

试试这个:[http://stackoverflow.com/questions/3462968/how-to-unpair-bluetooth-device-using-android-2-1 -sdk] [1] [1]:http://stackoverflow.com/questions/3462968/how-to-unpair-bluetooth-device-using-android-2-1-sdk – broody 2012-03-07 20:49:57

+0

@broody ,有关于此的其他解决方案? – antonio081014 2012-03-07 22:53:44

回答

52

此代码适用于我。

private void pairDevice(BluetoothDevice device) { 
    try { 
     if (D) 
      Log.d(TAG, "Start Pairing..."); 

     waitingForBonding = true; 

     Method m = device.getClass() 
      .getMethod("createBond", (Class[]) null); 
     m.invoke(device, (Object[]) null); 

     if (D) 
      Log.d(TAG, "Pairing finished."); 
    } catch (Exception e) { 
     Log.e(TAG, e.getMessage()); 
    } 
} 

private void unpairDevice(BluetoothDevice device) { 
    try { 
     Method m = device.getClass() 
      .getMethod("removeBond", (Class[]) null); 
     m.invoke(device, (Object[]) null); 
    } catch (Exception e) { 
     Log.e(TAG, e.getMessage()); 
    } 
} 
+0

它对JB有用吗? o.O – Ewoks 2013-06-11 13:06:44

+0

@Ewoks,不知道你的意思 – 2013-06-14 18:24:46

+0

这是否适用于Jelly Bean(aka 4.1.x)?因为我很确定他们添加了新的蓝牙堆栈,并且createBond方法不见了.. – Ewoks 2013-06-15 12:30:25

1

在BluetoothService类中有方法removebond()来取消配对,配对设备。最后这个方法调用rmovebondnative()。

+0

我会试试看。谢谢。 – 2012-04-16 21:22:58

+0

它是一个公开曝光的API吗? – 2017-05-01 17:20:37

-4

如果您想要首先删除配对蓝牙设备,您必须先取消配对所有设备,然后单击serch选项,您会发现所有设备已从列表中删除。

8

取消配对的所有设备:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     if (pairedDevices.size() > 0) { 
      for (BluetoothDevice device : pairedDevices) { 
       try { 
        Method m = device.getClass() 
          .getMethod("removeBond", (Class[]) null); 
        m.invoke(device, (Object[]) null); 
       } catch (Exception e) { 
        Log.e("Removing has been failed.", e.getMessage()); 
       } 
      } 
     } 
+0

不是** removeBond()**应该是BluetoothDevice上的公共方法? http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/bluetooth/BluetoothDevice.java – 2017-05-01 17:20:03

+1

不,它用'@ hide'注释标记,这使得它不公开。 – Christopher 2017-11-08 13:42:08

相关问题