簡體   English   中英

無法從BLE設備讀取,寫入正常(Android)

[英]Unable to Read from BLE Device, Write is working (Android)

我正在嘗試從BLE設備讀取值。 我遵循的步驟:

  1. 我能夠發現BLE設備並連接到它。
  2. 我可以通過解析服務找到所需的特征並獲取GattCharacteristic。
  3. 我能夠將值寫入BLE設備特性,並且已經過驗證。
  4. 我嘗試從中讀取值的該特性的屬性為:READ,WRITE和INDICATE / NOTIFY。

我用於讀寫的功能如下:

a)讀取功能:

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

    boolean status;
Log.w(TAG, "SCBABLe: Writing BLuetooth");
status=mBluetoothGatt.readCharacteristic(characteristic);

if(status) {

    Log.w(TAG, "Read Successfully");
}

else
{
    Log.w(TAG, "Read Unsuccessfully");

}

   }

b)寫功能

public void writeCharacteristic(BluetoothGattCharacteristic characteristic)
{
    if (mBluetoothAdapter == null || mBluetoothGatt == null)
    {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    boolean status;
    Log.w(TAG, "SCBABLe: Writing BLuetooth");
    status=mBluetoothGatt.writeCharacteristic(characteristic);

    if(status) {

        Log.w(TAG, "Write Successfully");
    }

    else
    {
        Log.w(TAG, "Write Unuccessfully");

    }
}

在獲得所需的特征后(通過匹配UUID),我從不同的活動中調用上述兩者。

//Current Activity Name: DeviceControlActivity
//BluetoothLeService is the other activity where Read and write are defined


private static final DeviceControlActivity holder1 = new DeviceControlActivity();

public BluetoothGattCharacteristic reqChar;
public BluetoothLeService reqService;
private BluetoothLeService mBluetoothLeService;

private void displayGattServices(List<BluetoothGattService> gattServices) 
{
  if (gattServices == null) return;
    String uuid = null;
  for (BluetoothGattService gattService : gattServices) 
    {
        uuid = gattService.getUuid().toString();
        // Loops through available Characteristics.
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) 
        {

                uuid = gattCharacteristic.getUuid().toString();
        //SampleGattAttributes.UNKNOWN_CHARACTERISTIC is the hardcoded uuid against which i am checking     
       if((uuid.equals((SampleGattAttributes.UNKNOWN_CHARACTERISTIC))))
       {
          holder1.reqChar = gattCharacteristic;
          holder1.reqService = mBluetoothLeService;

          //Call for write
          byte [] byte1= {0x01, 0x10};

          holder1.reqChar.setValue(byte1);
          holder1.reqService.writeCharacteristic(holder1.reqChar);

          //Call for read

          holder1.reqService.readCharacteristic(holder1.reqChar);

結果:讀取函數返回false,寫入函數返回true,因此已成功寫入所需特性的值。(已驗證)

請,任何人都可以幫助並告知為什么讀取未執行的原因嗎? 當它具有Read as屬性並定義了正確的值時,為什么仍返回false?

問題出在這幾行。

holder1.reqService.writeCharacteristic(holder1.reqChar);

//Call for read

holder1.reqService.readCharacteristic(holder1.reqChar);

調用writeCharacteristic之后,是否還要進行其他讀/寫/其他操作。 ,該調用將不會執行並返回false。 您必須等待BluetoothGattCallback.onCharacteristicWrite才能執行進一步的操作。

在Android BLE實現中,需要對gatt操作調用進行排隊,以便一次僅執行一個操作(讀,寫等)。 因此,例如,在gatt.readCharacteristic(characteristicX)之后,您需要等待gatt回調BluetoothGattCallback.onCharacteristicRead()指示讀取已完成。 如果在上一個操作完成之前啟動第二個gatt.readCharacteristic()操作,則第二個操作將失敗(返回false)。這適用於所有gatt.XXX()操作。

它的工作量很小,但是我認為最好的解決方案是為所有gatt操作創建一個命令隊列,並一次運行一次。 您可以使用命令模式來完成此操作。

暫無
暫無

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

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