BLE蓝牙不能接受到Notify消息

通过阅读官方的nRF Toolbox APP的源码, 按照它的初始化方法对指定的UUID服务蓝牙对象进行初始化,就可以接收消息了:

具体代码如下:

@Override
        public void onServicesDiscovered(final BluetoothGatt bleGatt, int status) {
            Log.d(TAG, "onServicesDiscovered: services size: " + bleGatt.getServices().size());
            List<BluetoothGattService> serviceList = bleGatt.getServices();
            Observable.from(serviceList)
                    .flatMap(new Func1<BluetoothGattService, Observable<BluetoothGattCharacteristic>>() {
                        @Override
                        public Observable<BluetoothGattCharacteristic> call(BluetoothGattService bleGattService) {
                            return Observable.from(bleGattService.getCharacteristics());
                        }
                    })
                    .filter(new Func1<BluetoothGattCharacteristic, Boolean>() {
                        @Override
                        public Boolean call(BluetoothGattCharacteristic bleGattChar) {
                            Log.e(TAG, "cc: " + bleGattChar.getUuid().toString());
                            if(bleGattChar.getUuid().toString().equals(UUID_RECEIVE)){
                               // bleGatt.requestMtu(0);
                               // boolean b = bleGatt.setCharacteristicNotification(bleGattChar, true);
                                enableNotifications(bleGatt, bleGattChar);
                             //   service.SET
                                //Log.e(TAG, "boolean bleGattChar:" + b);
                            }
                            return bleGattChar.getUuid().toString().equals(UUID_SEND);
                        }
                    })
                    .subscribe(new Action1<BluetoothGattCharacteristic>() {
                        @Override
                        public void call(BluetoothGattCharacteristic bleGattChar) {
                          /*  if(bleGattChar.getUuid().toString().equals(UUID_RECEIVE)){
                                bleGatt.setCharacteristicNotification(bleGattChar, true);
                                Log.e(TAG, "CC bleGattChar: " + bleGattChar.getUuid().toString());
                            }*/
                            if(bleGattChar.getUuid().toString().equals(UUID_SEND)){
                                mBleGattChar = bleGattChar;
                            }
                        }
                    });
        }

protected final boolean enableNotifications( BluetoothGatt gatt ,final BluetoothGattCharacteristic characteristic) {
        if (gatt == null || characteristic == null)
            return false;

        // Check characteristic property
        final int properties = characteristic.getProperties();
        if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0)
            return false;

        Log.d("BLE", "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
        gatt.setCharacteristicNotification(characteristic, true);
        final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
        if (descriptor != null) {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            Log.v("BLE", "Enabling notifications for " + characteristic.getUuid());
            Log.d("BLE", "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
            return gatt.writeDescriptor(descriptor);
        }
        return false;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值