GATT之前, createRfcommSocketToServiceRecord, createInsecureRfcommSocketToServiceRecordAndroid的,怎样才能让BLE装置配对设备(保税)
方法可以使配对设备,
但关贸总协定只没有关于配对设备选项, 使用BluetoothDevice.connectGatt(...)
如果已经连接,我想配对设备。
thx。
GATT之前, createRfcommSocketToServiceRecord, createInsecureRfcommSocketToServiceRecordAndroid的,怎样才能让BLE装置配对设备(保税)
方法可以使配对设备,
但关贸总协定只没有关于配对设备选项, 使用BluetoothDevice.connectGatt(...)
如果已经连接,我想配对设备。
thx。
据我所知,以启动BLE配对过程有两种方式:
1)从19 API和高达你可以通过调用mBluetoothDevice.createBond()
开始配对。您无需连接远程BLE设备即可开始配对过程。
2)当试图做盖特操作,让我们例如该方法
mBluetoothGatt.readCharacteristic(characteristic)
如果远程BLE设备需要被结合做任何通信然后回调时
onCharacteristicRead( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
被称为其status
参数值将等于GATT_INSUFFICIENT_AUTHENTICATION
或GATT_INSUFFICIENT_ENCRYPTION
,而不等于GATT_SUCCESS
。如果发生这种情况,配对程序将自动开始。
这里是要找出失败时一旦onCharacteristicRead
回调函数被调用
@Override
public void onCharacteristicRead(
BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status)
{
if(BluetoothGatt.GATT_SUCCESS == status)
{
// characteristic was read successful
}
else if(BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION == status ||
BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION == status)
{
/*
* failed to complete the operation because of encryption issues,
* this means we need to bond with the device
*/
/*
* registering Bluetooth BroadcastReceiver to be notified
* for any bonding messages
*/
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
mActivity.registerReceiver(mReceiver, filter);
}
else
{
// operation failed for some other reason
}
}
其他人提的是,该操作将自动启动配对过程为例: Android Bluetooth Low Energy Pairing
这是怎么了接收器可以实现
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
{
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
switch(state){
case BluetoothDevice.BOND_BONDING:
// Bonding...
break;
case BluetoothDevice.BOND_BONDED:
// Bonded...
mActivity.unregisterReceiver(mReceiver);
break;
case BluetoothDevice.BOND_NONE:
// Not bonded...
break;
}
}
}
};
这个答案对我来说帮了大忙!我在使用Android 6.0+进行配对通知时遇到了问题,用户甚至不会看到这些通知(较旧的Android做了很好的对话,这是我想要的所有情况)。有一件事我做得比你更不同,就是从开始就调用createBond()(即使在连接到设备之前),如果device.getBondState()== BluetoothDevice.BOND_NONE。这是因为在阅读特征时我没有得到“GATT_INSUFFICIENT_AUTHENTICATION”;操作系统会在它发送通知来配对设备时保留它。 – cclogg
如果我的** onCharacteristicRead()**永远不会被调用? –
@IgorGanapolsky如果您100%确定您正在调用此方法mBluetoothGatt.readCharacteristic(特性),那么将调用回调,除非可能存在与远程设备有关的问题,无论它是代码相关还是连接丢失(如果这是你应该得到一个不同的回调,但我不记得这个名字)。如果有人知道不同,请告诉我们。 – Kiki