Skip to content

Commit b346290

Browse files
author
Victor Tchistiak
committed
20190916 - connect would use resolved address as preference and remove now redundant _remote_address
1 parent 9eb5e3a commit b346290

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ BluetoothSerial SerialBT;
1414

1515
String MACadd = "AA:BB:CC:11:22:33";
1616
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
17+
//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
1718
String name = "OBDII";
1819
char *pin = "1234"; //<- standard pin would be provided by default
1920

@@ -25,7 +26,11 @@ void setup() {
2526
Serial.println("The device started in master mode, make sure remote BT device is on!");
2627
//delay(2000);
2728
SerialBT.connect(address);
28-
//SerialBT.connect(name);
29+
//SerialBT.connect(name); //slow as it needs to resolve name to address first, but allows to connect to different devices with the same name
30+
while(!SerialBT.connected()) {delay(1000); }
31+
SerialBT.disconnect();
32+
while(SerialBT.connected()) {delay(1000); }
33+
SerialBT.connect();
2934
}
3035

3136
void loop() {

libraries/BluetoothSerial/src/BluetoothSerial.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,8 @@ bool BluetoothSerial::connect(String remoteName)
669669
strncpy(_remote_name, remoteName.c_str(), ESP_BT_GAP_MAX_BDNAME_LEN);
670670
_remote_name[ESP_BT_GAP_MAX_BDNAME_LEN] = 0;
671671
log_i("master : remoteName");
672-
return (esp_bt_gap_start_discovery(inq_mode, inq_len, inq_num_rsps) == ESP_OK);
672+
// will first resolve name to address
673+
return (esp_bt_gap_start_discovery(inq_mode, inq_len, inq_num_rsps) == ESP_OK);
673674
}
674675

675676
bool BluetoothSerial::connect(uint8_t remoteAddress[])
@@ -681,30 +682,32 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[])
681682
}
682683
_remote_name[0] = 0;
683684
_isRemoteAddressSet = true;
684-
memcpy(_remote_address, remoteAddress, ESP_BD_ADDR_LEN);
685-
memcpy(_peer_bd_addr, _remote_address, ESP_BD_ADDR_LEN);
685+
memcpy(_peer_bd_addr, remoteAddress, ESP_BD_ADDR_LEN);
686686
log_i("master : remoteAddress");
687687
return ( esp_spp_start_discovery(_peer_bd_addr) == ESP_OK);
688688
}
689689

690690
bool BluetoothSerial::connect()
691691
{
692692
if (!isReady(true)) return false;
693-
if (_remote_name[0]) {
694-
log_i("master : remoteName");
695-
return (esp_bt_gap_start_discovery(inq_mode, inq_len, inq_num_rsps) == ESP_OK);
696-
} else if (_isRemoteAddressSet){
693+
if (_isRemoteAddressSet){
694+
// use resolved or set address first
697695
log_i("master : remoteAddress");
698-
memcpy(_peer_bd_addr, _remote_address, ESP_BD_ADDR_LEN);
699696
return (esp_spp_start_discovery(_peer_bd_addr) == ESP_OK);
700-
} else {
697+
} else if (_remote_name[0]) {
698+
log_i("master : remoteName");
699+
// will resolve name to address first - it may take a while
700+
return (esp_bt_gap_start_discovery(inq_mode, inq_len, inq_num_rsps) == ESP_OK);
701+
} else {
701702
log_e("Neither Remote name nor address was provided");
702703
}
703704
return false;
704705
}
705706

706707
bool BluetoothSerial::disconnect() {
707708
if (_spp_client) {
709+
flush();
710+
log_i("disconnecting");
708711
return (esp_spp_disconnect(_spp_client) == ESP_OK);
709712
}
710713
return false;

0 commit comments

Comments
 (0)