How to serve localhost in rapsbery pi thru bluetooth

neonexxa
3 min readMar 24, 2023

Serving localhost on a Raspberry Pi via Bluetooth isn’t a typical use case, as Bluetooth is not designed to handle the same kind of network traffic that Wi-Fi or Ethernet can handle. However, you can set up a Bluetooth PAN (Personal Area Network) to create a small-scale network and access services running on your Raspberry Pi through Bluetooth. Here’s a step-by-step guide:

  1. Update and upgrade your Raspberry Pi:
sudo apt update
sudo apt upgrade

2. Install necessary Bluetooth packages:

sudo apt install bluetooth bluez bluez-tools rfkill

3. Enable Bluetooth service and start it:

sudo systemctl enable bluetooth.service
sudo systemctl start bluetooth.service

4. Verify Bluetooth is active:

sudo rfkill unblock bluetooth
sudo hciconfig hci0 up

5. Install dnsmasq for DHCP services:

sudo apt install dnsmasq

6. Edit the dnsmasq configuration file:

sudo nano /etc/dnsmasq.conf

Add these lines to the file:

interface=bnep0
dhcp-range=192.168.99.100,192.168.99.200,255.255.255.0,12h

Save and close the file.

7. Create a script to set up Bluetooth PAN networking:

sudo nano /usr/local/sbin/bt-pan

8. Add the following content to the bt-pan script:

#!/bin/sh

PAN_ADDR="192.168.99.1"
DEV="bnep0"

case $1 in
"up")
ifconfig $DEV $PAN_ADDR netmask 255.255.255.0 up
systemctl restart dnsmasq
;;
"down")
ifconfig $DEV down
;;
*)
echo "Usage: $0 [up|down]"
exit 1
;;
esac

9. Save the file and make it executable:

sudo chmod +x /usr/local/sbin/bt-pan

10. Create a systemd service to start the script automatically:

sudo nano /etc/systemd/system/bt-pan.service

Add the following content to the file:

[Unit]
Description=Bluetooth PAN
Requires=bluetooth.service
After=bluetooth.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/bt-pan up
ExecStop=/usr/local/sbin/bt-pan down

[Install]
WantedBy=multi-user.target

11. Enable and start the bt-pan service:

sudo systemctl enable bt-pan.service
sudo systemctl start bt-pan.service

12. Edit the /etc/bluetooth/main.conf file:

sudo nano /etc/bluetooth/main.conf

13. Uncomment and modify the Class field under [General] to enable Network Access Point (NAP) profile:

Class = 0x200420

Save and close the file.

14. Restart the Bluetooth service:

sudo systemctl restart bluetooth.service

15. Pair your Raspberry Pi with your other device (e.g., smartphone, tablet, or computer) over Bluetooth. Make sure the other device supports Bluetooth PAN (Personal Area Network) or NAP (Network Access Point) profile.

16. Once paired, connect to the Raspberry Pi’s NAP service from your other device. The exact steps may vary depending on the operating system of the device you are connecting to the Raspberry Pi.

17. After connecting to the Raspberry Pi’s NAP service, your device should receive an IP address from the 192.168.99.x range. You can now access your Raspberry Pi's localhost services by using its IP address (192.168.99.1).

Please note that this setup allows you to access the services running on the Raspberry Pi using IP addresses and not domain names like localhost. Also, Bluetooth connections typically have lower bandwidth and range compared to Wi-Fi or Ethernet, so performance may be limited depending on your use case.

Can We Access it In the Tablet Browser?

Yes, once you have successfully connected your device to the Raspberry Pi via Bluetooth PAN and obtained an IP address in the 192.168.99.x range, you can access the services running on the Raspberry Pi by using its IP address (192.168.99.1) in your device’s web browser.

For example, if you have a web server running on the Raspberry Pi, you can access it by entering http://192.168.99.1 or http://192.168.99.1:port_number (if it's running on a specific port) in your device's web browser.

However, keep in mind that Bluetooth connections typically have lower bandwidth and range compared to Wi-Fi or Ethernet, so the performance of web services accessed over Bluetooth may be limited.

--

--