简体   繁体   English

在Android应用程序中连续读取ble中的数据

[英]Read data from ble in android app continuously

I want to read data from ble hardware continuously in android app via bluetooth. 我想通过蓝牙在Android应用程序中连续读取ble硬件中的数据。 Connection has done i can send data from app to ble but cant read data from ble. 连接已经完成我可以从应用程序发送数据到ble但不能从ble读取数据。

onCharactersticChanged method has to be called when getting some data from ble but this callback method is not calling. 从ble获取一些数据时必须调用onCharactersticChanged方法,但是这个回调方法没有调用。 and i'm trying to notify to ble but 而我正试着通知他们

writeChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(BLEUtils.CLIENT_CHARACTERISTIC_CONFIG_UUID, BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

gatt.writeDescriptor(descriptor);

  // Enable local notifications
        gatt.setCharacteristicNotification(writeChar, true);

 boolean succes = gatt.writeDescriptor(descriptor);

its returns false 它的返回false

I suggest you should set the ENABLE_NOTIFICATION_VALUE when subscribing to the characteristics. 我建议您在订阅特征时设置ENABLE_NOTIFICATION_VALUE。 This is my code to do this: 这是我的代码:

public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            List<BluetoothGattService> services = getSupportedGattServices();
            for (BluetoothGattService service : services) {
                if (!service.getUuid().equals(UUID_TARGET_SERVICE))
                    continue;

                List<BluetoothGattCharacteristic> gattCharacteristics =
                        service.getCharacteristics();

                // Loops through available Characteristics.
                for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                    if (!gattCharacteristic.getUuid().equals(UUID_TARGET_CHARACTERISTIC))
                        continue;

                    final int charaProp = gattCharacteristic.getProperties();

                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                        setCharacteristicNotification(gattCharacteristic, true);
                    } else {
                        Log.w(TAG, "Characteristic does not support notify");
                    }
                }
            }
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

then you can subscribe to the characteristics with NOTIFICATION: 那么您可以通过NOTIFICATION订阅特征:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR);
    descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                                :BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);

}

after this you can start writing to the device: 在此之后,您可以开始写入设备:

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (descriptor.getCharacteristic().getUuid().equals(UUID_TARGET_CHARACTERISTIC)) {
                Log.i(TAG, "Successfully subscribed");
            }

            byte[] data = <Your data here>;
            BluetoothGattService Service = mBluetoothGatt.getService(UUID_TARGET_SERVICE);

            BluetoothGattCharacteristic charac = Service
            .getCharacteristic(UUID_TARGET_CHARACTERISTIC);

            charac.setValue(data);
            mBluetoothGatt.writeCharacteristic(charac);
        } else {
            Log.e(TAG, "Error subscribing");
        }
    }

And you will receive the callback onCharactersticChanged for reading, no need to actually call the read operation. 并且您将收到onCharactersticChanged用于读取的回调,无需实际调用读取操作。

Remember mBluetoohGatt can only handle 1 operation at a time, if you execute another one while the previous one is unfinished, it won't put in queue, but will return false. 记住mBluetoohGatt一次只能处理1个操作,如果你执行另一个操作而前一个未完成,它将不会进入队列,但会返回false。

If it returns false it usually means you already have a pending GATT operation. 如果它返回false,通常意味着您已经有待处理的GATT操作。 You need to structure your code so that you only have one outstanding GATT operation at a time. 您需要构建代码,以便一次只有一个未完成的GATT操作。 You need to wait for the corresponding callback to arrive until you can execute the next operation. 您需要等待相应的回调到达,直到您可以执行下一个操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM