在 Android 11 中,开发者可以使用 BluetoothGattServer 来创建一个蓝牙从设备。常见的场景是把 Android 设备作为一个蓝牙外设,通过蓝牙连接后可以进行数据传输,或者下发指令控制某些硬件设备。
但是,建立安全连接显然是蓝牙从设备必须要支持的功能之一。在 BluetoothGattServer 中,设置连接密码可以保证数据传输的安全性。本篇文章具体介绍了在 Android 11 上如何创建 BluetoothGattServer,并且如何设置连接密码,保证数据传输的安全性。
1. 创建 BluetoothGattServer
首先创建一个 BluetoothGattServer 对象:
```
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
BluetoothGattServer gattServer = bluetoothManager.openGattServer(getApplicationContext(), new BluetoothGattServerCallback() {...});
```
创建 BluetoothGattServer 需要注意的事项:
- SO_REUSEPORT
蓝牙监听的端口号是 6,需要使用 SO_REUSEPORT 参数保证 Android 底层蓝牙协议栈不会占用这个端口号。
- BluetoothGattServerCallback
BluetoothGattServerCallback 是回调监听类,用来处理连接事件、数据传输事件以及连接状态改变事件等。
2. 设置连接密码
在 Android 11 中,设置连接密码也很简单,只需要在 BluetoothGattCharacteristic 中设置。在这里设置密码需要的步骤:
- 构建一个 BluetoothGattCharacteristic 对象,设置 UUID 和属性等。
```
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002a03-0000-1000-8000-00805f9b34fb"), BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
```
- 设置密码值
. 在 characteristic 中设置密码值,密码为 "123456"。
```
characteristic.setValue("123456");
```
- 添加 characteristic 到 BluetoothGattServer
在 BluetoothGattServer 中使用 addService 添加 characteristic。
```
BluetoothGattService service = new BluetoothGattService(UUID.fromString("00002a03-0000-1000-8000-00805f9b34fb"), BluetoothGattService.SERVICE_TYPE_PRIMARY);
service.addCharacteristic(characteristic);
gattServer.addService(service);
```
整个设置密码的过程就是这些,当连接到这个 BluetoothGattServer 时,需要使用密码进行安全连接。
3. 安全连接
在连接 BluetoothGattServer 时,需要通过 connectGatt 方法来建立连接。密码验证是在连接建立后进行的,可以通过 BluetoothGattCallback 的 onCharacteristicWrite 和 onConnectionStateChange 回调方法来处理密码验证的逻辑。
- 这里仍需要构建一个 BluetoothGattCallback 对象,处理 connectGatt 回调事件。
```
BluetoothGattCallback gattCallback = new BluetoothGattCallback() {...}
```
- 建立连接
```
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
BluetoothGatt gatt = device.connectGatt(context, false, gattCallback);
```
- 密码验证
在 onCharacteristicWrite 中验证是否输入了正确的密码。
```
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (characteristic.equals(passwordCharacteristic)){
byte[] value = characteristic.getValue();
if (Arrays.equals(value, "123456".getBytes())){
Log.i(TAG, "Passkey correct, start bonding.");
gatt.setCharacteristicNotification(passwordCharacteristic, false);
gattCallback.onStartBonding(gatt, BluetoothDevice.BONDING_VARIANT_PASSKEY_CONFIRMATION, value);
} else {
Log.i(TAG, "Passkey incorrect, disconnect.");
gatt.disconnect();
}
}
}
```
- 断开连接
```
gatt.disconnect();
```
这样就能成功连接设备了。当输入的密码和设备预设密码一致时,会进入绑定状态,可以在 BluetoothGattCallback 的 onBondStateChanged 中处理。
```
@Override
public void onBondStateChanged(BluetoothDevice device, int prevState, int newState) {
super.onBondStateChanged(device, prevState, newState);
if (newState == BluetoothDevice.BOND_BONDED){
Log.i(TAG, "Bonding complete.");
// do something ...
}
}
```
这篇文章介绍了在 Android11 下通过 BluetoothGattServer 和 BluetoothGattCallback 的方式创建一个蓝牙从设备,并且如何通过输入密码进行安全连接的方法。在具体开发中,可以根据实际情况改变最后的连接以及断开连接的逻辑。