Android Ble 在 BLE 设备上的 GATT 服务中找不到特征

标签 android sdk bluetooth bluetooth-lowenergy

我在为我的 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 有问题?

最佳答案

这就是您要找的。它是 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);
        }

关于Android Ble 在 BLE 设备上的 GATT 服务中找不到特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21298780/

相关文章:

iphone - 网址为:(空),但请求字符串不为空

iphone - 在iPhone上实现蓝牙数据传输

android - 在android中使用变量访问XML

安卓 OpenCV : cannot dlopen camera wrapper library

ios - IBAction UIButton 触发延迟

java - 如何克服这个异常

c# - Windows Phone 8 蓝牙错误 HRESULT : 0x8007271D

android - 单击按钮即可发现蓝牙,无需导航至蓝牙设置页面

android - 如何获取当前 Facebook 用户的公共(public) ID(不是 scoped-id)或获取当前用户的个人资料照片

java - 无法从 json 中读取 URL 字符串并将其应用于 Android 中的对象