xref: /btstack/port/arduino/examples/ANCS/ANCS.ino (revision 8caefee39d444df6d8908a96a844825f10fbdaa4)
1#include <BTstack.h>
2#include <stdio.h>
3#include "att_server.h"
4#include "gatt_client.h"
5#include "ancs_client_lib.h"
6#include "sm.h"
7#include <SPI.h>
8
9/*
10 * EXAMPLE_START(ANCS): ANCS Client
11 */
12
13/*
14 * @section Advertisement
15 * @text An ANCS Client needs to include the ANCS UUID in its advertisement to
16 * get recognized by iOS
17 */
18
19/* LISTING_START(ANCSAdvertisement): ANCS Advertisement */
20const uint8_t adv_data[] = {
21    // Flags general discoverable
22    0x02, 0x01, 0x02,
23    // Name
24    0x05, 0x09, 'A', 'N', 'C', 'S',
25    // Service Solicitation, 128-bit UUIDs - ANCS (little endian)
26    0x11,0x15,0xD0,0x00,0x2D,0x12,0x1E,0x4B,0x0F,0xA4,0x99,0x4E,0xCE,0xB5,0x31,0xF4,0x05,0x79
27};
28/* LISTING_END(ANCSAdvertisement): ANCS Advertisement */
29
30/*
31 * @section Setup
32 *
33 * @text In the setup, the LE Security Manager is configured to accept pairing requests.
34 * Then, the ANCS Client library is initialized and and ancs_callback registered.
35 * Finally, the Advertisement data is set and Advertisements are started.
36 */
37
38/* LISTING_START(ANCSSetup): ANCS Setup */
39void setup(void){
40
41    Serial.begin(9600);
42    Serial.println("BTstack ANCS Client starting up...");
43
44    // startup BTstack and configure log_info/log_error
45    BTstack.setup();
46
47    sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
48    sm_set_authentication_requirements( SM_AUTHREQ_BONDING );
49
50    // setup ANCS Client
51    ancs_client_init();
52    ancs_client_register_callback(&ancs_callback);
53
54    // enable advertisements
55    BTstack.setAdvData(sizeof(adv_data), adv_data);
56    BTstack.startAdvertising();
57}
58/* LISTING_END(ANCSSetup): ANCS Setup */
59
60void loop(void){
61    BTstack.loop();
62}
63
64/*
65 * @section ANCS Callback
66 * @text In the ANCS Callback, connect and disconnect events are received.
67 * For actual notifications, ancs_client_attribute_name_for_id allows to
68 * look up the name. To get the notification body, e.g., the actual message,
69 * the GATT Client needs to be used direclty.
70 */
71
72/* LISTING_START(ANCSCallback): ANCS Callback */
73void ancs_callback(ancs_event_t * event){
74    const char * attribute_name;
75    switch (event->type){
76        case ANCS_CLIENT_CONNECTED:
77            Serial.println("ANCS Client: Connected");
78            break;
79        case ANCS_CLIENT_DISCONNECTED:
80            Serial.println("ANCS Client: Disconnected");
81            break;
82        case ANCS_CLIENT_NOTIFICATION:
83            attribute_name = ancs_client_attribute_name_for_id(event->attribute_id);
84            if (!attribute_name) break;
85            Serial.print("Notification: ");
86            Serial.print(attribute_name);
87            Serial.print(" - ");
88            Serial.println(event->text);
89            break;
90        default:
91            break;
92    }
93}
94/* LISTING_END(ANCSCallback): ANCS Callback */
95
96