From fc781a8a8608f00057b140ae9392bf437cb490c9 Mon Sep 17 00:00:00 2001 From: Sudha Bheemanna Date: Wed, 30 Sep 2015 12:31:53 +0530 Subject: [PATCH] [TNEF-6079] [Tutorial][Network] errors in snippets Fixed the Jira issue in the BT documentation. Change-Id: I31e105de0371e25a1d0edebb835ad04a113dfdc2 Signed-off-by: Sudha Bheemanna --- .../html/native/network/bluetooth_tutorial_n.htm | 257 +++++++++++---------- 1 file changed, 130 insertions(+), 127 deletions(-) mode change 100644 => 100755 org.tizen.tutorials/html/native/network/bluetooth_tutorial_n.htm diff --git a/org.tizen.tutorials/html/native/network/bluetooth_tutorial_n.htm b/org.tizen.tutorials/html/native/network/bluetooth_tutorial_n.htm old mode 100644 new mode 100755 index 9601238..f62f869 --- a/org.tizen.tutorials/html/native/network/bluetooth_tutorial_n.htm +++ b/org.tizen.tutorials/html/native/network/bluetooth_tutorial_n.htm @@ -832,7 +832,7 @@ int ret = 0; ret = bt_gatt_client_create(remote_addr, &client); if (ret == BT_ERROR_NONE)    dlog_print(DLOG_INFO, LOG_TAG, "Success"); -break; +return; @@ -850,7 +850,7 @@ int ret = 0; ret = bt_gatt_client_destroy(client); if (ret == BT_ERROR_NONE)    dlog_print(DLOG_INFO, LOG_TAG, "Success"); -break; +return; @@ -862,7 +862,7 @@ char *addr = NULL; ret = bt_gatt_client_get_remote_address(client, &addr); if (ret == BT_ERROR_NONE)    dlog_print(DLOG_INFO, LOG_TAG, "Success"); -break; +return; @@ -876,7 +876,7 @@ if (ret != BT_ERROR_NONE) {    dlog_print(DLOG_INFO, LOG_TAG, "fail"); } -break; +return;
  • Use the bt_gatt_client_foreach_svc_cb() callback to initiate the service characteristics discovery: @@ -957,26 +957,28 @@ __bt_gatt_client_foreach_desc_cb(int total, int index, bt_gatt_h desc_handle, vo   if (ret != BT_ERROR_NONE)   {      dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_client_get_service is failed : %d", ret); -     break; +     return;   }   ret = bt_gatt_service_get_characteristic(svc, chr_uuid, &chr);   if (ret != BT_ERROR_NONE)   {      dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_service_get_characteristic is failed : %d", ret); -     break; +     return; +  } + +  ret = bt_gatt_characteristic_get_descriptor(chr, desc_uuid, &desc); +  if (ret != BT_ERROR_NONE) +  { +     dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_characteristic_get_descriptor is failed : %d", ret); +     return;   } -     ret = bt_gatt_characteristic_get_descriptor(chr, desc_uuid, &desc); -     if (ret != BT_ERROR_NONE) -     { -        dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_characteristic_get_descriptor is failed : %d", ret); -        break; -     }   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 is failed : %d", ret); +     return;   }

    After the reading operation is complete, use the bt_gatt_client_read_complete_cb() callback to handle values:

    @@ -1015,21 +1017,21 @@ __bt_gatt_client_read_complete_cb(int result, bt_gatt_h gatt_handle, void *data)    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_client_get_service is failed : %d", ret); -   break; +   return;    }    ret = bt_gatt_service_get_characteristic(svc, chr_uuid, &chr);    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_service_get_characteristic is failed : %d", ret); -      break; +      return;    }    ret = bt_gatt_characteristic_get_descriptor(chr, desc_uuid, &desc);    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_characteristic_get_descriptor is failed : %d", ret); -      break; +      return;    }    ret = __bt_gatt_client_set_value("int32", @@ -1037,10 +1039,65 @@ __bt_gatt_client_read_complete_cb(int result, bt_gatt_h gatt_handle, void *data)    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_set_value is failed : %d", ret); -      break; +      return;    }    ret = bt_gatt_client_write_value(desc, __bt_gatt_client_write_complete_cb, NULL); +   if (ret != BT_ERROR_NONE) +   { +      dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_client_write_value is failed : %d", ret); +      return; +   } + + +

    __bt_gatt_client_set_value is defined below:

    +
    +int __bt_gatt_client_set_value(char *type, char *value, bt_gatt_h h)
    +{
    +   int ret;
    +   int s_val;
    +   unsigned int u_val;
    +   char *buf;
    +   int len;
    +
    +   if (strcasecmp(type, "int8") == 0) {
    +     s_val = atoi(value);
    +     buf = (char *)&s_val;
    +     len = 1;
    +   } else if (strcasecmp(type, "int16") == 0) {
    +     s_val = atoi(value);
    +     buf = (char *)&s_val;
    +     len = 2;
    +   } else if (strcasecmp(type, "int32") == 0) {
    +     s_val = atoi(value);
    +     buf = (char *)&s_val;
    +     len = 4;
    +   } else if (strcasecmp(type, "uint8") == 0) {
    +     u_val = strtoul(value, NULL, 10);
    +     buf = (char *)&u_val;
    +     len = 1;
    +   } else if (strcasecmp(type, "uint16") == 0) {
    +     u_val = strtoul(value, NULL, 10);
    +     buf = (char *)&u_val;
    +     len = 2;
    +   } else if (strcasecmp(type, "uint32") == 0) {
    +     u_val = strtoul(value, NULL, 10);
    +     buf = (char *)&u_val;
    +     len = 4;
    +   } else if (strcasecmp(type, "str") == 0) {
    +     buf = value;
    +     len = strlen(buf);
    +   } else
    +     return BT_ERROR_INVALID_PARAMETER;
    +
    +   ret = bt_gatt_set_value(h, buf, len);
    +   if (ret != BT_ERROR_NONE)
    +     TC_PRT("bt_gatt_set_value is failed : %d", ret);
    +
    +   return ret;
    +}
    +
    +
  • After the writing operation is complete, use the bt_gatt_client_write_complete_cb() callback to finish the task:

    @@ -1073,20 +1130,21 @@ __bt_gatt_client_write_complete_cb(int result, bt_gatt_h gatt_handle, void *data    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_client_get_service is failed : %d", ret); -      break; +      return;    }    ret = bt_gatt_service_get_characteristic(svc, chr_uuid, &chr);    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_service_get_characteristic is failed : %d", ret); -      break; +      return;    }    ret = bt_gatt_client_set_characteristic_value_changed_cb(chr, __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 is failed : %d", ret); +      return;    }

    After registering the callback operation, use the __bt_gatt_client_value_changed_cb() callback to display the changed value:

    @@ -1125,20 +1183,21 @@ __bt_gatt_client_value_changed_cb(bt_gatt_h chr, char *value, int len, void *use    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_client_get_service is failed : %d", ret); -      break; +      return;    }    ret = bt_gatt_service_get_characteristic(svc, chr_uuid, &chr);    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_service_get_characteristic is failed : %d", ret); -      break; +      return;    }    ret = bt_gatt_client_unset_characteristic_value_changed_cb(chr);    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_client_unset_characteristic_value_changed_cb is failed : %d", ret); +      return;    } } @@ -1151,8 +1210,9 @@ int ret = 0; ret = bt_gatt_client_destroy(client); if (ret == BT_ERROR_NONE)    dlog_print(DLOG_INFO, LOG_TAG, "Success"); + client = NULL; -break; +return; @@ -1337,7 +1397,7 @@ break;    bt_gatt_h dsc = NULL;    // dsc = Assuming descriptor handle is already available -   ret = bt_gatt_descriptor_get_characteristic(des, &chr); +   ret = bt_gatt_descriptor_get_characteristic(dsc, &chr);    if (ret != BT_ERROR_NONE)    {       dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_descriptor_get_characteristic is failed : %d", ret); @@ -1360,7 +1420,7 @@ break;
  • Get the write type of the specified characteristic:
        bt_gatt_h chr = NULL;
    -   bt_gatt_write_type_e write_type = NULL;
    +   bt_gatt_write_type_e write_type;
        // svc = Assuming characteristic handle is already available
     
        ret = bt_gatt_characteristic_get_write_type(chr, &write_type);
    @@ -1440,7 +1500,8 @@ if (ret == BT_ERROR_NONE)
     
     void __write_completed_cb(int result, bt_gatt_h request_handle, void *user_data)
     {
    -
    +  if (result != BT_ERROR_NONE)
    +    dlog_print(DLOG_INFO, LOG_TAG, "Write request failed");
     }
     
     int main()
    @@ -1820,7 +1881,21 @@ main()
     
     static void __bt_adapter_le_device_discovery_state_changed_cb(int result, bt_adapter_le_device_discovery_state_e discovery_state, bt_adapter_le_device_discovery_info_s *discovery_info, void *user_data);
     {
    +   if (discovery_info == NULL && discovery_state == BT_ADAPTER_LE_DEVICE_DISCOVERY_FOUND)
    +     dlog_print(DLOG_ERROR, LOG_TAG, "No discovery_info!.");
    +   return;
    +
    +   if (discovery_state != BT_ADAPTER_LE_DEVICE_DISCOVERY_FOUND) {
    +     dlog_print(DLOG_INFO, LOG_TAG, "LE discovery %s", discovery_state == BT_ADAPTER_LE_DEVICE_DISCOVERY_STARTED? "Started" : "Finished");
    +   }
    +   else
    +   {
    +     dlog_print(DLOG_INFO, LOG_TAG,"%s Adv %d Scan resp %d RSSI %d Addr_type %d", discovery_info->remote_address,
    +     discovery_info->adv_data_len, discovery_info->scan_data_len, discovery_info->rssi,discovery_info->address_type);
     
    +     if (discovery_info->adv_data_len > 31 || discovery_info->scan_data_len > 31)
    +     bt_adapter_le_stop_device_discovery();
    +   }
     }
     
     int main()
    @@ -1836,7 +1911,7 @@ int main()
           dlog_print(DLOG_ERROR, LOG_TAG, "[bt_adapter_le_start_device_discovery] Failed.");
        }
        // To unset the LE device discovery state change callback
    -   ret = bt_adapter_le_unset_device_discovery_state_changed_cb()
    +   ret = bt_adapter_le_unset_device_discovery_state_changed_cb();
     
        return;
     }
    @@ -1851,14 +1926,10 @@ int main()
     

    Add the advertising data:

    -static bt_advertiser_h advertiser = NULL;
    -static bt_advertiser_h advertiser_list[3] = {NULL, };
    -static int advertiser_index = 0;
    +   static bt_advertiser_h advertiser = NULL;
    +   static bt_advertiser_h advertiser_list[3] = {NULL, };
    +   static int advertiser_index = 0;
     
    -int 
    -le_add_advertising_data()
    -{
    -   int adv_data_type = 3; // Default all
        int manufacturer_id = 117;
        char *manufacture = NULL;
        char manufacture_0[] = {0x0, 0x0, 0x0, 0x0};
    @@ -1891,107 +1962,39 @@ le_add_advertising_data()
              dlog_print(DLOG_INFO, LOG_TAG, "clear scan response data [0x%04x]", ret);
        }
     
    -   switch (adv_data_type) 
    -   {
    -      case 0: // Service UUID
    -         ret = bt_adapter_le_add_advertising_service_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING,  
    -                                                          time_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_uuid [0x%04x]", ret);
    -
    -         ret = bt_adapter_le_add_advertising_service_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                          battery_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_uuid [0x%04x]", ret);
    -
    -         manufacture = manufacture_0;
    -         break;
    -
    -      case 1: // Service solicitation
    -         ret = bt_adapter_le_add_advertising_service_solicitation_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                                       heart_rate_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "dd service_solicitation_uuid [0x%04x]", ret);
    +   ret = bt_adapter_le_add_advertising_service_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING,
    +         time_svc_uuid_16);
    +   if (ret != BT_ERROR_NONE)
    +       dlog_print(DLOG_INFO, LOG_TAG, "add service_uuid [0x%04x]", ret);
     
    -         ret = bt_adapter_le_add_advertising_service_solicitation_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                                       immediate_alert_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_solicitation_uuid [0x%04x]", ret);
    +   ret = bt_adapter_le_add_advertising_service_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING,
    +         battery_svc_uuid_16);
    +   if (ret != BT_ERROR_NONE)
    +       dlog_print(DLOG_INFO, LOG_TAG, "add service_uuid [0x%04x]", ret);
     
    -         manufacture = manufacture_1;
    -         break;
    +   ret = bt_adapter_le_add_advertising_service_solicitation_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING,
    +         heart_rate_svc_uuid_16);
    +   if (ret != BT_ERROR_NONE)
    +       dlog_print(DLOG_INFO, LOG_TAG, "add service_solicitation_uuid [0x%04x]", ret);
     
    -      case 2: // Appearance & TX power level
    -         ret = bt_adapter_le_set_advertising_appearance(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                        appearance);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add appearance data [0x%04x]", ret);
    +   ret = bt_adapter_le_add_advertising_service_solicitation_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING,
    +         immediate_alert_svc_uuid_16);
    +   if (ret != BT_ERROR_NONE)
    +       dlog_print(DLOG_INFO, LOG_TAG, "add service_solicitation_uuid [0x%04x]", ret);
     
    -         ret = bt_adapter_le_set_advertising_tx_power_level(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, true);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print (DLOG_INFO, LOG_TAG, "add appearance data [0x%04x]", ret);
    +   ret = bt_adapter_le_set_advertising_appearance(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING,appearance);
    +   if (ret != BT_ERROR_NONE)
    +       dlog_print(DLOG_INFO, LOG_TAG, "add appearance data [0x%04x]", ret);
     
    -         manufacture = manufacture_2;		
    -         break;
    +   ret = bt_adapter_le_set_advertising_tx_power_level(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING,true);
    +   if (ret != BT_ERROR_NONE)
    +       dlog_print(DLOG_INFO, LOG_TAG, "add tx_power_level [0x%04x]", ret);
     
    -      case 3: // All
    -         ret = bt_adapter_le_add_advertising_service_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                          time_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_uuid [0x%04x]", ret);
    -
    -         ret = bt_adapter_le_add_advertising_service_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                          battery_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_uuid [0x%04x]", ret);
    -
    -         ret = bt_adapter_le_add_advertising_service_solicitation_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                                       heart_rate_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_solicitation_uuid [0x%04x]", ret);
    -
    -         ret = bt_adapter_le_add_advertising_service_solicitation_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                                       immediate_alert_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_solicitation_uuid [0x%04x]", ret);
    -
    -         ret = bt_adapter_le_set_advertising_appearance(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                        appearance);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add appearance data [0x%04x]", ret);
    -
    -         ret = bt_adapter_le_set_advertising_tx_power_level(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                            true);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add tx_power_level [0x%04x]", ret);
    -
    -         manufacture = manufacture_3;
    -         break;
    +   manufacture = manufacture_3;
     
    -      case 4: // ANCS {
    -         ret = bt_adapter_le_add_advertising_service_solicitation_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                                       time_svc_uuid_16);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_solicitation_uuid [0x%04x]", ret);
    -
    -         ret = bt_adapter_le_add_advertising_service_solicitation_uuid(advertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, 
    -                                                                       ancs_uuid_128);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print(DLOG_INFO, LOG_TAG, "add service_solicitation_uuid [0x%04x]", ret);
    -
    -         ret = bt_adapter_le_set_advertising_device_name(advertiser, BT_ADAPTER_LE_PACKET_SCAN_RESPONSE, true);
    -         if (ret != BT_ERROR_NONE)
    -            dlog_print (DLOG_INFO, LOG_TAG, "set device name [0x%04x]", ret);
    -       return 0;
    -      }
    -      default:
    -         dlog_print(DLOG_INFO, LOG_TAG, "No adv data");
    -         break;
    -   }
        // Default scan response data 
        ret = bt_adapter_le_add_advertising_service_data(advertiser, BT_ADAPTER_LE_PACKET_SCAN_RESPONSE, 
    -                                                    time_svc_uuid_16, service_data, 
    -                                                    sizeof(service_data));
    +         time_svc_uuid_16, service_data, sizeof(service_data));
        if (ret != BT_ERROR_NONE)
           dlog_print(DLOG_INFO, LOG_TAG, "add service_data [0x%04x]", ret);
     
    @@ -2000,10 +2003,10 @@ le_add_advertising_data()
           dlog_print(DLOG_INFO, LOG_TAG, "set device name [0x%04x]", ret);
     
        ret = bt_adapter_le_add_advertising_manufacturer_data(advertiser, BT_ADAPTER_LE_PACKET_SCAN_RESPONSE, 
    -                                                         manufacturer_id, manufacture, sizeof(manufacture_0));
    +         manufacturer_id, manufacture, sizeof(manufacture_0));
        if (ret != BT_ERROR_NONE)
           dlog_print(DLOG_INFO, LOG_TAG, "add manufacturer data [0x%04x]", ret);
    -}
    +
     
  • @@ -2115,7 +2118,7 @@ static void __bt_adapter_le_advertising_state_changed_cb_3(int result, bt_adapter_le_advertising_state_changed_cb cb; if (advertiser_index == 0) cb = __bt_adapter_le_advertising_state_changed_cb; -else if (advertiser_index == 0) cb = __bt_adapter_le_advertising_state_changed_cb_2; +else if (advertiser_index == 1) cb = __bt_adapter_le_advertising_state_changed_cb_2; else cb = __bt_adapter_le_advertising_state_changed_cb_3; advertiser = advertiser_list[advertiser_index]; -- 2.7.4