簡體   English   中英

在Android應用程序中連續讀取ble中的數據

[英]Read data from ble in android app continuously

我想通過藍牙在Android應用程序中連續讀取ble硬件中的數據。 連接已經完成我可以從應用程序發送數據到ble但不能從ble讀取數據。

從ble獲取一些數據時必須調用onCharactersticChanged方法,但是這個回調方法沒有調用。 而我正試着通知他們

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);

它的返回false

我建議您在訂閱特征時設置ENABLE_NOTIFICATION_VALUE。 這是我的代碼:

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);
        }
    }

那么您可以通過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);

}

在此之后,您可以開始寫入設備:

    @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");
        }
    }

並且您將收到onCharactersticChanged用於讀取的回調,無需實際調用讀取操作。

記住mBluetoohGatt一次只能處理1個操作,如果你執行另一個操作而前一個未完成,它將不會進入隊列,但會返回false。

如果它返回false,通常意味着您已經有待處理的GATT操作。 您需要構建代碼,以便一次只有一個未完成的GATT操作。 您需要等待相應的回調到達,直到您可以執行下一個操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM