forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathancs.ino
More file actions
159 lines (126 loc) · 5.91 KB
/
ancs.ino
File metadata and controls
159 lines (126 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Import libraries (BLEPeripheralObserver depends on SPI)
#include <SPI.h>
#include <BLEPeripheralObserver.h>
#include <BLEUtil.h>
//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST)
BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver();
BLEBondStore bleBondStore;
// remote services
BLERemoteService ancsService = BLERemoteService("7905f431b5ce4e99a40f4b1e122d00d0");
// remote characteristics
BLERemoteCharacteristic ancsNotificationSourceCharacteristic = BLERemoteCharacteristic("9fbf120d630142d98c5825e699a21dbd", BLENotify);
//BLERemoteCharacteristic ancsControlPointCharacteristic = BLERemoteCharacteristic("69d1d8f345e149a898219bbdfdaad9d9", BLEWrite);
//BLERemoteCharacteristic ancsDataSourceCharacteristic = BLERemoteCharacteristic("22eac6e924d64bb5be44b36ace7c7bfb", BLENotify);
void setup() {
Serial.begin(9600);
#if defined (__AVR_ATmega32U4__)
while(!Serial);
#endif
// clears bond data on every boot
bleBondStore.clearData();
blePeriphObserv.setBondStore(bleBondStore);
blePeriphObserv.setServiceSolicitationUuid(ancsService.uuid());
blePeriphObserv.setLocalName("ANCS");
// set device name and appearance
blePeriphObserv.setDeviceName("Arduino ANCS");
blePeriphObserv.setAppearance(0x0080);
blePeriphObserv.addRemoteAttribute(ancsService);
blePeriphObserv.addRemoteAttribute(ancsNotificationSourceCharacteristic);
// blePeriphObserv.addRemoteAttribute(ancsControlPointCharacteristic);
// blePeriphObserv.addRemoteAttribute(ancsDataSourceCharacteristic);
// assign event handlers for connected, disconnected to peripheral
blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
blePeriphObserv.setEventHandler(BLEBonded, blePeripheralBondedHandler);
blePeriphObserv.setEventHandler(BLERemoteServicesDiscovered, blePeripheralRemoteServicesDiscoveredHandler);
// assign event handlers for characteristic
ancsNotificationSourceCharacteristic.setEventHandler(BLEValueUpdated, ancsNotificationSourceCharacteristicValueUpdated);
// ancsDataSourceCharacteristic.setEventHandler(BLEValueUpdated, ancsDataSourceCharacteristicCharacteristicValueUpdated);
// begin initialization
blePeriphObserv.begin();
Serial.println(F("BLE Peripheral - ANCS"));
}
void loop() {
blePeriphObserv.poll();
}
void blePeripheralConnectHandler(BLECentral& central) {
// central connected event handler
Serial.print(F("Connected event, central: "));
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLECentral& central) {
// central disconnected event handler
Serial.print(F("Disconnected event, central: "));
Serial.println(central.address());
}
void blePeripheralBondedHandler(BLECentral& central) {
// central bonded event handler
Serial.print(F("Remote bonded event, central: "));
Serial.println(central.address());
if (ancsNotificationSourceCharacteristic.canSubscribe()) {
ancsNotificationSourceCharacteristic.subscribe();
}
}
void blePeripheralRemoteServicesDiscoveredHandler(BLECentral& central) {
// central remote services discovered event handler
Serial.print(F("Remote services discovered event, central: "));
Serial.println(central.address());
if (ancsNotificationSourceCharacteristic.canSubscribe()) {
ancsNotificationSourceCharacteristic.subscribe();
}
}
enum AncsNotificationEventId {
AncsNotificationEventIdAdded = 0,
AncsNotificationEventIdModified = 1,
AncsNotificationEventIdRemoved = 2
};
enum AncsNotificationEventFlags {
AncsNotificationEventFlagsSilent = 1,
AncsNotificationEventFlagsImportant = 2,
AncsNotificationEventFlagsPositiveAction = 4,
AncsNotificationEventFlagsNegativeAction = 8
};
enum AncsNotificationCategoryId {
AncsNotificationCategoryIdOther = 0,
AncsNotificationCategoryIdIncomingCall = 1,
AncsNotificationCategoryIdMissedCall = 2,
AncsNotificationCategoryIdVoicemail = 3,
AncsNotificationCategoryIdSocial = 4,
AncsNotificationCategoryIdSchedule = 5,
AncsNotificationCategoryIdEmail = 6,
AncsNotificationCategoryIdNews = 7,
AncsNotificationCategoryIdHealthAndFitness = 8,
AncsNotificationCategoryIdBusinessAndFinance = 9,
AncsNotificationCategoryIdLocation = 10,
AncsNotificationCategoryIdEntertainment = 11
};
struct AncsNotification {
unsigned char eventId;
unsigned char eventFlags;
unsigned char catergoryId;
unsigned char catergoryCount;
unsigned long notificationUid;
};
void ancsNotificationSourceCharacteristicValueUpdated(BLECentral& central, BLERemoteCharacteristic& characteristic) {
Serial.println(F("ANCS Notification Source Value Updated:"));
struct AncsNotification notification;
memcpy(¬ification, characteristic.value(), sizeof(notification));
Serial.print("\tEvent ID: ");
Serial.println(notification.eventId);
Serial.print("\tEvent Flags: 0x");
Serial.println(notification.eventFlags, HEX);
Serial.print("\tCategory ID: ");
Serial.println(notification.catergoryId);
Serial.print("\tCategory Count: ");
Serial.println(notification.catergoryCount);
Serial.print("\tNotification UID: ");
Serial.println(notification.notificationUid);
// BLEUtil::printBuffer(characteristic.value(), characteristic.valueLength());
}
//void ancsDataSourceCharacteristicCharacteristicValueUpdated(BLECentral& central, BLERemoteCharacteristic& characteristic) {
// Serial.print(F("ANCS Data Source Value Updated: "));
//
// BLEUtil::printBuffer(characteristic.value(), characteristic.valueLength());
//}