ret = bt_gatt_client_create(remote_addr, &client);
if (ret == BT_ERROR_NONE)
dlog_print(DLOG_INFO, LOG_TAG, "Success");
-break;
+return;
</pre>
</li>
ret = bt_gatt_client_destroy(client);
if (ret == BT_ERROR_NONE)
dlog_print(DLOG_INFO, LOG_TAG, "Success");
-break;
+return;
</pre>
</li>
ret = bt_gatt_client_get_remote_address(client, &addr);
if (ret == BT_ERROR_NONE)
dlog_print(DLOG_INFO, LOG_TAG, "Success");
-break;
+return;
</pre>
</li>
{
dlog_print(DLOG_INFO, LOG_TAG, "fail");
}
-break;
+return;
</pre>
</li>
<li>Use the <span style="font-family: Courier New,Courier,monospace;">bt_gatt_client_foreach_svc_cb()</span> callback to initiate the service characteristics discovery:
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;
}
</pre>
<p>After the reading operation is complete, use the <span style="font-family: Courier New,Courier,monospace;">bt_gatt_client_read_complete_cb()</span> callback to handle values:</p>
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",
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;
+ }
+
+</pre>
+<p>__bt_gatt_client_set_value is defined below:</p>
+<pre class="prettyprint">
+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;
+}
+</pre>
+</li>
</pre>
<p>After the writing operation is complete, use the <span style="font-family: Courier New,Courier,monospace;">bt_gatt_client_write_complete_cb()</span> callback to finish the task:</p>
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;
}
</pre>
<p>After registering the callback operation, use the <span style="font-family: Courier New,Courier,monospace;">__bt_gatt_client_value_changed_cb()</span> callback to display the changed value:</p>
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;
}
}
</pre>
ret = bt_gatt_client_destroy(client);
if (ret == BT_ERROR_NONE)
dlog_print(DLOG_INFO, LOG_TAG, "Success");
+
client = NULL;
-break;
+return;
</pre></li>
</ol>
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);
<li>Get the write type of the specified characteristic:
<pre class="prettyprint">
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);
<pre class="prettyprint">
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()
<pre class="prettyprint">
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()
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;
}
<p>Add the advertising data:</p>
<pre class="prettyprint">
-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};
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);
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);
-}
+
</pre>
</li>
<li>
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];