Skip to content

Commit b5f2e74

Browse files
committed
clean up ancs example
1 parent f92ed91 commit b5f2e74

File tree

2 files changed

+69
-41
lines changed

2 files changed

+69
-41
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
BLEClientDis bleClientDis;
2424
BLEAncs bleancs;
2525

26-
char buffer[128];
26+
#define BUFSIZE 128
27+
char buffer[BUFSIZE];
2728

2829
// Check BLEAncs.h for AncsNotification_t
2930
const char* EVENT_STR[] = { "Added", "Modified", "Removed" };
@@ -37,7 +38,7 @@ const char* CAT_STR [] =
3738
void setup()
3839
{
3940
Serial.begin(115200);
40-
while ( !Serial ) delay(10); // for nrf52840 with native usb
41+
// while ( !Serial ) delay(10); // for nrf52840 with native usb
4142

4243
Serial.println("Bluefruit52 BLE ANCS Example");
4344
Serial.println("----------------------------\n");
@@ -52,10 +53,12 @@ void setup()
5253

5354
Bluefruit.begin();
5455
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
55-
Bluefruit.setName("Bluefruit52");
5656
Bluefruit.Periph.setConnectCallback(connect_callback);
5757
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
5858

59+
// Set connection secured callback, invoked when connection is encrypted
60+
Bluefruit.Pairing.setSecuredCallback(connection_secured_callback);
61+
5962
// Configure DIS client
6063
bleClientDis.begin();
6164

@@ -108,6 +111,8 @@ void loop()
108111

109112
void connect_callback(uint16_t conn_handle)
110113
{
114+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
115+
111116
Serial.println("Connected");
112117

113118
Serial.print("Discovering DIS ... ");
@@ -116,16 +121,16 @@ void connect_callback(uint16_t conn_handle)
116121
Serial.println("Discovered");
117122

118123
// Read and print Manufacturer string
119-
memset(buffer, 0, sizeof(buffer));
120-
if ( bleClientDis.getManufacturer(buffer, sizeof(buffer)) )
124+
memset(buffer, 0, BUFSIZE);
125+
if ( bleClientDis.getManufacturer(buffer, BUFSIZE) )
121126
{
122127
Serial.print("Manufacturer: ");
123128
Serial.println(buffer);
124129
}
125130

126131
// Read and print Model Number string
127-
memset(buffer, 0, sizeof(buffer));
128-
if ( bleClientDis.getModel(buffer, sizeof(buffer)) )
132+
memset(buffer, 0, BUFSIZE);
133+
if ( bleClientDis.getModel(buffer, BUFSIZE) )
129134
{
130135
Serial.print("Model: ");
131136
Serial.println(buffer);
@@ -141,54 +146,77 @@ void connect_callback(uint16_t conn_handle)
141146

142147
// ANCS requires pairing to work, it makes sense to request security here as well
143148
Serial.print("Attempting to PAIR with the iOS device, please press PAIR on your phone ... ");
144-
if ( Bluefruit.requestPairing(conn_handle) )
149+
conn->requestPairing();
150+
}
151+
}
152+
153+
void connection_secured_callback(uint16_t conn_handle)
154+
{
155+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
156+
157+
if ( !conn->secured() )
158+
{
159+
// It is possible that connection is still not secured by this time.
160+
// This happens (central only) when we try to encrypt connection using stored bond keys
161+
// but peer reject it (probably it remove its stored key).
162+
// Therefore we will request an pairing again --> callback again when encrypted
163+
conn->requestPairing();
164+
}
165+
else
166+
{
167+
Serial.println("Secured");
168+
169+
if ( bleancs.discovered() )
145170
{
146-
Serial.println("Done");
147171
Serial.println("Enabling notifications");
148172
Serial.println();
149173
bleancs.enableNotification();
150-
151-
Serial.println("| Event | Category (count) | Title | Message | App ID | App Name |");
152-
Serial.println("---------------------------------------------------------------------------------------------------------------");
153174
}
154175
}
155176
}
156177

157178
void ancs_notification_callback(AncsNotification_t* notif)
158179
{
159-
int n;
160-
Serial.printf("| %-8s | ", EVENT_STR[notif->eventID]);
180+
uint32_t const uid = notif->uid;
181+
182+
// Application ID & Name
183+
char appID[128] = { 0 };
184+
bleancs.getAppID(uid, appID, sizeof(appID));
161185

162-
// Print Category with padding
163-
n = Serial.printf("%s (%d)", CAT_STR[notif->categoryID], notif->categoryCount);
164-
for (int i=n; i<20; i++) Serial.print(' ');
165-
Serial.print(" | ");
186+
memset(buffer, 0, BUFSIZE);
187+
bleancs.getAppAttribute(appID, ANCS_APP_ATTR_DISPLAY_NAME, buffer, BUFSIZE);
188+
189+
Serial.printf("%-15s (%s)\n", buffer, appID);
166190

167191
// Get notification Title
168-
// iDevice often includes Unicode "Bidirection Text Control" in the Title.
169-
// Most strings have U+202D at the beginning and U+202C at the end. You may
170-
// want to remove them.
171-
// U+202D is E2-80-AD, U+202C is E2-80-AC in UTF-8
172-
memset(buffer, 0, sizeof(buffer));
173-
bleancs.getAttribute(notif->uid, ANCS_ATTR_TITLE, buffer, sizeof(buffer));
174-
Serial.printf("%-14s | ", buffer);
192+
// iDevice often include Unicode "Bidirection Text Control" in the Title.
193+
// Mostly are U+202D as beginning and U+202C as ending. Let's remove them
194+
memset(buffer, 0, BUFSIZE);
195+
if ( bleancs.getTitle(uid, buffer, BUFSIZE) )
196+
{
197+
char u202D[3] = { 0xE2, 0x80, 0xAD }; // U+202D in UTF-8
198+
char u202C[3] = { 0xE2, 0x80, 0xAC }; // U+202C in UTF-8
199+
200+
int len = strlen(buffer);
201+
202+
if ( 0 == memcmp(&buffer[len-3], u202C, 3) )
203+
{
204+
len -= 3;
205+
buffer[len] = 0; // chop ending U+202C
206+
}
207+
208+
if ( 0 == memcmp(buffer, u202D, 3) )
209+
{
210+
memmove(buffer, buffer+3, len-2); // move null-terminator as well
211+
}
212+
}
175213

214+
Serial.printf("%-15s %s\n", buffer, EVENT_STR[notif->eventID]);
215+
176216
// Get notification Message
177-
memset(buffer, 0, sizeof(buffer));
178-
bleancs.getAttribute(notif->uid, ANCS_ATTR_MESSAGE, buffer, sizeof(buffer));
179-
Serial.printf("%-15s | ", buffer);
180-
181-
// Get App ID and store in the app_id variable
182-
char app_id[64] = { 0 };
183-
memset(buffer, 0, sizeof(buffer));
184-
bleancs.getAttribute(notif->uid, ANCS_ATTR_APP_IDENTIFIER, buffer, sizeof(buffer));
185-
strcpy(app_id, buffer);
186-
Serial.printf("%-20s | ", app_id);
187-
188-
// Get Application Name
189-
memset(buffer, 0, sizeof(buffer));
190-
bleancs.getAppAttribute(app_id, ANCS_APP_ATTR_DISPLAY_NAME, buffer, sizeof(buffer));
191-
Serial.printf("%-15s | ", buffer);
217+
memset(buffer, 0, BUFSIZE);
218+
bleancs.getMessage(uid, buffer, BUFSIZE);
219+
Serial.printf(" %s\n", buffer);
192220

193221
Serial.println();
194222

libraries/Bluefruit52Lib/examples/Peripheral/ancs_arcada/ancs_arcada.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ void ancs_notification_callback(AncsNotification_t* notif)
320320

321321
// iDevice often include Unicode "Bidirection Text Control" in the Title.
322322
// Mostly are U+202D as beginning and U+202C as ending. Let's remove them
323-
if ( bleancs.getTitle (uid, myNtf->title , BUFSIZE) )
323+
if ( bleancs.getTitle(uid, myNtf->title, BUFSIZE) )
324324
{
325325
char u202D[3] = { 0xE2, 0x80, 0xAD }; // U+202D in UTF-8
326326
char u202C[3] = { 0xE2, 0x80, 0xAC }; // U+202C in UTF-8

0 commit comments

Comments
 (0)