7
votes

I have got 2 Raspberry Pis, running Raspbian, up-to-date. Each Pi has exactly one Bluetooth USB dongle. I can make them communicate via RFCOMM using this.

Now, I would like to establish a proper IP layer over the Bluetooth physical link. As far as I understand, I have to use PAN (Personal Area Network), that should create a network interface I could manage with ifconfig. I am trying to adapt this tutorial, but I am stuck at an invalid exchange error.

On RPi0, I run

$ sudo pand --nodetach --listen --master --role=NAP
pand[2313]: Bluetooth PAN daemon version 4.99

On RPi1, I run

$ pand --nodetach --connect 00:1A:7D:DA:71:16
pand[2323]: Bluetooth PAN daemon version 4.99
pand[2323]: Connecting to 00:1A:7D:DA:71:16
pand[2323]: Connect to 00:1A:7D:DA:71:16 failed. Invalid exchange(52)

I cannot find any relevant information about this error code. Any idea how I could solve this ? Or am I mistaken and should I use something else instead of PAN ?

Thanks in advance !

EDIT: When specifying the Bluetooth device I want to listen on, I get another error message. It doesn't really make sense either, since I do not run any other Bluetooth server.

$ sudo pand -i 00:1A:7D:DA:71:16 --nodetach --listen --role=NAP
pand[2582]: Bluetooth PAN daemon version 4.99
pand[2582]: Bind failed. Address already in use(98)
4
wnated to know - can I connnect sevral devices using Bluetooth at the same time? lets say I have 3 tablets that only have Bluetooth , can I connect all 3 of them to the pi and then go to the internet at the same time ?Korenron
Hi! No idea, unfortunately. I haven't been working with Bluetooth for a few years now. Maybe you should consider opening a brand new Stack Overflow question? Best of luck.tvuillemin

4 Answers

10
votes

So things seem to have changed quite a lot recently and the Pi comes with Bluez 5.23 in 2016.

Having just spent two days, these steps have solved it for my pi but might help for any Debian Jessie install. I hope so. Tested on a new pi, running jessie with fresh install just now. This will give a bluetooth pan bridged to your eth0 network (and thus use your existing dhcp/dns server etc). This is my first post, so please forgive stupidity around the various conventions here. I hope this helps someone and saves you a little time.

This is prbably not an optimal solution (I'm no guru), and I'd love to hear about some improvements.

Install some things (python stuff will help with scritps): sudo apt-get install bridge-utils bluez python-dbus python-gobject

Download two very cool python scripts, put them in /usr/local/bin and chmod both perhaps to 755 depending on who needs access to execute... blueagent5 and bt-pan. Many thanks and homage to their respective authors. Gosh this kind of thing saves so much time and misery.

Now, we need a bridge. Add the following to the end of /etc/network/interfaces

auto pan0
iface pan0 inet dhcp
  bridge_stp off
  bridge_ports eth0

I rebooted at about this time to make sure all was as it would be normally.

sudo reboot

Log back in and we issue

modprobe bnep
hciconfig hci0 lm master,accept
ip link set pan0 up

If you don't want pin prompt, don't do this next step. To ensure we get a PIN prompt, issue this...

hciconfig hci0 sspmode 0

Start PAN using the special magic in the bt-pan script. It doesn't return, so add an ampersand at the end.

bt-pan  server pan0 &

Start the bluetooth security agent with wonderful ease and confidence. Optionally set a pin (it defaults to 0000).

blueagent5 --pin 4321 &

Okay, one last thing. Forward the network. This will only work if there is no fancy authentication at the router/dhcp, if there is, you may need to look further to solve this issue.

sysctl -w net.ipv4.ip_forward=1

iptables -A INPUT -i pan0 -j ACCEPT
iptables -A FORWARD -i pan0 -j ACCEPT

Once done, you may need to save these iptables settings and reinstate them each time the system boots.

Tiptoe over to your tablet or whatever you are trying to connect to the internet. Open Bluetooth in your settings. Pair with 4321 as your pin, and connect to the local network.

But you didn't need to tiptoe after all, it all seems quite robust to me. Enjoy!

4
votes

After a lot of struggle, this is how I got it working. Let's hope it can help someone else. Long story short, edit /etc/bluetooth/main.conf to include this line:

DisablePlugins = network

Restart the bluetooth service.

sudo service bluetooth restart

FYI, here is my command for the "server" :

sudo pand -i 00:1A:7D:DA:71:14 --listen --master --role=NAP

And for the "client":

sudo pand --nodetach --connect 00:1A:7D:DA:71:14

Then you can just use ifconfig, ping, ssh... usual stuff. You might want to take a look here, it was useful to me.

2
votes

@cowswithguns

Your solution works almost flawlessly except for

bt-pan  server pan &

it should be

bt-pan  server pan0 &

Works great with rpi3 and bluez5.X

Only caveat it still uses somehow the wifi. I am trying to ssh to rpi3 via bluetooth while rpi3 is associated with a device that provide its own AP for a one-to-one connection and this cuts off the bluetooth communication. Works only if the rpi3 is associated to a wifi AP.

1
votes

A bluez-tools package is available on raspbian with the same functionality as the aforementioned python scripts. original answer

Here's how you can setup a bluetooth pan on Raspbian.

This answer uses systemd/networkd but you could do something similar with an interfaces file and dnsmasq if you wanted.

Run:

sudo apt-get install bluez-tools

Create the following files:

/etc/systemd/network/pan0.netdev

[NetDev]
Name=pan0
Kind=bridge

/etc/systemd/network/pan0.network

[Match]
Name=pan0

[Network]
Address=172.20.1.1/24
DHCPServer=yes

/etc/systemd/system/bt-agent.service

[Unit]
Description=Bluetooth Auth Agent

[Service]
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput
Type=simple

[Install]
WantedBy=multi-user.target

/etc/systemd/system/bt-network.service

[Unit]
Description=Bluetooth NEP PAN
After=pan0.network

[Service]
ExecStart=/usr/bin/bt-network -s nap pan0
Type=simple

[Install]
WantedBy=multi-user.target

Then run

sudo systemctl enable systemd-networkd
sudo systemctl enable bt-agent
sudo systemctl enable bt-network
sudo systemctl start systemd-networkd
sudo systemctl start bt-agent
sudo systemctl start bt-network

Finally to pair, run:

sudo bt-adapter --set Discoverable 1

Then, on the other device pair it and connect the network. Hopefully everything should just work after that.