- Making ESP32 WiFi/Bluetooth work together
- Connect bluetooth and wifi
- When to use Bluetooth tethering?
- When your phone does not support Wi-Fi sharing over Wi-Fi hotspot
- When you want to conserve battery
- How to share Wi-Fi over Bluetooth
- Step 1: Turn on your phone’s Bluetooth
- Step 2: Turn on Bluetooth tethering on your phone
- Step 3: Pair your device via Bluetooth and receive connectivity
- How to make wifi and bluetooth work simultaneously?
Making ESP32 WiFi/Bluetooth work together
I’m creating a App that involves Bluetooth and WiFi. I firstly connect the Phone to the Bluetooth and pass the WiFi ssid and password through it. Then after receiving the ssid and the password I attempt to connect it to the WiFi. I manage to store the ssid and password into a char array.
After sending the data from Bluetooth, and the ESP32 making the connection with the WiFi, the Bluetooth gets disconnected. I can’t connect the Bluetooth again because WiFi is connected, since I control certain statements from the code via Bluetooth.read(), I will need to allow the connection to coexist between WiFi and Bluetooth.
The project consist on SPIFFS (Flash memory saving). The WiFi ssid and password are passed from Bluetooth firstly, then, I make the connection from the Bluetooth data received into a char array, so I can connect it to the internet later on.
I thought firstly on adding a callback that alerts when the device is attempting to connect, but couldn’t find in the documentation any code that was able to do so. I have a callback function that tells me when the device connects and disconnects.
I searched up and found something about Menuconfig but I couldn’t find where it was located.
This is what I was reading from: https://www.espressif.com/sites/default/files/documentation/ESP32_FAQs__EN.pdf
5.3.2. How do ESP32 Bluetooth and Wi-Fi coexist? In the menuconfig menu, there is a special option called “Software controls WiFi/ Bluetooth coexistence”, which is used to control the ESP32's Bluetooth and Wi-Fi coexistence using software, thus balancing the coexistence requirement for controlling the RF module by both the Wi-Fi and Bluetooth modules. Please note that if Option “Software controls WiFi/Bluetooth coexistence” is enabled, the BLE scan interval shall not exceed 0x100 slots (about 160 ms).
I couldn’t find where menuconfig was located.
I’m using the «`BluetoothSerial.h« library
This is a piece of the code:
// ------------------------------ Inicialização do loop ------------------------------ // void loop() < // Preparando String para enviar ao usuario line_one_length = line_one.length(); line_two_length = line_two.length(); line_three_length = line_three.length(); char receive_line_one[line_one_length], receive_line_two[line_two_length], receive_line_three[line_three_length]; char receive_wifi_ssid[line_one_length], receive_wifi_pass[line_two_length]; String deletar; file = SPIFFS.open("/wifi.txt"); // ------------------------------ Ve se o usuario digitou a palavra deletar ------------------------------ // if (SerialBT.available())< while (SerialBT.available()) < insert_chars = SerialBT.read(); deletar = String(deletar + insert_chars); >deletar.trim(); > // ------------------------------ Conexão ao WiFi ------------------------------ // if (line_one != "" and wifi_state == 0 and receive_wifi_ssid != "") < line_one.toCharArray(receive_wifi_ssid, line_one_length); line_two.toCharArray(receive_wifi_pass, line_two_length); delay(100); WiFi.begin(receive_wifi_ssid, receive_wifi_pass); Serial.println("Conectando ao WiFi. "); while (WiFi.status() != WL_CONNECTED) < Serial.print("."); delay(1000); >Serial.println("\nConectado ao WiFi"); wifi_state = 1; > Serial.print("Estado: "); Serial.println(connection_state); // ------------------------------ Vê se a conteudo dentro do FILE ------------------------------ // if (file.size() > 0) < if (deletar == "deletar")< delete_file_info(); wifi_ssid = ""; wifi_password = ""; database_info = ""; line_one = ""; line_two = ""; line_three = ""; wifi_state = 0; Serial.println("Arquivo deletado com sucesso"); return; >read_file_info(); all_lines(); >else< if (wifi_ssid == "" and connection_state == 0 and line_one == "")< Serial.print("File size: "); Serial.print(file.size()); Serial.println(", O valor dentro da string é nulo, nada será adicionado ao arquivo"); delay(1000); >else if (connection_state == 1 and line_one == "") < // Chamando a função para armazenar os dados do usuario dentro do FILE bluetooth_while_loop(); >> file.close(); delay(3000);
Connect bluetooth and wifi
Let us first start by addressing what the term Bluetooth tethering actually means. The feature, as you may have guessed, is quite similar to the Wi-Fi hotspot and USB tethering, where you can share the Internet connection used by the phone to be shared with any device that supports Bluetooth.
On older Android phones, the Bluetooth tethering feature only allowed sharing of mobile data with other devices. However, if you have a newer Android phone, you can actually use Bluetooth tethering to share (and extend) the range of your Wi-Fi connection to other devices.
Now, the question may arise as to why use Bluetooth tethering in the first place when you have the Wi-Fi hotspot feature, which is not only faster — but easier to use. Let us explain a couple of use cases.
When to use Bluetooth tethering?
As mentioned earlier, Bluetooth tethering is an objectively slower form of connectivity compared to traditional Wi-Fi hotspots. So why use it in the first place? Turns out, there are a couple of occasions when using Bluetooth tethering may actually make sense.
When your phone does not support Wi-Fi sharing over Wi-Fi hotspot
As of 2021, many low-end and mid-range smartphones (still) do not allow a Wi-Fi connection and a Wi-Fi hotspot to be enabled simultaneously. This means you will not be able to use Wi-Fi Hotspot to share your internet connection with other devices. In such scenarios, you are left with no other option but to use Bluetooth tethering since this method of tethering works even if you are connected to a Wi-Fi network.
As to why anyone would want to do this, hear me out.
Suppose your friend pops in for a couple of hours, and you do not really wish to share your Wi-Fi password with him. Instead of opening up the PC and creating a guest network on your router, you can simply create a Wi-Fi Hotspot on your phone (which is already connected to your router) and use it as an extender. And if that doesn’t work, you can use the Bluetooth tethering feature to do the exact same thing. The connection would be slower -, but hey, it would still work for him!
When you want to conserve battery
You might already know it — but the general consensus is that the Wi-Fi Hotspot feature is a massive battery hog. In case you plan to use keep your phone tethered to another device for several hours, the battery might conk faster than you would imagine. In such scenarios, it is wiser to switch to Bluetooth tethering since the battery drain is comparatively lower in this mode.
Let us now move to the part where we actually set up and use Bluetooth tethering to share your Wi-Fi connection. This process is actually really easy. Here’s how to do it.
How to share Wi-Fi over Bluetooth
Step 1: Turn on your phone’s Bluetooth
First, ensure that your phone is already connected to the Wi-Fi network at your home/office. Then, make sure you turn on Bluetooth from within the settings menu.
- To do this, go to Device connectivity > Bluetooth.
- Ensure Bluetooth is On.
Step 2: Turn on Bluetooth tethering on your phone
Now that you’re certain that Bluetooth is enabled on your device:
- Go to Wireless & networks.
- Select Tethering & portable hotspot.
- Enable Bluetooth tethering.
Step 3: Pair your device via Bluetooth and receive connectivity
With this done, you can then use Bluetooth on your laptop or PC, tablet, or another device to connect to your phone and make use of its internet connection.
- Simply pair your smartphone with the device you want to share the connection with under Device connectivity > Bluetooth.
- Once connected, make sure Internet access sharing is enabled in the device profile by tapping the settings icon next to the device name.
- Finally, set up the connected device to receive its network connection via Bluetooth in its network settings.
This is very straightforward with smartphones, but to connect a Windows PC, you have to go to Settings > Devices and printer, right-click the icon representing your phone, and select Connect using > Access point. Ta-Da! You’re ready to start browsing the internet.
Did you find this useful? Have you managed to connect your phone’s internet connection with other devices? Tell us in the comments.
This article was updated in July 2021. Older comments were retained.
How to make wifi and bluetooth work simultaneously?
I have a laptop running Debian Jessie with Intel Wireless-N 7265 BGN adapter. Mine looks like the one on the right, with an antenna cable going to the right-most socket. According to dmesg I’m using WLAN firmware iwlwifi-7265-16.ucode and BT firmware ibt-hw-37.8.10-fw-1.10.2.27.d.bseq , which I have installed from Jessie-backports. As far as I understand, Wifi / Bluetooth coexistence is possible and enabled:
$ sudo modinfo iwlwifi . parm: bt_coex_active:enable wifi/bt co-exist (default: enable) (bool) . $ cat /sys/module/iwlwifi/parameters/bt_coex_active Y
Both Wifi and Bluetooth work separately, however, when I try to activate them at the same time, I lose Wifi connectivity. The laptop disconnects from a network if it was connected, and scanning for networks yields no results unless I disable Bluetooth. Once Bluetooth is disabled, I’m able to reconnect to Wifi again. What could be the reason for this? Is there a particular firmware I could use to get Wifi and Bluetooth work together? Or do I have the bad kind of adaptor, and upgrading it would solve my issue? Would it help to get a second antenna connected? I don’t have one, but I would consider ordering / making one if that is known to help. EDIT: Here’s what happens on my system starting from a fresh reboot:
dmesg | grep iwlwifi > dmesg_iwlwifi dmesg | grep -i blue > dmesg_bluetooth sudo modinfo iwlwifi | grep -v alias > modinfo_iwlwifi sudo modinfo btintel > modinfo_btintel uname -a Linux hostname 4.6.0-0.bpo.1-686-pae #1 SMP Debian 4.6.4-1~bpo8+1 (2016-08-11) i686 GNU/Linux
- setting cfg80211_disable_40mhz_24ghz to «Y» — no visible changes
- setting bt_coex_active to «N» — make things worse: WiFi stops working altogether, BT still works fine
- upgrading firmware to the latest version as described here — no visible changes
- setting disable_11ac to «Y» and 11n_disable to 1 — no visible changes
Final edit: we went to see some friends this weekend, and when I wanted to show them my issue, I discovered that I could stay connected to their router while using BT. There is a drop in connection speed when BT is in use, but this is to be expected AFAIK. I’m not sure if my question will be of any use to anyone, but just in case: the router I had the issue with was a german FritzBox configured to 2.4 GHz, channel 6. Since I don’t own that router, I can’t really fiddle with it to investigate.