BLE4.0设置了通知无效的问题

在android的蓝牙4.0的开发中,我们经常会用到服务,特征,

正常情况下,我们会在某个服务中通过uuid取出需要的特征,然后对特征进行读写操作,

下面上一份个人用的读写和设置通知的方法,,,也许这样不太友好,但是个人使用习惯不同..仅供参考

 /**
     * 发送指令
     *
     * @param hex         发送的16进制内容
     * @param uuid        需要操作的特征uuid
     * @param type        操作类型(1=读,2=写,3=信号强度,4=通知)
     * @param uuidService 服务的uuid
     *
     * @return
     */
    private boolean sendCmd(byte[] hex, UUID uuid, int type, UUID uuidService) {
        boolean sendOk = false;
        try {
            BluetoothGatt gatt = mBluetoothGatt;
            if (gatt != null) {
                BluetoothGattService mGattService = MyBleDeviceUtils.getService(gatt, uuidService);
                if (mGattService != null) {
                    BluetoothGattCharacteristic mCharacteristic = MyBleDeviceUtils.getServiceWrite
                        (mGattService, uuid);
                    if (mCharacteristic != null) {
                        if (hex != null) {
                            //写入的内容
                            mCharacteristic.setValue(hex);
                        }
                        mCharacteristic.setWriteType(BluetoothGattCharacteristic
                            .WRITE_TYPE_NO_RESPONSE);//设置类型
                        switch (type) {
                            case 1:
                                sendOk = gatt.readCharacteristic(mCharacteristic);
                                break;

                            case 2:
                                sendOk = gatt.writeCharacteristic(mCharacteristic);
                                break;

                            case 3:
                                sendOk = gatt.readRemoteRssi();
                                break;

                            case 4:
                                sendOk = gatt.setCharacteristicNotification(mCharacteristic,
                                    true);
                                List<BluetoothGattDescriptor> mm = mCharacteristic.getDescriptors();
                                if (sendOk) {
                                    for (BluetoothGattDescriptor bluetoothGattDescriptor : mm) {
                                        bluetoothGattDescriptor.setValue(BluetoothGattDescriptor
                                            .ENABLE_NOTIFICATION_VALUE);
                                        gatt.writeDescriptor(bluetoothGattDescriptor);
                                    }
                                }
                                break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            MyLog.e(TAG, "读/写/设置通知,异常:" + e.toString());
            e.printStackTrace();
        }
        return sendOk;
    }

在上面的代码中设置通知有个坑,直接设置

sendOk = gatt.setCharacteristicNotification(mCharacteristic, true);

会不生效,没有通知返回,必须把特征下面的BluetoothGattDescriptor全部写入

BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE

内容才会有效..


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值