Android部分手机会无法接收到onCharacteristicChanged()所返回的消息

在Android 6.0且API Level低于24。部分手机会无法接收到onCharacteristicChanged()所返回的消息,使用以下方法能够完美解决问题。

  public boolean enableNotification(BluetoothGatt gatt, UUID serviceUUID, UUID characteristicUUID) {
//        BluetoothGattService service = gatt.getService(serviceUUID);
//        BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
//        if (characteristic != null) {
//        if(bluetoothGatt.setCharacteristicNotification(characteristic2, true)){
//            //获取到Notify当中的Descriptor通道 然后再进行注册
//            BluetoothGattDescriptor clientConfig = characteristic2 .getDescriptor(UUID.fromString(DESCRIPTOR_UUID));
//            clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
//            bluetoothGatt.writeDescriptor(clientConfig);
//        }
        boolean success = false;
        BluetoothGattService service = gatt.getService(serviceUUID);
        if (service != null) {
            BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
            if (characteristic != null) {
                success = gatt.setCharacteristicNotification(characteristic, true);
                gatt.readCharacteristic(characteristic);
                if (success) {
                    for(BluetoothGattDescriptor dp: characteristic.getDescriptors()){
                        if (dp != null) {
                            if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
                                dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                            } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
                                dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                            }
                            int writeType = characteristic.getWriteType();
                            Log.e(TAG, "enableNotification: "+writeType );                            characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                            bluetoothGatt.writeDescriptor(dp);
                            characteristic.setWriteType(writeType);
                        }
                    }
                }
            }
        }
        return success;
    }

    private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service, UUID characteristicUUID) {
        BluetoothGattCharacteristic characteristic = null;
        List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
        for (BluetoothGattCharacteristic c : characteristics) {
            if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0 && characteristicUUID.equals(c.getUuid())) {
                characteristic = c;
                break;
            }
        }
        if (characteristic != null) {
            return characteristic;
        }
        for (BluetoothGattCharacteristic c : characteristics) {
            if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0 && characteristicUUID.equals(c.getUuid())) {
                characteristic = c;
                break;
            }
        }
        return characteristic;
    }


然后只需在onServicesDiscovered()方法中调用这个方法即可


enableNotification(bluetoothGatt,serviceNotifyUUID, notifyUUID);
            serviceNotifyUUID为服务ID,notifyUUID为notify的UUID
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,501评论 2 59
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,800评论 19 139
  • 0.Android手机操作系统的四层架构? Applications , Application Framewor...
    lucas777阅读 12,401评论 0 16
  • “韩老师,您一定要注意班里的情况,有很多学生在讲啪啪啪,我们家姑娘很受影响。”一位家长气愤不平地打电话给我。 这种...
    韩乔羽阅读 3,529评论 0 0
  • 若是有人问,还有比「心怀善意」更难的事吗?我的答复一定是——没有了! 1. 我的说法,你同不同意,我不知道;但我相...
    吴益军子阅读 1,014评论 0 1