读取未调用的特征

时间:2018-02-07 09:48:59

标签: android android-ble

我非常陌生,仍在努力。在我的应用程序中,我正在尝试将一个字节数组发送到ble设备,作为响应,将从设备接收一个字节数组。但是不调用onCharacteristicRead()。我知道代码方面缺少一些东西,但我无法弄清楚。请帮忙。先感谢您。下面我发布我的代码:

     @Override
                        public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {


                            MainActivity.this.runOnUiThread(new Runnable() {
                                public void run() {
                                    Log.d("onServicesDiscovered", "Service uuid ");

                                    List<BluetoothGattService> gattServices = gatt.getServices();

                                    Log.d("onServicesDiscovered", "Services count: "+gattServices.size());

                                    if (status == BluetoothGatt.GATT_SUCCESS) {

                                        ArrayList<String> alst_uuid = new ArrayList<String>();
                                        // List<BluetoothGattService> gattServices = gatt.getServices();
                                        // Log.d("onServicesDiscovered", "Services count: "+gattServices.size());

                                        for (BluetoothGattService gattService : gattServices) {
                                            String serviceUUID = gattService.getUuid().toString();
                                            if(serviceUUID.equalsIgnoreCase("56788877-e7cb-469b-6578-2742f1ba77cc"))
                                            {
                                                for(BluetoothGattCharacteristic characteristic: gattService.getCharacteristics())
                                                {
                                                    if(characteristic.getUuid().toString().equals("xxxxxxx-b042-4876-aae1-112855353cc1"))
                                                    {
                                                        Log.d("foundoutchar", characteristic.getUuid().toString());
                                                        String originalString = "BB0100017E";
                                                        byte[] byt_arr = hexStringToByteArray(originalString);
                                                        characteristic.setValue(byt_arr); // call this BEFORE(!) you 'write' any stuff to the server
                                                        gatt.writeCharacteristic(characteristic);
                                                    }
                                                }
                                            }
                                            alst_uuid.add(serviceUUID);
                                            List<BluetoothGattCharacteristic> list = gattService.getCharacteristics();
                                            Log.d("onServicesDiscovered", "Service uuid "+serviceUUID);
                                            serviceObject object = new serviceObject();
                                            String characteristics ="";
                                            String properties ="";
                                            for(int i = 0; i< list.size(); i++)
                                            {

                                                Log.d("onServicesDiscovered", "Service Characteristics "+list.get(i).getUuid());
                                                characteristics = characteristics+""+list.get(i).getUuid()+"\\\n";
                                                properties = properties+""+list.get(i).getProperties()+"\\\n";


                                                Log.d("onServicesDiscovered", "Service Properties "+list.get(i).getProperties());


                                            }




                                    } else {
                                        Log.d("log", "onServicesDiscovered received: " + status);
                                    }
                                }
                            });

                        }

                                @Override
                                public void onCharacteristicWrite(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, int status) {
                                    super.onCharacteristicWrite(gatt, characteristic, status);
                                    MainActivity.this.runOnUiThread(new Runnable() {
                                        public void run()
                                        {
                                            Log.d("onCharacteristicWrite","writing"+characteristic.getUuid().toString());
                                        }
                                    });
                                }

                                @Override
                                public void onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, int status) {
                                    super.onCharacteristicRead(gatt, characteristic, status);
                                    MainActivity.this.runOnUiThread(new Runnable() {
                                        public void run()
                                        {
                                            Log.d("onCharacteristic","writing"+characteristic.getUuid().toString());
                                        }
                                    });

                                }
                            };
                    BluetoothGatt bluetoothGatt =  result.getDevice().connectGatt(MainActivity.this, true, gattCallback);

                }
               }

            // auto scroll for text view
        }
    };

1 个答案:

答案 0 :(得分:1)

onCharacteristicRead被调用以响应readCharacteristic命令,它看起来不像你的代码正在发布。

如果您的外围设备设置为自动回复您的写入字节,那么它就像您在通知或指示后一样。这些可以使用onCharacteristicChanged捕获但您必须在设置期间为每个特征启用通知。

如果您的设备没有设计为使用通知进行回复,那么您应该从onCharacteristicWrite回调中发出readCharacteristic。