Page 1 of 1

Closing a Bluetooth connection that hasn't been used.

Posted: Tue Dec 24, 2019 3:29 pm
by xgarbxgarb
I'm using serial Bluetooth so a user can set Wi-Fi details and read the IP address from a phone. I want to kill the Bluetooth connection when either:

a) The details have been entered correctly (via Bluetooth)
b) The device restarts and the user has viewed the IP address on their phone (via Bluetooth)
c) The device restarts and the user enters a previously known IP address in their browser

I'm using the following code to kill the connection (Not sure every command is necessary but I couldn't find documentation for this):

Code: Select all

  Serial.println("BT stopping");
  SerialBT.flush();  
  SerialBT.disconnect();
  SerialBT.end();
  Serial.println("BT stopped");
In cases a and b I get this in the serial monitor:
BT stopping
[V][BluetoothSerial.cpp:271] esp_spp_cb(): ESP_SPP_WRITE_EVT: 16 FREE
[V][BluetoothSerial.cpp:271] esp_spp_cb(): ESP_SPP_WRITE_EVT: 2 FREE
[BluetoothSerial.cpp:757] disconnect(): disconnecting
[BluetoothSerial.cpp:247] esp_spp_cb(): ESP_SPP_CLOSE_EVT
BT stopped


In the case c I get this:

BT stopping
BT stopped


The difference is that in a and b a callback has been called. For example b where the user has connected and has been sent the IP address looks like this:

Code: Select all

void callback_show_ip(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
  if (event == ESP_SPP_SRV_OPEN_EVT) {
    SerialBT.println("Device IP: ");
    SerialBT.println(WiFi.localIP());
  }
}

So... the question is. Why don't I see the 'ESP_SPP_CLOSE_EVT' message when I close a Bluetooth connection that hasn't been used?

Re: Closing a Bluetooth connection that hasn't been used.

Posted: Wed Dec 25, 2019 1:29 am
by chegewara
CLOSE event means close previously opened connection with peer device, not close bluetooth.

Re: Closing a Bluetooth connection that hasn't been used.

Posted: Fri Jan 31, 2020 5:53 pm
by xgarbxgarb
Thanks for the information. I've finished the project and put the code here: https://github.com/robotzero1/esp32-bluewifi if anyone is interested