Languages

Menu
Sites
Language
Gatt server implementation

Hi everybody,

I'm trying to implement a simple ble gatt server with one service containing one characteristics using tizen studio for the samsung gear S3. I followed what i could find on the docs but it doesn't seems to work and now i'm a bit lost. Here my code:

    if(bt_initialize() == BT_ERROR_NONE)
    {
        dlog_print(DLOG_INFO, LOG_TAG, "Bluetooth initialized successfully.");

        bt_adapter_state_e state = BT_ADAPTER_DISABLED;
        int ret = bt_adapter_get_state(&state);
        if (state == BT_ADAPTER_ENABLED)
            dlog_print(DLOG_INFO, LOG_TAG, "Bluetooth adapter initialized successfully.");

        // Creating gatt service
        if(bt_gatt_server_initialize() == BT_ERROR_NONE)
        {
            dlog_print(DLOG_INFO, LOG_TAG, "Gatt server initialized successfully.");

            int retval = bt_gatt_server_create(&(ad->gatt_server_handler));
            if(retval == BT_ERROR_NONE)
            {
                dlog_print(DLOG_INFO, LOG_TAG, "Gatt server created successfully.");

                if(bt_gatt_service_create(MIL_SERVICE_UUID, BT_GATT_SERVICE_TYPE_PRIMARY, &(ad->service_handler)) == BT_ERROR_NONE)
                {
                    dlog_print(DLOG_INFO, LOG_TAG, "Gatt service created successfully.");

                    if(bt_gatt_server_register_service(ad->gatt_server_handler, ad->service_handler) == BT_ERROR_NONE)
                    {
                        dlog_print(DLOG_INFO, LOG_TAG, "Service successfully added to gatt server.");

                        const char* char_val = "50";
                        if(bt_gatt_characteristic_create(MIL_CHARACTERISTIC_UUID, BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE,
                                                         BT_GATT_PROPERTY_READ | BT_GATT_PROPERTY_WRITE | BT_GATT_PROPERTY_NOTIFY,
                                                         char_val, 8, &(ad->characteristic_handler)) == BT_ERROR_NONE)
                        {
                            dlog_print(DLOG_INFO, LOG_TAG, "Characteristic created successfully.");

                            if(bt_gatt_service_add_characteristic(ad->service_handler, ad->characteristic_handler) == BT_ERROR_NONE)
                            {
                                dlog_print(DLOG_INFO, LOG_TAG, "Characteristic successfully added to service.");

                                if(bt_adapter_le_create_advertiser(&(ad->advertiser_handler)) == BT_ERROR_NONE)
                                {
                                    dlog_print(DLOG_INFO, LOG_TAG, "Advertiser successfully created.");

                                    const char *serviceUUID = "180F";
                                    int ret = bt_adapter_le_add_advertising_service_uuid(ad->advertiser_handler, BT_ADAPTER_LE_PACKET_ADVERTISING, serviceUUID);
                                    if(ret == BT_ERROR_NONE)
                                    {
                                        dlog_print(DLOG_INFO, LOG_TAG, "Service successfully added to advertiser.");

                                        if(bt_adapter_le_set_advertising_device_name(ad->advertiser_handler, BT_ADAPTER_LE_PACKET_SCAN_RESPONSE, true) == BT_ERROR_NONE)
                                        {
                                            dlog_print(DLOG_INFO, LOG_TAG, "Device name successfully added to advertiser.");

                                            int ret = bt_adapter_le_start_advertising_new(ad->advertiser_handler, NULL, NULL);
                                            if(ret == BT_ERROR_NONE)
                                            {
                                                dlog_print(DLOG_INFO, LOG_TAG, "Advertiser successfully started.");

                                            }
                                            else
                                            {
                                                dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when starting advertiser! %d", ret);
                                            }
                                        }
                                        else
                                        {
                                            dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when adding device name to advertiser!");
                                        }
                                    }
                                    else
                                    {
                                        char* errMsg = get_error_message(ret);
                                        dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when adding service to advertiser! %d", ret);
                                        dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when adding service to advertiser! %s", errMsg);
                                    }
                                }
                                else
                                {
                                    dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when creating advertiser!");
                                }

                                if(bt_gatt_server_start() == BT_ERROR_NONE)
                                {
                                    dlog_print(DLOG_INFO, LOG_TAG, "Gatt server successfully started.");
                                }
                                else
                                {
                                    dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when starting gatt server!");
                                }
                            }
                            else
                            {
                                dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when adding characteristic to service!");
                            }
                        }
                        else
                        {
                            dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when creating characteristic!");
                        }
                    }
                    else
                    {
                        dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when adding service to gatt server!");
                    }
                }
                else
                {
                    dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when creating gatt service!");
                }

            }
            else
            {
                dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when creating gatt server!");
            }
        }
        else
        {
            dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when initializing gatt server!");
        }
    }
    else
    {
        dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when initializing bluetooth!");
    }

I know my code isn't very digest but i just wanted to test quickly how to setup the whole gatt and clean up the code when this works. There aren't any errors in my log but sadly when i try to see this new service using a ble sniffer on my phone it doesn't show up (only the 2 basic services are showing up). Here my log:

06-26 08:18:24.189 : INFO / milservice ( 4679 : 4679 ) : Bluetooth initialized successfully.
06-26 08:18:24.209 : INFO / milservice ( 4679 : 4679 ) : Bluetooth adapter initialized successfully.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Gatt server initialized successfully.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Gatt server created successfully.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Gatt service created successfully.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Service successfully added to gatt server.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Characteristic created successfully.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Characteristic successfully added to service.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Advertiser successfully created.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Service successfully added to advertiser.
06-26 08:18:24.229 : INFO / milservice ( 4679 : 4679 ) : Device name successfully added to advertiser.
06-26 08:18:24.449 : INFO / milservice ( 4679 : 4679 ) : Advertiser successfully started.
06-26 08:18:24.459 : INFO / milservice ( 4679 : 4679 ) : Gatt server successfully started.

I don't know what i'm missing or if i do something wrong..

 

Responses

7 Replies
Armaan-Ul- Islam
All the logs look okay, don't they? I would recommend you to check this post on tizen developers forum: https://developer.tizen.org/forums/native-application-development/ble-gatt-server-implementation?langswitch=en Here 'Marco Buettner' stated: Tizen 2.3.x support GATT client only. GATT server are planned for Tizen 2.4 So, this could be the reason.
Gregory CORDIER

Hi Steve, have you been able to solve the problem ? I am able to add Service but then it seems to be empty with ble sniffer...

Armaan-Ul- Islam

Hello Gregory CORDIER,

Hope You Checked the Issue: [TW-4] ( and [TW-27] ) on bugs.tizen.org as I mentioned on my response ?

 

https://bugs.tizen.org/browse/TW-4?focusedCommentId=52118&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-52118 or just https://bugs.tizen.org/browse/TW-4

 

https://bugs.tizen.org/browse/TW-27?focusedCommentId=54688&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-54688 or just https://bugs.tizen.org/browse/TW-27

 

Gatt Server is Implemented from Tizen 2.4 (& higher). So, If you are using tizen wearable for Gatt Server implementation, please update your gear to Tizen 3.0, and if you are using tizen mobile please update to Tizen 2.4. Additional Info: Tizen 3.0 is recently available for GearS3

Dmitri G

Hi Steve, Gregory,

Have you been able to see your new service with the ble sniffer? I am running into identical problem: gatt server gets created successfully, however it does not get detected by the sniffer.

One additional clue I see from the logs is the following message:
I/BLUETOOTH_FRWK_APK ( 3267): bt-gatt-service.c: bluetooth_gatt_init(1601) > manager_id does not exists

Can anyone tell, where is bt-gatt-service.c located?

thanks,

Dmitri

 

Dmitri G

Ok, I found bt-gatt-service.c under https://review.tizen.org/git/?p=platform/core/connectivity/bluetooth-frwk.git;a=tree;f=bt-api;h=9787eee44225d6330421a8c4337ea9a1543bcb78;hb=HEAD

Dmitri G

Ok, I found the reason why the gatt server wasn't being detected:

bt_gatt_server_start() needs to be called only after bt_gatt_server_register_service(). Also bt_gatt_server_register_service() needs to be called only after adding characteristics.

Gregory CORDIER
Thank you Dmitri G for your feedback. I was not able to make it work and was working on something else these days. I will try your solution and see if it works...
I think we also need to add descriptors tocharacteristic in order to implement standard Bluetooth profiles...