Android的:如何以编程方式配对蓝牙设备?
问题描述:
请任何一个能帮助我与我配对的android手机与其他手机发现的编程
Please any one help me with pairing my android phone with other discovered phone programmatically ?
答
发现使用反射的解决方案,我现在做了这个原则,它是为我工作:
Found the solution using reflection, I am doing this now as follows and it is working for me:
//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());
}
}