Tizen BLE Application, Descriptor write failed

Tizen BLE Application, Descriptor write failed

BY 10 Sep 2018 Native Application Development

Hello,

I need to develop a BLE Tizen Application, which will create a bond and write the Enable notification to Descriptor.

Tizen Application verson 2.3.1 building on Samsung Gear S3 Classic.

The Remote Device is Health Custom characteristic patch BLE device and It has two characteristic CHAR_NOTIFY and CHAR_WRITE and requires Encrypted channel for Writing using descriptor.

Using CHAR_NOTIFY characteristic , I have to get descriptor, on that descriptor, I have to perform read and write.

even I got the descriptor, read opearation is successful 

    ret = bt_gatt_client_read_value(desc, __bt_gatt_client_read_complete_cb, NULL);  by this method,

But when I am going to write on that descriptor ,It got failed. “Error :: Write failed -1”

ret = bt_gatt_client_write_value(desc, __write_completed_cb, NULL); by this Method
 
where desc is a descriptor achieved from get_characteristic.

below is code snippet :

#define CHAR_NOTIFY “00003a54-4f40-4c51-ad31-c410f4e12c04”
#define CHAR_WRITE “0000038b-4f40-4c51-ad31-c410f4e12c04”
#define CHAR_NOTIFYCCD “00002902-0000-1000-8000-00805f9b34fb”
#define SERVICE_UUID “0000f72b-4f40-4c51-ad31-c410f4e12c04”
 
ret = bt_initialize();
if (ret != BT_ERROR_NONE) {
dlog_print(DLOG_INFO, LOG_TAG, “BT init failed.”);
}
else {
dlog_print(DLOG_INFO, LOG_TAG, “BT init succeed.”);
}
ret = bt_adapter_le_start_scan(__bt_adapter_le_scan_result_cb, NULL);
if (ret != BT_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG, “[bt_adapter_le_start_scan] Failed.”);
}
 
 
void __bt_adapter_le_scan_result_cb(int result,bt_adapter_le_device_scan_result_info_s *info, void *user_data)
{
bt_adapter_le_packet_type_e pkt_type = BT_ADAPTER_LE_PACKET_ADVERTISING;
sprintf(str, “Device scanning”);
if (info == NULL)
{
dlog_print(DLOG_INFO, LOG_TAG, “No discovery_info!”);
return;
}
else
{
dlog_print(DLOG_INFO, LOG_TAG, “some info found for patch %s”,info->remote_address);
}
dlog_print(DLOG_INFO, LOG_TAG, “Got our device”);
bt_adapter_le_stop_scan();
ret = bt_gatt_set_connection_state_changed_cb(_bt_gatt_connection_state_changed_cb, NULL);
if (ret != BT_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG,”[__bt_gatt_connection_state_changed_cb] Failed.”);
}
else
{
dlog_print(DLOG_ERROR, LOG_TAG, “Callback registered. %d “, ret);
}
ret = bt_device_destroy_bond(info->remote_address);
if (ret != BT_ERROR_NONE) {
dlog_print(DLOG_ERROR, LOG_TAG, “[bt_device_destroy_bond] failed. %d”,ret);
        return;
    } else {
        dlog_print(DLOG_INFO, LOG_TAG, “[bt_device_destroy_bond] succeeded. bt_device_destroy_bond callback will be called.”);
    }
ret = bt_device_set_bond_created_cb(device_bond_created_cb, info->remote_address);
if (ret != BT_ERROR_NONE) {
    dlog_print(DLOG_ERROR, LOG_TAG, “[bt_device_set_bond_created_cb] failed. %d”,ret);
 
    return;
}
ret = bt_gatt_connect(info->remote_address, true);
    if (ret != BT_ERROR_NONE)
    {
    dlog_print(DLOG_INFO, LOG_TAG, “Failed to connect LE device.”, ret);
    } else {
    dlog_print(DLOG_INFO, LOG_TAG, “Connected to our LE device.%s”,info->remote_address);
    ret = bt_device_create_bond(info->remote_address);
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG, “[bt_device_create_bond] failed.”);
         return;
    } else {
   dlog_print(DLOG_INFO, LOG_TAG, “[bt_device_create_bond] succeeded. device_bond_created_cb callback will be called.”);
    }
    }
 
void device_bond_created_cb(int result, bt_device_info_s *device_info, void *user_data)
{bt_gatt_h svc = NULL;
bt_gatt_h chr = NULL;
bt_gatt_h chr1 = NULL;
bt_gatt_h desc = NULL;
 
    if (result != BT_ERROR_NONE) {
        dlog_print(DLOG_ERROR, LOG_TAG, “[bt_device_bond_created_cb] failed. result(%d).”, result);
        return;
    }
    dlog_print(DLOG_ERROR, LOG_TAG, “[bt_device_bond_created_cb] success. result(%d).”, result);
    ret=bt_device_set_authorization_changed_cb(__bt_device_authorization_changed_cb,device_info->remote_address);
            if (ret != BT_ERROR_NONE)
                {
            dlog_print(DLOG_INFO, LOG_TAG, “callback is not registered”);
                }
                else
                {
            dlog_print(DLOG_INFO, LOG_TAG, “athorization callback is  registered”);
             }
        ret=bt_device_set_authorization(device_info->remote_address,BT_DEVICE_AUTHORIZED);
        if (ret != BT_ERROR_NONE)
        {
    dlog_print(DLOG_INFO, LOG_TAG, “device is not authorized %d”,ret);
    dlog_print(DLOG_INFO, LOG_TAG, “device is not authorized remote address %s”,device_info->remote_address);
        }
        else
        {
    dlog_print(DLOG_INFO, LOG_TAG, “device authorized”);
     }
ret = bt_gatt_client_create(device_info->remote_address, &client);
if (ret == BT_ERROR_NONE)
{
dlog_print(DLOG_INFO, LOG_TAG, “Client created”);
Evas_Object *label = elm_label_add(box);
evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(box,EVAS_HINT_FILL,EVAS_HINT_FILL);
elm_object_text_set(label, “Client Created”);
elm_box_pack_end(box,label);
evas_object_show(label);
char *addr = NULL;
ret = bt_gatt_client_get_remote_address(client, &addr);
if (ret == BT_ERROR_NONE)
dlog_print(DLOG_INFO, LOG_TAG, “Success %s”,addr);
ret = bt_gatt_client_foreach_services(client, __bt_gatt_client_foreach_svc_cb, NULL);
if (ret != BT_ERROR_NONE)
{
dlog_print(DLOG_INFO, LOG_TAG, “fail”);
}
ret = bt_gatt_client_get_service(client, SERVICE_UUID, &svc);
if (ret != BT_ERROR_NONE) {
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_client_get_service failed: %d”, ret);
    return;
}
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_client_get_service success: %d”, ret);
 
ret = bt_gatt_service_get_characteristic(svc, CHAR_WRITE, &chr);
    if (ret != BT_ERROR_NONE) {
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_service_get_characteristic failed: %d”, ret);
     return;
    }
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_service_get_characteristic success: %d”, ret);
 
ret = bt_gatt_service_get_characteristic(svc, CHAR_NOTIFY, &chr1);
    if (ret != BT_ERROR_NONE) {
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_service_get_characteristic failed: %d”, ret);
 
    return;
    }
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_service_get_characteristic success: %d”, ret);
 
    ret = bt_gatt_client_set_characteristic_value_changed_cb(chr1, __bt_gatt_client_value_changed_cb,
    NULL);
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_client_set_characteristic_value_changed_cb failed: %d”, ret);
 
        return;
    }
 
ret = bt_gatt_characteristic_get_descriptor(chr1, CHAR_NOTIFYCCD, &desc);
if (ret != BT_ERROR_NONE) {
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_characteristic_get_descriptor failed: %d”, ret);
 
    return;
}
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_characteristic_get_descriptor success: %d”, ret);
 
 
 
    ret = bt_gatt_client_read_value(desc, __bt_gatt_client_read_complete_cb, NULL);
    if (ret != BT_ERROR_NONE) {
        dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_client_read_value failed: %d”, ret);
 
        return;
    }

 char *myArray = (char []) {0x01, 0x00 };

ret = bt_gatt_set_value(desc, myArray, 2);
if (ret != BT_ERROR_NONE) {
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_set_value failed: %d”, ret);
 
    return;
}
else {
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_set_value :”);
 
}

 

ret = bt_gatt_client_write_value(desc, __write_completed_cb, NULL);
 
if (ret != BT_ERROR_NONE) {
    dlog_print(DLOG_INFO, LOG_TAG, “bt_gatt_client_write_value failed: %d %s”, ret, ret);
 
    return;
}
 
}

I followed all the steps provided in the following URL:

https://developer.tizen.org/development/guides/native-application/connectivity-and-wireless/bluetooth

Actually , I build an Android Application which will successfully create a bond and write successfully with descriptor.

Please help me out what I am missing?

Written by