23
23
BLEClientDis bleClientDis;
24
24
BLEAncs bleancs;
25
25
26
- char buffer[128 ];
26
+ #define BUFSIZE 128
27
+ char buffer[BUFSIZE];
27
28
28
29
// Check BLEAncs.h for AncsNotification_t
29
30
const char * EVENT_STR[] = { " Added" , " Modified" , " Removed" };
@@ -37,7 +38,7 @@ const char* CAT_STR [] =
37
38
void setup ()
38
39
{
39
40
Serial.begin (115200 );
40
- while ( !Serial ) delay (10 ); // for nrf52840 with native usb
41
+ // while ( !Serial ) delay(10); // for nrf52840 with native usb
41
42
42
43
Serial.println (" Bluefruit52 BLE ANCS Example" );
43
44
Serial.println (" ----------------------------\n " );
@@ -52,10 +53,12 @@ void setup()
52
53
53
54
Bluefruit.begin ();
54
55
Bluefruit.setTxPower (4 ); // Check bluefruit.h for supported values
55
- Bluefruit.setName (" Bluefruit52" );
56
56
Bluefruit.Periph .setConnectCallback (connect_callback);
57
57
Bluefruit.Periph .setDisconnectCallback (disconnect_callback);
58
58
59
+ // Set connection secured callback, invoked when connection is encrypted
60
+ Bluefruit.Pairing .setSecuredCallback (connection_secured_callback);
61
+
59
62
// Configure DIS client
60
63
bleClientDis.begin ();
61
64
@@ -108,6 +111,8 @@ void loop()
108
111
109
112
void connect_callback (uint16_t conn_handle)
110
113
{
114
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
115
+
111
116
Serial.println (" Connected" );
112
117
113
118
Serial.print (" Discovering DIS ... " );
@@ -116,16 +121,16 @@ void connect_callback(uint16_t conn_handle)
116
121
Serial.println (" Discovered" );
117
122
118
123
// 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 ) )
121
126
{
122
127
Serial.print (" Manufacturer: " );
123
128
Serial.println (buffer);
124
129
}
125
130
126
131
// 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 ) )
129
134
{
130
135
Serial.print (" Model: " );
131
136
Serial.println (buffer);
@@ -141,54 +146,77 @@ void connect_callback(uint16_t conn_handle)
141
146
142
147
// ANCS requires pairing to work, it makes sense to request security here as well
143
148
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 () )
145
170
{
146
- Serial.println (" Done" );
147
171
Serial.println (" Enabling notifications" );
148
172
Serial.println ();
149
173
bleancs.enableNotification ();
150
-
151
- Serial.println (" | Event | Category (count) | Title | Message | App ID | App Name |" );
152
- Serial.println (" ---------------------------------------------------------------------------------------------------------------" );
153
174
}
154
175
}
155
176
}
156
177
157
178
void ancs_notification_callback (AncsNotification_t* notif)
158
179
{
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));
161
185
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 );
166
190
167
191
// 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
+ }
175
213
214
+ Serial.printf (" %-15s %s\n " , buffer, EVENT_STR[notif->eventID ]);
215
+
176
216
// 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);
192
220
193
221
Serial.println ();
194
222
0 commit comments