2014-01-23 117 views
2

我在爲BLE設備開發android軟件時遇到問題。 我的軟件可以找到我的設備和GATT服務,但在我的服務中找不到任何特徵。Android Ble在BLE設備的GATT服務中找不到特徵

我檢查了android-sdk-4.4.2源碼,發現了一些代碼。 https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-sdk-4.4.2_r1 https://android.googlesource.com/platform/packages/apps/Bluetooth/+/android-sdk-4.4.2_r1

static char BASE_UUID[16] = { 
    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 
    0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
}; 

int uuidType(unsigned char* p_uuid) 
{ 
    int i = 0; 
    int match = 0; 
    int all_zero = 1; 

    for(i = 0; i != 16; ++i) 
    { 
     if (i == 12 || i == 13) 
      continue; 

     if (p_uuid[i] == BASE_UUID[i]) 
      ++match; 

     if (p_uuid[i] != 0) 
      all_zero = 0; 
    } 
    if (all_zero) 
     return 0; 
    if (match == 12) 
     return LEN_UUID_32; 
    if (match == 14) 
     return LEN_UUID_16; 
    return LEN_UUID_128; 
} 

我的BLE裝置UUID是0000XXXX-AABB-1000-8000-00805F9B34FB。 這段代碼是否會造成這種麻煩? 或者我的BLE設備UUID有問題嗎?

回答

1

瞭解GATT的實施。雖然我不是來自Android背景,並且我真的無法通過您發佈的代碼看到您正在做什麼,但我會建議您嘗試幾件事情以實現其功能。首先,就像您的設備具有唯一的MAC ID一樣,每個服務都有其UUID,並且服務中包含的特徵也具有其自己的UUID。

  • 連接後,請閱讀所需的GATT服務。
  • 一旦您收到GATT描述符,您應該能夠看到服務中包含的特徵,並且您應該能夠通過相應的ID或句柄來訪問它們。由於我沒有android開發經驗,所以我不能告訴你什麼句柄,什麼描述符,什麼ID,什麼數據結構會爲你做,但必須有一些。
0

這就是你要找的。它是gatt.discoverServices();的回調函數,並返回每個服務的UUID,併爲每個服務返回特徵UUID。

@Override 
    // New services discovered 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      for (BluetoothGattService gattService : gatt.getServices()) { 
       for (BluetoothGattCharacteristic mCharacteristic : gattService.getCharacteristics()) { 
        Log.i(TAG, "Found Characteristic: " + mCharacteristic.getUuid().toString()); 
       } 
       Log.i(TAG, "onServicesDiscovered UUID: " + gattService.getUuid().toString()); 
      } 
     } else { 
      Log.w(TAG, "onServicesDiscovered received: " + status); 
     } 
0

我期望的UUID的形式爲: 0000AABB-0000-1000-8000-00805F9B34FB。另外使用UUID.fromString(「你的uuid」)會容易得多。如果你知道uuid的特徵,在onServicesDiscovered()裏面你可以直接啓用char:

onServicesDiscovered() 
{ 
      BluetoothGattCharacteristic characteristic = gatt.getService(
        UUID.fromString(SENSOR_SERVICE_UUID)).getCharacteristic(
        UUID.fromString(CONFIG_UUID 
        )); 

      characteristic.setValue(new byte[]{0x01}); 
      gatt.writeCharacteristic(characteristic); 
}