小编use*_*451的帖子

无需用户输入引脚即可以编程方式配对蓝牙设备

我尝试连接的蓝牙设备始终具有相同的密码.这样可以通过以编程方式设置引脚来配对器件.

在尝试搜索如何完成后,我最终得到了以下代码:

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);
Run Code Online (Sandbox Code Playgroud)

cancelPairingUserInput给了我一个NoSuchMethodException,这很奇怪,因为该方法确实存在于BluetoothDevice课堂上.

是看起来Setpin还是SetPasskey不会做任何事情.该设备不会配对.它只在手动输入引脚后成对.

所以唯一有效的代码行是:

//Bonding the device:
Method mm = device.getClass().getMethod("createBond", (Class[]) null);
mm.invoke(device, (Object[]) null);
Run Code Online (Sandbox Code Playgroud)

Logcat输出:

09-27 12:34:46.408: ERROR/App(11671): …
Run Code Online (Sandbox Code Playgroud)

java android bluetooth

20
推荐指数
1
解决办法
3万
查看次数

标签 统计

android ×1

bluetooth ×1

java ×1