@@ -72,6 +72,8 @@ static esp_bt_pin_code_t _pin_code;
7272static int _pin_len;
7373static bool _isPinSet;
7474static bool _enableSSP;
75+ static esp_spp_sec_t _sec_mask;
76+ static esp_spp_role_t _role;
7577// start connect on ESP_SPP_DISCOVERY_COMP_EVT or save entry for getChannels
7678static bool _doConnect;
7779static std::map <int , std::string> sdpRecords;
@@ -359,8 +361,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
359361 param->disc_comp .scn [0 ]);
360362#endif
361363 xEventGroupClearBits (_spp_event_group, SPP_CLOSED);
362- // if(esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, param->disc_comp.scn[0], _peer_bd_addr) ) {
363- if (esp_spp_connect (ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE, /* ESP_SPP_ROLE_SLAVE */ ESP_SPP_ROLE_MASTER, param->disc_comp .scn [0 ], _peer_bd_addr) != ESP_OK) {
364+ if (esp_spp_connect (_sec_mask, _role, param->disc_comp .scn [0 ], _peer_bd_addr) != ESP_OK) {
364365 log_e (" ESP_SPP_DISCOVERY_COMP_EVT connect failed" );
365366 xEventGroupSetBits (_spp_event_group, SPP_CLOSED);
366367 }
@@ -928,6 +929,8 @@ bool BluetoothSerial::connect(String remoteName)
928929 disconnect ();
929930 _doConnect = true ;
930931 _isRemoteAddressSet = false ;
932+ _sec_mask = ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE;
933+ _role = ESP_SPP_ROLE_MASTER;
931934 strncpy (_remote_name, remoteName.c_str (), ESP_BT_GAP_MAX_BDNAME_LEN);
932935 _remote_name[ESP_BT_GAP_MAX_BDNAME_LEN] = 0 ;
933936 log_i (" master : remoteName" );
@@ -946,8 +949,14 @@ bool BluetoothSerial::connect(String remoteName)
946949
947950/* *
948951 * @Param channel: specify channel or 0 for auto-detect
952+ * @Param sec_mask:
953+ * ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE
954+ * ESP_SPP_SEC_NONE
955+ * @Param role:
956+ * ESP_SPP_ROLE_MASTER master can handle up to 7 connections to slaves
957+ * ESP_SPP_ROLE_SLAVE can only have one connection to a master
949958 */
950- bool BluetoothSerial::connect (uint8_t remoteAddress[], int channel)
959+ bool BluetoothSerial::connect (uint8_t remoteAddress[], int channel, esp_spp_sec_t sec_mask, esp_spp_role_t role )
951960{
952961 if (!isReady (true , READY_TIMEOUT)) return false ;
953962 if (!remoteAddress) {
@@ -958,6 +967,8 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel)
958967 _doConnect = true ;
959968 _remote_name[0 ] = 0 ;
960969 _isRemoteAddressSet = true ;
970+ _sec_mask = sec_mask;
971+ _role = role;
961972 memcpy (_peer_bd_addr, remoteAddress, ESP_BD_ADDR_LEN);
962973 log_i (" master : remoteAddress" );
963974 xEventGroupClearBits (_spp_event_group, SPP_CLOSED);
@@ -966,18 +977,15 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel)
966977 log_i (" spp connect to remote %s channel %d" ,
967978 bda2str (_peer_bd_addr, bda_str, sizeof (bda_str)),
968979 channel);
969- #warning todo: set master/slave
970- // return esp_spp_connect(ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE, /* ESP_SPP_ROLE_SLAVE */ ESP_SPP_ROLE_MASTER, channel, _peer_bd_addr) == ESP_OK;
971- // if(esp_spp_connect(ESP_SPP_SEC_NONE, /* ESP_SPP_ROLE_SLAVE */ ESP_SPP_ROLE_MASTER, channel, _peer_bd_addr) != ESP_OK ) {
972- if (esp_spp_connect (ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE, /* ESP_SPP_ROLE_SLAVE */ ESP_SPP_ROLE_MASTER, channel, _peer_bd_addr) != ESP_OK ) {
980+ if (esp_spp_connect (sec_mask, role, channel, _peer_bd_addr) != ESP_OK ) {
973981 log_e (" spp connect failed" );
974982 return false ;
975983 }
976984 bool rc=waitForConnect (READY_TIMEOUT);
977985 if (rc) {
978986 log_i (" connected" );
979987 } else {
980- if (this . isClosed ()) {
988+ if (this -> isClosed ()) {
981989 log_e (" connect failed" );
982990 } else {
983991 log_e (" connect timed out after %dms" , READY_TIMEOUT);
0 commit comments