我无法通过蓝牙将引脚设置为连接到设备。 这是我的代码:
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
callPairDevice(device);
} else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
setPin (device);
}
}...
};
用于创建键的方法
public boolean callPairDevice(BluetoothDevice device) {
try {
btAdapter.cancelDiscovery();
Method createBond = BluetoothDevice.class.getMethod("createBond");
return (Boolean) createBond.invoke(device);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
此方法尝试设置引脚:
public void setPin (BluetoothDevice device) {
try {
Method m = BluetoothDevice.class.getMethod("setPin", byte[].class);
byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "1234");
boolean isSetPin = (Boolean) m.invoke(device, pin);
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
btSocket.connect();
} catch (Exception e) {
Log.e("setPin()", e.getMessage());
}
}
有人可以帮我找到解决方案吗? 非常感谢....