Java 通过蓝牙将Android连接到设备:如何在没有任何密钥弹出窗口的情况下设置pin
我无法设置pin以通过蓝牙连接到设备。 这是我的代码:Java 通过蓝牙将Android连接到设备:如何在没有任何密钥弹出窗口的情况下设置pin,java,android,bluetooth,android-button,Java,Android,Bluetooth,Android Button,我无法设置pin以通过蓝牙连接到设备。 这是我的代码: private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothD
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);
}
}...
};
用于创建绑定的方法
此方法尝试设置pin:
有人能帮我找到解决办法吗?
非常感谢……尝试使用:CreateSecurerCommsockettoServiceRecord(MY_UUID);
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());
}
}