Android - BLE 编程绑定不适用于所有 CoolPad Note 3
Android - BLE bonding programmatically doesn't work on all CoolPad Note 3
我正在使用下面的广播接收器来捕获绑定请求并在没有用户弹出请求的情况下绑定它。
private static BroadcastReceiver pairingBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
Toast.makeText(context, "broadcast type "+type, Toast.LENGTH_LONG).show();
if (type == BluetoothDevice.PAIRING_VARIANT_PIN) {
if(devicePin != null) {
CommonStuff.Log("bond pin "+devicePin);
Toast.makeText(context, "broadcast bond pin "+devicePin, Toast.LENGTH_LONG).show();
byte[] pin = new byte[10];
try {
pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class)
.invoke(BluetoothDevice.class, devicePin);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
/*for (int i=0; i< pin.length; i++) {
CommonStuff.Log("byte bond pin "+pin[i]);
}*/
device.setPin(pin);
/*if(type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION || type == 1) {
device.setPairingConfirmation(true);
}*/
abortBroadcast();
}
}
} else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
if(state == BluetoothDevice.BOND_BONDING)
return;
if(state == BluetoothDevice.BOND_BONDED) {
Toast.makeText(context, "Bond success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Bond failed", Toast.LENGTH_LONG).show();
}
}
}
};
假设 devicePin 是全局初始化的。这适用于大多数手机。但是在 coolpad note 3 中,我得到的 BluetoothDevice.EXTRA_PAIRING_VARIANT 值为 1。当我尝试使用 setPin() 绑定此设备而不考虑 EXTRA_PAIRING_VARIANT 值时,绑定失败。
有人遇到过同样的问题吗?请帮我解决这个问题。
BluetoothDevice.EXTRA_PAIRING_VARIANT 因为 1 表示只有用户可以输入密码。但是我们可以通过编程方式使用名为 setpasskey.
的私有方法设置密钥
添加下面的代码作为上面代码中类型的 else if case
else if(type == 1) {
int pin = Integer.parseInt(devicePin);
try {
device.getClass().getMethod("setPasskey", int.class)
.invoke(device, pin);
abortBroadcast();
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
}
我正在使用下面的广播接收器来捕获绑定请求并在没有用户弹出请求的情况下绑定它。
private static BroadcastReceiver pairingBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
Toast.makeText(context, "broadcast type "+type, Toast.LENGTH_LONG).show();
if (type == BluetoothDevice.PAIRING_VARIANT_PIN) {
if(devicePin != null) {
CommonStuff.Log("bond pin "+devicePin);
Toast.makeText(context, "broadcast bond pin "+devicePin, Toast.LENGTH_LONG).show();
byte[] pin = new byte[10];
try {
pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class)
.invoke(BluetoothDevice.class, devicePin);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
/*for (int i=0; i< pin.length; i++) {
CommonStuff.Log("byte bond pin "+pin[i]);
}*/
device.setPin(pin);
/*if(type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION || type == 1) {
device.setPairingConfirmation(true);
}*/
abortBroadcast();
}
}
} else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
if(state == BluetoothDevice.BOND_BONDING)
return;
if(state == BluetoothDevice.BOND_BONDED) {
Toast.makeText(context, "Bond success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Bond failed", Toast.LENGTH_LONG).show();
}
}
}
};
假设 devicePin 是全局初始化的。这适用于大多数手机。但是在 coolpad note 3 中,我得到的 BluetoothDevice.EXTRA_PAIRING_VARIANT 值为 1。当我尝试使用 setPin() 绑定此设备而不考虑 EXTRA_PAIRING_VARIANT 值时,绑定失败。
有人遇到过同样的问题吗?请帮我解决这个问题。
BluetoothDevice.EXTRA_PAIRING_VARIANT 因为 1 表示只有用户可以输入密码。但是我们可以通过编程方式使用名为 setpasskey.
的私有方法设置密钥添加下面的代码作为上面代码中类型的 else if case
else if(type == 1) {
int pin = Integer.parseInt(devicePin);
try {
device.getClass().getMethod("setPasskey", int.class)
.invoke(device, pin);
abortBroadcast();
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
}