38
38
39
39
void BLECharacteristic::init (void )
40
40
{
41
- _is_temp = false ;
41
+ _is_temp = false ;
42
+
42
43
varclr (&_properties);
43
44
_descriptor = NULL ;
44
- _max_len = BLE_GATTS_VAR_ATTR_LEN_MAX;
45
+ _max_len = BLE_GATTS_VAR_ATTR_LEN_MAX;
45
46
46
47
varclr (&_report_ref_desc);
47
48
@@ -60,7 +61,8 @@ void BLECharacteristic::init(void)
60
61
61
62
_rd_authorize_cb = NULL ;
62
63
_wr_authorize_cb = NULL ;
63
- _wr_cb = NULL ;
64
+ _wr_cb = NULL ;
65
+ _cccd_wr_cb = NULL ;
64
66
}
65
67
66
68
BLECharacteristic::BLECharacteristic (void )
@@ -136,6 +138,11 @@ void BLECharacteristic::setWriteCallback(write_cb_t fp)
136
138
_wr_cb = fp;
137
139
}
138
140
141
+ void BLECharacteristic::setCccdWriteCallback (write_cb_t fp)
142
+ {
143
+ _cccd_wr_cb = fp;
144
+ }
145
+
139
146
void BLECharacteristic::setReadAuthorizeCallback (read_authorize_cb_t fp)
140
147
{
141
148
_attr_meta.rd_auth = (fp ? 1 : 0 );
@@ -274,8 +281,10 @@ err_t BLECharacteristic::start(void)
274
281
275
282
// Currently Only register to Bluefruit when having callback support
276
283
// And The Characteristic must not be temporary memory aka local variable
284
+ // Or Properties is Notify and/or Indicate for saving CCCD for bonded connection
277
285
if ( !_is_temp &&
278
- (_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _properties.notify || _properties.indicate ) )
286
+ (_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb ||
287
+ _properties.notify || _properties.indicate ) )
279
288
{
280
289
(void ) Bluefruit._registerCharacteristic (this );
281
290
}
@@ -318,9 +327,16 @@ void BLECharacteristic::eventHandler(ble_evt_t* event)
318
327
{
319
328
ble_gatts_evt_write_t * request = (ble_gatts_evt_write_t *) &event->evt .gatts_evt .params .write ;
320
329
321
- if ( _wr_cb && (uuid == request->uuid ))
330
+ // Value write
331
+ if ( _wr_cb && (request->handle == _handles.value_handle ))
332
+ {
333
+ _wr_cb (*this , request);
334
+ }
335
+
336
+ // CCCD write
337
+ if ( _cccd_wr_cb && (request->handle == _handles.cccd_handle ) )
322
338
{
323
- _wr_cb (*this , &event-> evt . gatts_evt . params . write );
339
+ _cccd_wr_cb (*this , request );
324
340
}
325
341
}
326
342
break ;
0 commit comments