首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >bluetoothGatt.writeCharacteristic始终返回false,且ble特征类型为write_no_reponse

bluetoothGatt.writeCharacteristic始终返回false,且ble特征类型为write_no_reponse
EN

Stack Overflow用户
提问于 2019-08-07 10:02:57
回答 2查看 805关注 0票数 0

我正在写一个Android应用程序来与BLE仪表交谈。我已经能够扫描设备,连接到目标,发现服务,获取特征。但是,当我尝试编写write_no_reponse特征时,该方法总是返回false。我相信它的特性是可写的,我用别人的app来写。当我调试成android.bluetooth代码时,下面的序列出现了故障,可能它返回了一个空服务,debug运行到这个步骤,然后单步退出方法:

代码语言:javascript
运行
复制
BluetoothGattService service = characteristic.getService(); 

这会导致writeCharacteristic返回false。但是在writeCharacteristic之前,我有一个日志

代码语言:javascript
运行
复制
Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
bluetoothGatt.writeCharacteristic(mCharacteristic);

且日志正确且不返回null;

任何帮助都是非常感谢的!

并且我用同样的代码写出了一个写特性,很管用。我试着使用

代码语言:javascript
运行
复制
mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

来设置写入类型,但也失败了。

写入数据代码:

代码语言:javascript
运行
复制
public void writeReadDataCommand() {
    if (null != bluetoothGatt) {
        mCharacteristic.setValue("123123");
        bluetoothGatt.setCharacteristicNotification(mCharacteristic, true);
        mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
        boolean isWrite = bluetoothGatt.writeCharacteristic(mCharacteristic);
        if (isWrite) {
            Log.d(TAG, "writeReadDataCommand: write data to BLE");
        }
    }
}

onServiceDiscovered代码:

代码语言:javascript
运行
复制
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);

    if (status == BluetoothGatt.GATT_SUCCESS) {
        bluetoothGatt = gatt;
        BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_UUID));
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));
        Log.d(TAG, "onServicesDiscovered: writeType" + characteristic.getWriteType());

        boolean isSuccess = gatt.setCharacteristicNotification(characteristic, true);
        if (isSuccess) {
            mCharacteristic = characteristic;
            List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
            if (null != descriptors && descriptors.size() > 0) {
                for (BluetoothGattDescriptor descriptor : descriptors) {
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    gatt.writeDescriptor(descriptor);
                }
            }
        }
        connectCallback.findService();
    } else {
        Log.d(TAG, "onServicesDiscovered received: " + status);
    }
}

并且onCharacteristicWrite回调不会触发

代码语言:javascript
运行
复制
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    Log.d(TAG, "onCharacteristicWrite: " + Arrays.toString(characteristic.getValue()));
}

我已经有这个bug很长时间了,非常感谢如果有人能帮助我。

EN

回答 2

Stack Overflow用户

发布于 2019-08-07 20:19:28

characteristics

如图所示,我的服务中有两个特征-- 0和1。这两个特征具有相同的uuid。我做的第一件事是

代码语言:javascript
运行
复制
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));

这段代码找到了0的特征,然后我把数据写到it.Obviously,我失败了。因为0特性只能读取和通知。因此,我执行以下步骤将日期写入BLE

代码语言:javascript
运行
复制
                characteristics = service.getCharacteristics();
                for(BluetoothGattCharacteristic characteristic : characteristics){
                    if(characteristic.getUuid().equals(UUID.fromString(CHARACTERISTIC_UUID))){
                        enableNotification(characteristic);
                    }
                }
代码语言:javascript
运行
复制
            for(BluetoothGattCharacteristic characteristic : characteristics){
                if(characteristic.getUuid().equals(UUID.fromString(CHARACTERISTIC_UUID))){
                    characteristic.setValue("123123");
                    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

                    boolean isWrite = bluetoothGatt.writeCharacteristic(characteristic);
                    if (isWrite) {
                        Log.d(TAG, "writeReadDataCommand: write data to ble");
                    }
                }
            }

我尝试通知这两个特征并将数据写入这两个特征,尽管它们具有相同的UUID,但它们具有不同的功能。显然,一个特征将通知失败,一个特征将写入失败。但最终我成功地写出了我的数据。

票数 0
EN

Stack Overflow用户

发布于 2021-01-26 21:49:42

在onDescriptorWrite()发送数据到BLE设备后执行

代码语言:javascript
运行
复制
 private final BluetoothGattCallback getGattCallback1=new BluetoothGattCallback() {
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
        }

        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
        }

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorRead(gatt, descriptor, status);
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            // here send broadcast to MainActivity.That u are ready to write Data To the device.
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
        }

        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
        }
    };

 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void setCharacteristicNotifications(BluetoothGatt bluetoothGatt,BluetoothGattCharacteristic characteristic,
                                               boolean enabled,String bleAddress) {
        if (mBluetoothAdapter == null || bluetoothGatt == null) {
            return;
        }
        bluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        BluetoothGattDescriptor descriptor = null;
        descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
        if (descriptor == null) {
            enableNotiticationToFirmwareCompleted(false,bleAddress);
            return;
        }
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        bluetoothGatt.writeDescriptor(descriptor);

     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57386072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档