2016-07-29 68 views
0

我試圖在BLE模塊之間交換android設備和自定義板之間的數據。我在Android設備版本5.0和更高版本中使用featuresWrite()方法時遇到了一些麻煩。在情人版本characteristicsWrite()方法返回true,然後調用onCharacteristicsWrite在BleGattCallback()回調Androi BLE writeCharacteristics方法總是在Android 5.0及更高版本中返回false

寫方法:

private void sendMessageToDevice(String message) { 

    byte nullByte = 0x00; 
    byte[] temp = message.getBytes(); 
    byte[] tx = new byte[temp.length + 1]; 
    tx[tx.length - 1] = nullByte; 

    System.arraycopy(temp, 0, tx, 0, temp.length); 

    characteristicWrite.setValue(tx); 

    boolean writeResult = mBluetoothGatt.writeCharacteristic(characteristicWrite); 

    Log.d(LOG_TAG, "writeResult"); //true 
} 

onCharacteristicsWrite方法:

@Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicWrite(gatt, characteristic, status); 

     Log.d(LOG_TAG, status); // status = 0 - GATT_SUCCESS 
    } 

但是,當我執行android上的驗證碼設備v 5.0結果writeCharacteristic()方法始終爲false,並且onCharacteristicWrite()回調未調用。

任何人都可以解釋我如何正確建立BLE設備和Android 5.0+之間的連接嗎?也許我需要一些這種類型的設備的具體方法。 在三星和聯想智能手機v5.0 +上測試應用程序。

+0

你確定你正確連接你的'GATT'圖層嗎? – Jeeter

+0

應用程序工作正常,Android設備版本低於5.0,所以我想層連接正常。我正在使用這個tutoriall創建連接:[Android BLE](https://developer.android.com/guide/topics/connectivity/bluetooth-le.html) –

回答

0

問題已解決。你只需要調用discoverServices()你writeCharacteristic功能的「假」的結果後再次:

private void sendMessageToDevice(String message) { 

     ............. 

     boolean writeResult = mBluetoothGatt.writeCharacteristic(characteristicWrite); 

     if(!writeResult) 
      mBluetoothGatt.discoverServices(); 
} 

之後,你需要再次調用writeCharacteristic()函數,以及我的結果是「真」。

+0

奇怪。通常,它返回false的唯一時間是當你已經有一個待定的gatt操作。在Android中,您始終需要等待回調才能執行新的gatt操作。 – Emil