ble 蓝牙在Read方法中读取数据每次只能读取一次,怎么做可以循环读取? 10C

ble蓝牙开发中我们onServicesDiscovered()方法中遍历服务后并且设置数据,然后再onCharacteristicRead()方法中读取数据,然而在读取的方法中每次只能自动读取一次,只有当有数据变化时才会再次读取,但是我想一次读取多个数据,不需要其他的操作,代码怎么实现?下面是我在读取方法中写的,每次只能读取一个数据。求大神帮帮忙?

   List<BluetoothGattService> services = gatt.getServices();
                for (BluetoothGattService s : services) {
                    List<BluetoothGattCharacteristic> gattCharacteristicss = s.getCharacteristics();
                    for (int i = 0; i < gattCharacteristicss.size(); i++) {
                        BluetoothGattCharacteristic gattCharacteristic = gattCharacteristicss.get(i);
                        Log.e("000", "gattCharacteristicss:" + i + ":" + gattCharacteristic.getUuid().toString());
                        if (gattCharacteristic.getUuid().toString().equals(SampleGattAttributes
                                .GAMESIR_BLE_CHARACTERISTICS_X1_Manifacturer)) {

                            try {
                                manifacturer = new String(gattCharacteristic.getValue(), "UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            Log.e("000", "Manifacturer:" + manifacturer);

                        }

//
                        if (gattCharacteristic.getUuid().toString().equals(SampleGattAttributes
                                .GAMESIR_BLE_CHARACTERISTICS_X1_Software)) {

                            try {
                                software = new String(gattCharacteristic.getValue(), "UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            Log.e("000", "Software:" + software);
                        }

                        else if (gattCharacteristic.getUuid().toString().equals(SampleGattAttributes
                                .dISHardwareREV_UUID)) {

                            try {
                                hardware = new String(gattCharacteristic.getValue(), "UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            Log.e("000", "Hardware:" + hardware);
//
                        }
                        else if (gattCharacteristic.getUuid().toString().equals(SampleGattAttributes
                                .GAMESIR_BLE_CHARACTERISTICS_X1_FIRMWARE)) {

                            try {
                                version = new String(gattCharacteristic.getValue(), "UTF-8");
                                Log.e("000", "firmware_version数据1:" + version);
                                UpdateUiUtil.setFirmWare(version);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
////////
                        }
                        else if (gattCharacteristic.getUuid().toString().equals(SampleGattAttributes
                                .dISSerialNR_UUID)) {

                            try {
                                serial = new String(gattCharacteristic.getValue(), "UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            Log.e("000", "Serial:" + serial);
                        } else if (gattCharacteristic.getUuid().toString().equals(SampleGattAttributes
                                .dISModelNR_UUID)) {

                            try {
                                model = new String(gattCharacteristic.getValue(), "UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            Log.e("000", "Model:" + model);
//
                        } else if (gattCharacteristic.getUuid().toString().equals(SampleGattAttributes
                                .dISSystemID_UUID)) {

                            try {
                                systemID = new String(gattCharacteristic.getValue(), "UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            Log.e("000", "SystemID:" + systemID);
                        }

hanfengwuyu
某-某 onCharacteristicChanged,在这个回调里返回数据,可能是多条。我的做法是,在onCharacteristicChanged里面调用readCharacteristic(characteristic);然后把返回的数据,除了第一条都拼一起(第一条是你发送的数据),然后在onCharacteristicRead这个回调里面,吧拼好的byte[],发到你要用的地方,并且清除数据。
11 个月之前 回复

2个回答

onCharacteristicChanged,在这个回调里返回数据,可能是多条。我的做法是,在onCharacteristicChanged里面调用readCharacteristic(characteristic);然后把返回的数据,除了第一条都拼一起(第一条是你发送的数据),然后在onCharacteristicRead这个回调里面,吧拼好的byte[],发到你要用的地方,并且清除数据。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问