2015-04-19 20:52:31 +00:00
|
|
|
#include <BTstack.h>
|
|
|
|
#include <stdio.h>
|
2015-11-13 14:04:41 +00:00
|
|
|
#include "ble/att_server.h"
|
|
|
|
#include "ble/gatt_client.h"
|
2016-01-21 13:42:13 +00:00
|
|
|
#include "ancs_client.h"
|
2016-01-29 21:35:46 +00:00
|
|
|
#include "btstack_event.h"
|
2015-11-13 14:04:41 +00:00
|
|
|
#include "ble/sm.h"
|
2015-04-19 20:52:31 +00:00
|
|
|
#include <SPI.h>
|
|
|
|
|
2015-06-03 18:02:40 +00:00
|
|
|
/*
|
|
|
|
* EXAMPLE_START(ANCS): ANCS Client
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @section Advertisement
|
|
|
|
* @text An ANCS Client needs to include the ANCS UUID in its advertisement to
|
|
|
|
* get recognized by iOS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* LISTING_START(ANCSAdvertisement): ANCS Advertisement */
|
2015-04-19 20:52:31 +00:00
|
|
|
const uint8_t adv_data[] = {
|
|
|
|
// Flags general discoverable
|
|
|
|
0x02, 0x01, 0x02,
|
|
|
|
// Name
|
|
|
|
0x05, 0x09, 'A', 'N', 'C', 'S',
|
|
|
|
// Service Solicitation, 128-bit UUIDs - ANCS (little endian)
|
|
|
|
0x11,0x15,0xD0,0x00,0x2D,0x12,0x1E,0x4B,0x0F,0xA4,0x99,0x4E,0xCE,0xB5,0x31,0xF4,0x05,0x79
|
|
|
|
};
|
2015-06-03 18:02:40 +00:00
|
|
|
/* LISTING_END(ANCSAdvertisement): ANCS Advertisement */
|
2015-04-19 20:52:31 +00:00
|
|
|
|
2015-06-03 18:02:40 +00:00
|
|
|
/*
|
|
|
|
* @section Setup
|
|
|
|
*
|
|
|
|
* @text In the setup, the LE Security Manager is configured to accept pairing requests.
|
|
|
|
* Then, the ANCS Client library is initialized and and ancs_callback registered.
|
|
|
|
* Finally, the Advertisement data is set and Advertisements are started.
|
|
|
|
*/
|
2015-04-19 20:52:31 +00:00
|
|
|
|
2015-06-03 18:02:40 +00:00
|
|
|
/* LISTING_START(ANCSSetup): ANCS Setup */
|
2015-05-13 08:30:46 +00:00
|
|
|
void setup(void){
|
2015-05-21 13:54:06 +00:00
|
|
|
|
2015-05-29 18:46:52 +00:00
|
|
|
Serial.begin(9600);
|
|
|
|
Serial.println("BTstack ANCS Client starting up...");
|
2015-05-21 13:54:06 +00:00
|
|
|
|
|
|
|
// startup BTstack and configure log_info/log_error
|
2015-05-18 20:48:32 +00:00
|
|
|
BTstack.setup();
|
2015-04-19 20:52:31 +00:00
|
|
|
|
|
|
|
sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
|
|
|
|
sm_set_authentication_requirements( SM_AUTHREQ_BONDING );
|
|
|
|
|
|
|
|
// setup ANCS Client
|
|
|
|
ancs_client_init();
|
|
|
|
ancs_client_register_callback(&ancs_callback);
|
2015-05-21 13:54:06 +00:00
|
|
|
|
|
|
|
// enable advertisements
|
|
|
|
BTstack.setAdvData(sizeof(adv_data), adv_data);
|
|
|
|
BTstack.startAdvertising();
|
2015-04-19 20:52:31 +00:00
|
|
|
}
|
2015-06-03 18:02:40 +00:00
|
|
|
/* LISTING_END(ANCSSetup): ANCS Setup */
|
2015-04-19 20:52:31 +00:00
|
|
|
|
2015-05-13 08:30:46 +00:00
|
|
|
void loop(void){
|
2015-05-18 20:48:32 +00:00
|
|
|
BTstack.loop();
|
2015-04-19 20:52:31 +00:00
|
|
|
}
|
2015-06-03 18:02:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @section ANCS Callback
|
|
|
|
* @text In the ANCS Callback, connect and disconnect events are received.
|
|
|
|
* For actual notifications, ancs_client_attribute_name_for_id allows to
|
|
|
|
* look up the name. To get the notification body, e.g., the actual message,
|
|
|
|
* the GATT Client needs to be used direclty.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 21:35:46 +00:00
|
|
|
|
2015-06-03 18:02:40 +00:00
|
|
|
/* LISTING_START(ANCSCallback): ANCS Callback */
|
2016-02-19 13:05:39 +00:00
|
|
|
void ancs_callback(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
2015-06-03 18:02:40 +00:00
|
|
|
const char * attribute_name;
|
2016-02-19 13:52:36 +00:00
|
|
|
if (hci_event_packet_get_type(packet) != HCI_EVENT_ANCS_META) return;
|
|
|
|
switch (hci_event_ancs_meta_get_subevent_code()]){
|
2016-02-18 19:59:42 +00:00
|
|
|
case ANCS_SUBEVENT_CLIENT_CONNECTED:
|
2015-06-03 18:02:40 +00:00
|
|
|
Serial.println("ANCS Client: Connected");
|
|
|
|
break;
|
2016-02-18 19:59:42 +00:00
|
|
|
case ANCS_SUBEVENT_CLIENT_DISCONNECTED:
|
2015-06-03 18:02:40 +00:00
|
|
|
Serial.println("ANCS Client: Disconnected");
|
|
|
|
break;
|
2016-02-18 19:59:42 +00:00
|
|
|
case ANCS_SUBEVENT_CLIENT_NOTIFICATION:
|
|
|
|
attribute_name = ancs_client_attribute_name_for_id(ancs_subevent_client_notification_get_attribute_id(packet));
|
2015-06-03 18:02:40 +00:00
|
|
|
if (!attribute_name) break;
|
|
|
|
Serial.print("Notification: ");
|
|
|
|
Serial.print(attribute_name);
|
|
|
|
Serial.print(" - ");
|
2016-02-18 19:59:42 +00:00
|
|
|
Serial.println(ancs_subevent_client_notification_get_text(packet));
|
2015-06-03 18:02:40 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* LISTING_END(ANCSCallback): ANCS Callback */
|
|
|
|
|