想要在安卓11上设置蓝牙密码,首先确保打开了“蓝牙设备配对”,并记住了设备的蓝牙地址。然后在开发者选项中,找到“蓝牙LE密码”设置,输入想要的密码并保存。当其他设备通过蓝牙连接此手机时,需要输入这个密码才能进行配对。记住这个密码很重要,否则他人可以轻易地连接你的设备。
在 安卓 11 中,开发者可以使用 BluetoothGattServer 来创建一个蓝牙从设备。常常出现在大家视野里的场景是把 安卓 设备作为一个蓝牙外设,通过蓝牙连接后可以进行数据传输,或者下发指令控制某些硬件设备。
但是,建立安全连接显然是蓝牙从设备必须要支持的功能之一。在 BluetoothGattServer 中,设置连接密码可以保证数据传输的安全性。本篇文章具体介绍了在 安卓 11 上如何创建 BluetoothGattServer,并且如何设置连接密码,保证数据传输的安全性。
1. 创建 BluetoothGattServer
首先创建一个 BluetoothGattServer 对象
```
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH,SERVICE);
BluetoothGattServer gattServer = bluetoothManager.openGattServer(getApplicationContext(), new BluetoothGattServerCallback() {...});
```
创建 BluetoothGattServer 需要留意的事项
- SO,REUSEPORT
蓝牙监听的端口号是 6,需要使用 SO,REUSEPORT 参数保证 安卓 底层蓝牙协议栈不会占用这个端口号。
- BluetoothGattServerCallback
BluetoothGattServerCallback 是回调监听类,用来处理连接事件、数据传输事件以及连接状态改变事件等。
2. 设置连接密码
在 安卓 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 ...
}
}
```
本文介绍了在 安卓11 下通过 BluetoothGattServer 和 BluetoothGattCallback 的方法创建一个蓝牙从设备,并且如何通过输入密码进行安全连接的方法。在具体开发中,能够依据实际情况改变最后的连接以及断开连接的逻辑。