You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tizen 3.0.0.2下Bluetooth LE GATT Server特征值始终为NULL求助

Hey there, let's dig into why your BlueScanner isn't showing the values for your BLE characteristic and descriptor. I spotted a few key issues in your code that are likely causing this:

1. Incorrect Descriptor UUID Format

Your descriptor UUID is set as "0x1002" — BLE UUIDs don't use the 0x prefix. For custom 16-bit UUIDs, you should use just the string "1002", but keep in mind non-SIG 16-bit UUIDs can have compatibility issues. For better reliability, use a full 128-bit UUID that matches your service's base pattern (e.g., tweak the last few characters of your service UUID).

2. Missing Read Callback Implementation

You registered a read callback (__bt_gatt_server_read_value_requested_cb) but didn't provide its code. BLE clients like BlueScanner rely on this callback to receive the characteristic value when they send a read request. Without it, the client won't get any data to display.

3. Initial Value Handling for Characteristic/Descriptor

  • For the characteristic, you passed a string but didn't explicitly cast it to a byte array or specify its length correctly.
  • For the descriptor, "50" is a string, but if you intend to send a numeric value of 50, you should pass a byte representation instead of a string literal.

4. Double-Check Advertising Setup (Bonus)

Make sure your Gear S3 is advertising the service UUID you created. If the client can't discover the service properly, it won't be able to access its characteristics/descriptors at all.


Here's the revised code with fixes applied:

#include <string.h>
#include <dlog.h>
#include <bluetooth.h>

#define LOG_TAG "BLE_SERVER"
#define LE_INITIAL_BUF_SIZE 64

const char *service_uuid = "ADE3D529-C784-4F63-A987-EB69F70EE816";
bt_gatt_service_type_e type = BT_GATT_SERVICE_TYPE_PRIMARY;

// Server handles
static bt_gatt_server_h gattServer = NULL;
static bt_gatt_h gattSvc = NULL;
static bt_gatt_h gattChara = NULL;
static bt_gatt_h gattDescriptor = NULL;

const char *charaUuid = "AD7B334F-4637-4B86-90B6-9D787F03D218";
const char *charaValue = "myCharacteristic";

// Fixed descriptor UUID (matching service base pattern)
const char *DescUuid = "ADE3D529-C784-4F63-A987-EB69F70EE817";
uint8_t descValue = 50; // Numeric value as byte

// Implement the read callback to return characteristic value
static void __bt_gatt_server_read_value_requested_cb(bt_gatt_h characteristic, unsigned int offset, void *user_data) {
    int ret = bt_gatt_server_send_read_value_response(
        characteristic, 
        BT_ERROR_NONE, 
        offset, 
        (unsigned char*)charaValue, 
        strlen(charaValue)
    );
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "Failed to send read response: %s", err);
    }
}

void createService() {
    dlog_print(DLOG_INFO, LOG_TAG, "Create Service");
    int ret = bt_gatt_server_initialize();
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "Init GATT server failed. err = %s", err);
        return;
    }

    if (!gattServer) {
        ret = bt_gatt_server_create(&gattServer);
        if (ret != BT_ERROR_NONE) {
            char* err = get_error_message(ret);
            dlog_print(DLOG_ERROR, LOG_TAG, "Create GATT server failed. err = %s", err);
            return;
        } else {
            dlog_print(DLOG_INFO, LOG_TAG, "Create GATT server Succeed");
        }
    }

    ret = bt_gatt_service_create(service_uuid, type, &gattSvc);
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "Create GATT service failed. err = %s", err);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "Create GATT service Succeed");
    }

    ret = bt_gatt_characteristic_create(
        charaUuid, 
        BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE, 
        BT_GATT_PROPERTY_INDICATE | BT_GATT_PROPERTY_READ, 
        (unsigned char*)charaValue, 
        strlen(charaValue), 
        &gattChara
    );
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "create characteristic failed. err = %s", err);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "create characteristic Succeed");
    }

    ret = bt_gatt_server_set_read_value_requested_cb(gattChara, __bt_gatt_server_read_value_requested_cb, NULL);
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "register read request callback failed. err = %s", err);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "register read request callback Succeed");
    }

    ret = bt_gatt_descriptor_create(
        DescUuid, 
        BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE, 
        &descValue, 
        sizeof(descValue), 
        &gattDescriptor
    );
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "create descriptor failed. err = %s", err);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "create descriptor server Succeed");
    }

    ret = bt_gatt_characteristic_add_descriptor(gattChara, gattDescriptor);
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "add descriptor to characteristic failed. err = %s", err);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "add descriptor to characteristic Succeed");
    }

    ret = bt_gatt_service_add_characteristic(gattSvc, gattChara);
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "add characteristic to service failed. err = %s", err);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "add characteristic to service Succeed");
    }

    ret = bt_gatt_server_register_service(gattServer, gattSvc);
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "register GATT service failed. err = %s", err);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "register GATT service Succeed");
    }

    ret = bt_gatt_server_start();
    if (ret != BT_ERROR_NONE) {
        char* err = get_error_message(ret);
        dlog_print(DLOG_ERROR, LOG_TAG, "start GATT server failed. err = %s", err);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, "start GATT server Succeed");
    }
}

Final Checks

  1. After applying these fixes, restart your Gear S3 app and re-scan with BlueScanner.
  2. Tap directly on the characteristic/descriptor in BlueScanner to trigger a read request — some scanners don't auto-fetch values.
  3. Verify all your log messages show "Succeed" for each step to ensure no hidden errors.

内容的提问来源于stack exchange,提问作者Mick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:31:42