18caefee3SMatthias Ringwald#include <BTstack.h> 28caefee3SMatthias Ringwald#include <stdio.h> 3*3edc84c5SMatthias Ringwald#include "ble/att_server.h" 4*3edc84c5SMatthias Ringwald#include "ble/gatt_client.h" 58caefee3SMatthias Ringwald#include "ancs_client_lib.h" 6*3edc84c5SMatthias Ringwald#include "ble/sm.h" 78caefee3SMatthias Ringwald#include <SPI.h> 88caefee3SMatthias Ringwald 98caefee3SMatthias Ringwald/* 108caefee3SMatthias Ringwald * EXAMPLE_START(ANCS): ANCS Client 118caefee3SMatthias Ringwald */ 128caefee3SMatthias Ringwald 138caefee3SMatthias Ringwald/* 148caefee3SMatthias Ringwald * @section Advertisement 158caefee3SMatthias Ringwald * @text An ANCS Client needs to include the ANCS UUID in its advertisement to 168caefee3SMatthias Ringwald * get recognized by iOS 178caefee3SMatthias Ringwald */ 188caefee3SMatthias Ringwald 198caefee3SMatthias Ringwald/* LISTING_START(ANCSAdvertisement): ANCS Advertisement */ 208caefee3SMatthias Ringwaldconst uint8_t adv_data[] = { 218caefee3SMatthias Ringwald // Flags general discoverable 228caefee3SMatthias Ringwald 0x02, 0x01, 0x02, 238caefee3SMatthias Ringwald // Name 248caefee3SMatthias Ringwald 0x05, 0x09, 'A', 'N', 'C', 'S', 258caefee3SMatthias Ringwald // Service Solicitation, 128-bit UUIDs - ANCS (little endian) 268caefee3SMatthias Ringwald 0x11,0x15,0xD0,0x00,0x2D,0x12,0x1E,0x4B,0x0F,0xA4,0x99,0x4E,0xCE,0xB5,0x31,0xF4,0x05,0x79 278caefee3SMatthias Ringwald}; 288caefee3SMatthias Ringwald/* LISTING_END(ANCSAdvertisement): ANCS Advertisement */ 298caefee3SMatthias Ringwald 308caefee3SMatthias Ringwald/* 318caefee3SMatthias Ringwald * @section Setup 328caefee3SMatthias Ringwald * 338caefee3SMatthias Ringwald * @text In the setup, the LE Security Manager is configured to accept pairing requests. 348caefee3SMatthias Ringwald * Then, the ANCS Client library is initialized and and ancs_callback registered. 358caefee3SMatthias Ringwald * Finally, the Advertisement data is set and Advertisements are started. 368caefee3SMatthias Ringwald */ 378caefee3SMatthias Ringwald 388caefee3SMatthias Ringwald/* LISTING_START(ANCSSetup): ANCS Setup */ 398caefee3SMatthias Ringwaldvoid setup(void){ 408caefee3SMatthias Ringwald 418caefee3SMatthias Ringwald Serial.begin(9600); 428caefee3SMatthias Ringwald Serial.println("BTstack ANCS Client starting up..."); 438caefee3SMatthias Ringwald 448caefee3SMatthias Ringwald // startup BTstack and configure log_info/log_error 458caefee3SMatthias Ringwald BTstack.setup(); 468caefee3SMatthias Ringwald 478caefee3SMatthias Ringwald sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 488caefee3SMatthias Ringwald sm_set_authentication_requirements( SM_AUTHREQ_BONDING ); 498caefee3SMatthias Ringwald 508caefee3SMatthias Ringwald // setup ANCS Client 518caefee3SMatthias Ringwald ancs_client_init(); 528caefee3SMatthias Ringwald ancs_client_register_callback(&ancs_callback); 538caefee3SMatthias Ringwald 548caefee3SMatthias Ringwald // enable advertisements 558caefee3SMatthias Ringwald BTstack.setAdvData(sizeof(adv_data), adv_data); 568caefee3SMatthias Ringwald BTstack.startAdvertising(); 578caefee3SMatthias Ringwald} 588caefee3SMatthias Ringwald/* LISTING_END(ANCSSetup): ANCS Setup */ 598caefee3SMatthias Ringwald 608caefee3SMatthias Ringwaldvoid loop(void){ 618caefee3SMatthias Ringwald BTstack.loop(); 628caefee3SMatthias Ringwald} 638caefee3SMatthias Ringwald 648caefee3SMatthias Ringwald/* 658caefee3SMatthias Ringwald * @section ANCS Callback 668caefee3SMatthias Ringwald * @text In the ANCS Callback, connect and disconnect events are received. 678caefee3SMatthias Ringwald * For actual notifications, ancs_client_attribute_name_for_id allows to 688caefee3SMatthias Ringwald * look up the name. To get the notification body, e.g., the actual message, 698caefee3SMatthias Ringwald * the GATT Client needs to be used direclty. 708caefee3SMatthias Ringwald */ 718caefee3SMatthias Ringwald 728caefee3SMatthias Ringwald/* LISTING_START(ANCSCallback): ANCS Callback */ 738caefee3SMatthias Ringwaldvoid ancs_callback(ancs_event_t * event){ 748caefee3SMatthias Ringwald const char * attribute_name; 758caefee3SMatthias Ringwald switch (event->type){ 768caefee3SMatthias Ringwald case ANCS_CLIENT_CONNECTED: 778caefee3SMatthias Ringwald Serial.println("ANCS Client: Connected"); 788caefee3SMatthias Ringwald break; 798caefee3SMatthias Ringwald case ANCS_CLIENT_DISCONNECTED: 808caefee3SMatthias Ringwald Serial.println("ANCS Client: Disconnected"); 818caefee3SMatthias Ringwald break; 828caefee3SMatthias Ringwald case ANCS_CLIENT_NOTIFICATION: 838caefee3SMatthias Ringwald attribute_name = ancs_client_attribute_name_for_id(event->attribute_id); 848caefee3SMatthias Ringwald if (!attribute_name) break; 858caefee3SMatthias Ringwald Serial.print("Notification: "); 868caefee3SMatthias Ringwald Serial.print(attribute_name); 878caefee3SMatthias Ringwald Serial.print(" - "); 888caefee3SMatthias Ringwald Serial.println(event->text); 898caefee3SMatthias Ringwald break; 908caefee3SMatthias Ringwald default: 918caefee3SMatthias Ringwald break; 928caefee3SMatthias Ringwald } 938caefee3SMatthias Ringwald} 948caefee3SMatthias Ringwald/* LISTING_END(ANCSCallback): ANCS Callback */ 958caefee3SMatthias Ringwald 96