无需用户输入密码即可以编程方式配对蓝牙设备 [英] Programmatically pair Bluetooth device without the user entering pin
问题描述
我尝试连接的蓝牙设备始终具有相同的密码.这应该可以通过以编程方式设置引脚来配对设备.
The Bluetooth device I am trying to connect has always the same pincode. This should make it possible to pair the device by setting the pin programmatically.
在尝试搜索如何做到这一点后,我最终得到了以下代码:
After trying to search how this could be done, I ended up with the code below:
BluetoothDevice device = getDevice();
//To avoid the popup notification:
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device, true);
byte[] pin = ByteBuffer.allocate(4).putInt(1234).array();
//int pinn = 1234;
//Entering pin programmatically:
Method ms = device.getClass().getMethod("setPin", byte[].class);
//Method ms = device.getClass().getMethod("setPasskey", int.class);
ms.invoke(device, pin);
//Bonding the device:
Method mm = device.getClass().getMethod("createBond", (Class[]) null);
mm.invoke(device, (Object[]) null);
cancelPairingUserInput
给了我一个 NoSuchMethodException
,这很奇怪,因为该方法确实存在于 BluetoothDevice
类中.
cancelPairingUserInput
gives me a NoSuchMethodException
, which is weird because the method does exist in BluetoothDevice
class.
看起来像 Setpin
或 SetPasskey
没有做任何事情.该设备无法配对.只有在手动输入 pin 后才能配对.
Is looks like Setpin
or SetPasskey
doesn't do anything. The device just wont pair. It only pairs after manually entering the pin.
所以唯一有效的代码行是:
So the only line of code that works is:
//Bonding the device:
Method mm = device.getClass().getMethod("createBond", (Class[]) null);
mm.invoke(device, (Object[]) null);
Logcat 输出:
09-27 12:34:46.408: ERROR/App(11671): cancelPairingUserInput [boolean]
java.lang.NoSuchMethodException: cancelPairingUserInput [boolean]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getMethod(Class.java:915)
at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.pair(BluetoothDiscoveryAndPairing.java:97)
at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.access$000(BluetoothDiscoveryAndPairing.java:25)
at test.app.bluetooth.model.BluetoothDiscoveryAndPairing$1.onReceive(BluetoothDiscoveryAndPairing.java:79)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:756)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
那我做错了什么?
推荐答案
您的设备中不存在隐藏的方法 cancelPairingUserInput.不要使用它.
The hidden method cancelPairingUserInput does not exist in your device. Don't use it.
- 您应该为 android.bluetooth.device.action.PAIRING_REQUEST 注册 BroadcastReceiver
- 调用 createBond()
- 等待 BroadcastReceiver 触发
- 在 BroadcastReceiver 中,如果 action 是 android.bluetooth.device.action.PAIRING_REQUEST调用这个方法
public void setBluetoothPairingPin(BluetoothDevice device)
{
byte[] pinBytes = convertPinToBytes("0000");
try {
Log.d(TAG, "Try to set the PIN");
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
Log.d(TAG, "Success to add the PIN.");
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
Log.d(TAG, "Success to setPairingConfirmation.");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
它也适用于装有 Jelly Bean 版本 (4.1.2) Android 的设备.
It also works on a device with Jelly Bean version (4.1.2) of Android.
这篇关于无需用户输入密码即可以编程方式配对蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!