uuid正确却接收不到蓝牙设备返回的数据

这个问题也是我遇到的,通过第三方工具我排除了uuid错误的问题,发送给蓝牙设备的指令也成功了,但是就是收不到返回的,第三方的工具是能收到的,又排除了设备的问题,确定是自己代码的问题之后,看了一些博客之后也有人遇到这个问题,我在我的demo中也修改了,可以看到:

private void displayGattServices(List<BluetoothGattService> gattServices) {
        Iterator localIterator1 = gattServices.iterator();
        while (localIterator1.hasNext()) {
            BluetoothGattService localBluetoothGattService = (BluetoothGattService) localIterator1
                    .next();
            if (localBluetoothGattService.getUuid().toString()
                    .equalsIgnoreCase(SERVICE_UUID)) {
                List localList = localBluetoothGattService.getCharacteristics();
                Iterator localIterator2 = localList.iterator();
                while (localIterator2.hasNext()) {
                    BluetoothGattCharacteristic localBluetoothGattCharacteristic = (BluetoothGattCharacteristic)
                            localIterator2
                                    .next();
                    if (localBluetoothGattCharacteristic.getUuid().toString()
                            .equalsIgnoreCase(NOTIFICATION_UUID)) {
                        bleGattCharacteristic = localBluetoothGattCharacteristic;
                        mBluetoothGatt.setCharacteristicNotification(bleGattCharacteristic, true);
                    /*//notifiction默认是关闭的  需要设置0x01打开
                        List<BluetoothGattDescriptor> descriptors = localBluetoothGattCharacteristic.getDescriptors();
                        for (int i = 0; i < descriptors.size(); i++) {
                            if (descriptors.get(i).getUuid().toString().equals(DISENABLE)) {
                                BluetoothGattDescriptor bluetoothGattDescriptor = descriptors.get(i);
                                bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.PERMISSION_READ);
                                mBluetoothGatt.writeDescriptor(bluetoothGattDescriptor);
                            }
                        }*/
                        BluetoothGattDescriptor descriptor = localBluetoothGattCharacteristic
                                .getDescriptor(UUID.fromString(NOTIFICATION_UUID));
                        if (descriptor == null) {
                            descriptor = new BluetoothGattDescriptor(
                                    UUID.fromString(NOTIFICATION_UUID),
                                    BluetoothGattDescriptor.PERMISSION_WRITE);
                        }
                        descriptor
                                .setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                        mBluetoothGatt.writeDescriptor(descriptor);
                        break;
                    }
                }
                break;
            }
        }
    }

主要就是我注释掉的这段代码,在查找服务的方法,notifi默认是关闭的,我设置为0x01就打开了,如果0x01你们没用的话,再问下硬件工程师,然后解开上面的注释,把下面的注释掉就可以正常收发指令了
demo:https://github.com/duoshine/BlueToothConnect-master

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值