2014-01-23 167 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); 
}