BLE键盘设备与android手机连接或断开造成手机界面刷新的问题(2020-09-17)

                                          BLE键盘设备与android手机连接或断开造成手机界面刷新的问题

       根据目前测试结果,ble自拍器与android手机建立连接或断开,会造成第三方APP出现界面刷新的动作,下面是具体原因分析:

       ble自拍器中有HID服务,相当于一个键盘类型,当Android手机与键盘类型的设备建立连接时,系统检测到设备类型发生变化从而关闭当前的activity,并重新执行onCreate启动activity,导致界面刷新。

       但IOS系统不会有此现象,这是因为两个系统的处理方式不一样。另外,之前传统蓝牙键盘与android设备建立连接也会有同样的现象的,后来android系统做了优化处理,所以现在传统蓝牙的键盘类型设备与android手机建立连接时不会出现刷屏动作了。而ble走的不是同一个协议,这还得安卓系统去进一步优化。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是 Android 实现手机与蓝牙BLE HID设备的配对连接、解绑、断开连接的代码: 1. 配对连接 ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothGatt gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE); // 发现服务 gatt.discoverServices(); // 连接状态回调 private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { // 连接成功,发现服务 gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 连接断开 gatt.close(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { // 发现服务成功 BluetoothGattService service = gatt.getService(SERVICE_UUID); BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID); // 读取特征值 gatt.readCharacteristic(characteristic); } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); if (status == BluetoothGatt.GATT_SUCCESS) { // 读取特征值成功 byte[] value = characteristic.getValue(); // 处理特征值 handleCharacteristicValue(value); } } }; ``` 2. 解绑 ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothGatt gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE); // 断开连接 gatt.disconnect(); // 关闭连接 gatt.close(); ``` 3. 断开连接 ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothGatt gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE); // 断开连接 gatt.disconnect(); ``` 需要注意的是,以上代码中的 `SERVICE_UUID` 和 `CHARACTERISTIC_UUID` 分别为 BLE HID 设备的服务 UUID 和特征值 UUID,具体值需要根据实际设备进行设置。同时,还需要在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值