zoukankan      html  css  js  c++  java
  • android BLE 40 setCharacteristicNotification接收不到数据

    参考连接:http://www.codes51.com/article/detail_4021799.html

    问题描述 最近在开发android BLE  读写数据

    但是向设备写数据很顺利,但是在接收设备传来的数据时,死活接收不到.

     /**
         * Enables or disables notification on a give characteristic.
         *
         * @param characteristic Characteristic to act on.
         * @param enabled        If true, enable notification.  False otherwise.
         */
        public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                                  boolean enabled) {
            if (mBluetoothAdapter == null || mBluetoothGatt == null) {
                Log.w(TAG, "BluetoothAdapter not initialized");
                return;
            }
            mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        
        }

    上面这样写 是收不到数据的 ,主要是少加了mBluetoothGatt.writeDescriptor(descriptor);

    下面是修改后的代码

       /**
         * Enables or disables notification on a give characteristic.
         *
         * @param characteristic Characteristic to act on.
         * @param enabled        If true, enable notification.  False otherwise.
         */
        public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                                  boolean enabled) {
            if (mBluetoothAdapter == null || mBluetoothGatt == null) {
                Log.w(TAG, "BluetoothAdapter not initialized");
                return;
            }
    //        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
            boolean isEnableNotification =  mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
            if(isEnableNotification) {
                List<BluetoothGattDescriptor> descriptorList = characteristic.getDescriptors();
                if(descriptorList != null && descriptorList.size() > 0) {
                    for(BluetoothGattDescriptor descriptor : descriptorList) {
                        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                        mBluetoothGatt.writeDescriptor(descriptor);
                    }
                }
            }
        }

     如果 不是这种 情况接收不到数据就是另外一种可能了characteristic的 uuid设置不对

    你可以看下 boolean isEnableNotification = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    返回的是false 还是true,

    另外还会再写一篇关于蓝牙数据读写的文章

    以上就介绍了 android BLE 40 setCharacteristicNotification接收不到数据,包括了方面的内容,希望对Android开发有兴趣的朋友有所帮助。

    本文网址链接:http://www.codes51.com/article/detail_4021799.html

  • 相关阅读:
    kendoUI行编辑器的使用grid.editRow($("#grid tr:eq(1)"))无效
    Kendo-UI学习 DataSource 数据源属性说明
    fineReport 下拉联动 js
    报表FineReport中单元格中各种颜色的标识说明
    keil DSP最新版本
    ESP8266固件烧录篇
    git 报错 时出现Clone failed early EOF错误解决
    STM32 HAL库、标准外设库、LL库(STM32 Embedded Software)
    再谈EPLAN 中的项目结构-帮助理解
    启动EPLAN时,应该选哪个版本?Compact/select start/professional/select/maintenance/professional+/ultimate
  • 原文地址:https://www.cnblogs.com/jiuzhexingfu/p/7102353.html
Copyright © 2011-2022 走看看