一、APP端调用
//配对
try {
Method method = BluetoothDevice.class.getMethod("createBond");
method.invoke(BluetoothDevice device);
} catch (Exception e) {
e.printStackTrace();
}
//解除配对
try {
Method removeBondMethod = BluetoothDevice.class.getMethod("removeBond");
removeBondMethod.invoke(BluetoothDevice device);
} catch (Exception e) {
e.printStackTrace();
}
二、配对源码分析
上面代码可以看出,使用了 Java 的反射机制去调用 BluetoothDevice 里的 createBond() 和 removeBond() 方法,这里以 createBond() 为例看一下源码的调用过程。
1、配对请求
1)BluetoothDevice
源码位置:/frameworks/base/core/java/android/bluetooth/BluetoothDevice.java
public static final int TRANSPORT_AUTO = 0;
public boolean createBond() {
return createBond(TRANSPORT_AUTO);
}
public boolean createBond(int transport) {
return createBondInternal(transport, null, n