android 4.3 蓝牙 ble 在执行写入操作后不调用 onCharacteristicRead()



我想使用蓝牙外围设备执行读写操作。我编写了示例应用程序,其中在提供特定输入后,它给了我输出,但在 onCharacteristicWrite 之后没有调用内部代码 onCharacteristicRead。请帮帮我。

这是我的代码

    BluetoothGattCharacteristic WriteCharacteristic =null;
    BluetoothGattCharacteristic ReadCharacteristic =null;
    private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        /* State Machine Tracking */
        private int mState = 0;
        private void reset() { mState = 0; }
        private void advance() { 
            mState++;
            }
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            Log.e("onReliableWriteCompleted", "Called");
        };

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            Log.d(TAG, "Connection State Change: "+status+" -> "+connectionState(newState));
            if (status == BluetoothGatt.GATT_SUCCESS && newState==BluetoothProfile.STATE_CONNECTED) {
                /*
                 * Once successfully connected, we must next discover all the services on the
                 * device before we can read and write their characteristics.
                 */
                gatt.discoverServices();
                //String servicename = gatt.getService(BATTERY_DATA_CHAR).toString();
                //System.out.println(servicename);
                mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Discovering Services..."));
            } else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
                /*
                 * If at any point we disconnect, send a message to clear the weather values
                 * out of the UI
                 */
                connectedDevice = null;
                mHandler.sendEmptyMessage(MSG_CLEAR);
            } else if (status != BluetoothGatt.GATT_SUCCESS) {
                /*
                 * If there is a failure at any stage, simply disconnect
                 */
                gatt.disconnect();
                connectedDevice = null;
            }
        }
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.d(TAG, "Services Discovered: "+status);
            mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Enabling Sensors..."));
            /*
             * With services discovered, we are going to reset our state machine and start
             * working through the sensors we need to enable
             */
            //reset();
            String test="";
            List<BluetoothGattService> strname = gatt.getServices();
            String servicename =DINSTANCE_SERVICE.toString();
            for(int count=0;count<strname.size();count++)
                {
                    if(servicename.equalsIgnoreCase(strname.get(count).getUuid().toString()))
                        {
                        ReadCharacteristic = gatt.getService(DINSTANCE_SERVICE).getCharacteristic(DISTAINCE_READ_DATA);
                        WriteCharacteristic = gatt.getService(DINSTANCE_SERVICE).getCharacteristic(DISTANCE_WRITE_DATA);
                            test="123";
                            //characteristic.getStringValue(18);
                            //byte bbb[] = characteristic.getValue();
//                          gatt.readCharacteristic(characteristic);
                            /*String strInput = "012072";
                            byte passvalues[] = strInput.getBytes();
                            WriteCharacteristic.setValue(passvalues);
                            gatt.writeCharacteristic(WriteCharacteristic);*/
                            WriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                            //tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                            WriteCharacteristic.setValue(012072, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
                            gatt.writeCharacteristic(WriteCharacteristic);
                            break;
                        }
                }
            System.out.println(servicename);
        }
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) 
        {
            Log.e("onDescriptorRead","Read method gets called.");
        };
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            //For each read, pass the data up to the UI thread to update the display
            byte bbb[] = characteristic.getValue();
            if (HUMIDITY_DATA_CHAR.equals(characteristic.getUuid())) {
                mHandler.sendMessage(Message.obtain(null, MSG_HUMIDITY, characteristic));
            }
            if (PRESSURE_DATA_CHAR.equals(characteristic.getUuid())) {
                mHandler.sendMessage(Message.obtain(null, MSG_PRESSURE, characteristic));
            }
            if (PRESSURE_CAL_CHAR.equals(characteristic.getUuid())) {
                mHandler.sendMessage(Message.obtain(null, MSG_PRESSURE_CAL, characteristic));
            }
            if(DISTAINCE_READ_DATA.equals(characteristic.getUuid()))
                {
                    mHandler.sendMessage(Message.obtain(null, MSG_BATTERY, characteristic));
                }
            if(BATTERY_READ_CHAR.equals(characteristic.getUuid()))
                {
                    mHandler.sendMessage(Message.obtain(null, MSG_BATTERY, characteristic));
                }
            //After reading the initial value, next we enable notifications
            //setNotifyNextSensor(gatt);
        }
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) 
        {
            //After writing the enable flag, next we read the initial value
//            readNextSensor(gatt);
            try {
                if (status != BluetoothGatt.GATT_SUCCESS)
                    throw new AssertionError("Error on char write");
                super.onCharacteristicWrite(gatt, characteristic, status);
                if (characteristic.getUuid().equals(DISTANCE_WRITE_DATA)) {
                    BluetoothGattService syncService = gatt.getService(DINSTANCE_SERVICE);
                    BluetoothGattCharacteristic tChar = syncService.getCharacteristic(DISTANCE_WRITE_DATA);
                    ReadCharacteristic = syncService.getCharacteristic(DISTAINCE_READ_DATA);
                    if (tChar == null)throw new AssertionError("characteristic null when sync time!");
                    tChar.setValue(012072,BluetoothGattCharacteristic.FORMAT_SINT32, 0);
                    gatt.readCharacteristic(ReadCharacteristic);
//                  gatt.readDescriptor(null);
                }
            } catch (AssertionError e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            /*
             * After notifications are enabled, all updates from the device on characteristic
             * value changes will be posted here.  Similar to read, we hand these up to the
             * UI thread to update the display.
             */
            if (HUMIDITY_DATA_CHAR.equals(characteristic.getUuid())) {
                mHandler.sendMessage(Message.obtain(null, MSG_HUMIDITY, characteristic));
            }
            if (PRESSURE_DATA_CHAR.equals(characteristic.getUuid())) {
                mHandler.sendMessage(Message.obtain(null, MSG_PRESSURE, characteristic));
            }
            if (PRESSURE_CAL_CHAR.equals(characteristic.getUuid())) {
                mHandler.sendMessage(Message.obtain(null, MSG_PRESSURE_CAL, characteristic));
            }
        }
        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            //Once notifications are enabled, we move to the next sensor and start over with enable
            String strInput = "012072";
            byte passvalues[] = strInput.getBytes();
            descriptor.setValue(passvalues);
            advance();
            Log.e("onDescriptorWrite", "called");
            //enableNextSensor(gatt);
        }
        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) 
        {
            Log.e("onReadRemoteRssi", "called");
            Log.d(TAG, "Remote RSSI: "+rssi);
        }
        private String connectionState(int status) {
            switch (status) {
                case BluetoothProfile.STATE_CONNECTED:
                    return "Connected";
                case BluetoothProfile.STATE_DISCONNECTED:
                    return "Disconnected";
                case BluetoothProfile.STATE_CONNECTING:
                    return "Connecting";
                case BluetoothProfile.STATE_DISCONNECTING:
                    return "Disconnecting";
                default:
                    return String.valueOf(status);
            }
        }
    };
在这里,

您可以通过通知或指示来设置(订阅)字符,

方法是调用:
mBluetoothGatt.setCharacteristicNotification(characteristic, true)
mBluetoothGatt.readCharacteristic(characteristic);

从这里

最新更新