@@ -115,9 +115,16 @@ static bool get_name_from_eir(uint8_t *eir, char *bdname, uint8_t *bdname_len)
115
115
}
116
116
117
117
static bool btSetPin () {
118
+ esp_bt_pin_type_t pin_type;
118
119
if (_isPinSet) {
119
- log_i (" pin set" );
120
- esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
120
+ if (_pin_len) {
121
+ log_i (" pin set" );
122
+ pin_type = ESP_BT_PIN_TYPE_FIXED;
123
+ } else {
124
+ _isPinSet = false ;
125
+ log_i (" pin reset" );
126
+ pin_type = ESP_BT_PIN_TYPE_VARIABLE; // pin_code would be ignored (default)
127
+ }
121
128
return (esp_bt_gap_set_pin (pin_type, _pin_len, _pin_code) == ESP_OK);
122
129
}
123
130
return false ;
@@ -294,13 +301,13 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
294
301
case ESP_SPP_OPEN_EVT:// Client connection open
295
302
if (!_spp_client){
296
303
_spp_client = param->open .handle ;
297
- } else {
298
- secondConnectionAttempt = true ;
299
- esp_spp_disconnect (param->open .handle );
300
- }
301
- xEventGroupSetBits (_spp_event_group, SPP_CONNECTED);
302
- log_i (" ESP_SPP_OPEN_EVT" );
303
- break ;
304
+ } else {
305
+ secondConnectionAttempt = true ;
306
+ esp_spp_disconnect (param->open .handle );
307
+ }
308
+ xEventGroupSetBits (_spp_event_group, SPP_CONNECTED);
309
+ log_i (" ESP_SPP_OPEN_EVT" );
310
+ break ;
304
311
case ESP_SPP_START_EVT:// server started
305
312
log_i (" ESP_SPP_START_EVT" );
306
313
break ;
@@ -332,8 +339,9 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
332
339
esp_bt_gap_cancel_discovery ();
333
340
}
334
341
} else if (param->disc_res .prop [i].type == ESP_BT_GAP_DEV_PROP_BDNAME) {
335
- strcpy (peer_bdname, (char *)param->disc_res .prop [i].val );
336
- peer_bdname_len = strlen (peer_bdname);
342
+ peer_bdname_len = param->disc_res .prop [i].len ;
343
+ memcpy (peer_bdname, param->disc_res .prop [i].val , peer_bdname_len);
344
+ peer_bdname[peer_bdname_len] = ' \0 ' ;
337
345
log_v (" ESP_BT_GAP_DISC_RES_EVT : BDNAME : %s" , peer_bdname);
338
346
if (strlen (_remote_name) == peer_bdname_len
339
347
&& strncmp (peer_bdname, _remote_name, peer_bdname_len) == 0 ) {
@@ -369,7 +377,8 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
369
377
log_i (" ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d" , param->pin_req .min_16_digit );
370
378
if (param->pin_req .min_16_digit ) {
371
379
log_i (" Input pin code: 0000 0000 0000 0000" );
372
- esp_bt_pin_code_t pin_code = {0 };
380
+ esp_bt_pin_code_t pin_code;
381
+ memset (pin_code, ' 0' , ESP_BT_PIN_CODE_LEN);
373
382
esp_bt_gap_pin_reply (param->pin_req .bda , true , 16 , pin_code);
374
383
} else {
375
384
log_i (" Input pin code: 1234" );
@@ -637,19 +646,15 @@ void BluetoothSerial::enableSSP() {
637
646
* Set default parameters for Legacy Pairing
638
647
* Use fixed pin code
639
648
*/
640
- bool BluetoothSerial::setPin (const char * pin) {
641
- if (pin && *pin) {
642
- int i = 0 ;
643
- while (*(pin + i) && i < ESP_BT_PIN_CODE_LEN) {
644
- _pin_code[i] = *(pin+i);
645
- i++;
646
- }
647
- _pin_len = i;
648
- } else if (pin){
649
- _pin_len = 0 ; // resetting pin to none
649
+ bool BluetoothSerial::setPin (const char *pin) {
650
+ bool isEmpty = !(pin && *pin);
651
+ if (isEmpty && !_isPinSet) {
652
+ return true ; // nothing to do
653
+ } else if (!isEmpty){
654
+ _pin_len = strlen (pin);
655
+ memcpy (_pin_code, pin, _pin_len);
650
656
} else {
651
- log_e (" No pin is provided" );
652
- return false ;
657
+ _pin_len = 0 ; // resetting pin to none (default)
653
658
}
654
659
_isPinSet = true ;
655
660
if (isReady (false )) {
0 commit comments