简体   繁体   中英

Linux to Android Bluetooth Tethering (PAN)

I'm having a bluetooth enabled [embedded] Linux device and would like to share it's internal Network (NOT connected to the internet) via Bluetooth to Android devices (tried Android 6.x and now 7.1.2 - Lineage OS [rooted]). The setup already works well with Linux or Windows Clients. I read through tons of postings and documentations but I just cannot get it to work with Android devices:

My Linux-Device: dnsmasq as DHPC Server, Bluetooth Enabled and "bt-pan" [1] is up and running (the bridge interface is called bt-pan0 ). Via Dbus-Events I automatically "trust" any new connected device. Device-Class (if that matters) is configured with hciconfig to 0x020000.

On my Android phone, I pair (which works automatically) and connect (use "Internet Access") with my Linux-Device (Hotspot). In the dnsmasq log I see that the connected device gets an IP address properly.

From the linux-box I can ping the mobile phone on the published IP address and I see a new bnep0 device being created and added to the bt-pan0 bridge.

When I connect via adb shell to my phone, I also see a bt-pan device being created with the correct IP/Netmask setup as provided by my dnsmasq. Unfortunately when Wifi or Mobile-Data are enabled - I cannot ping the linux-box via it's IP address. When Wifi and Mobile-Data are disabled - the setup works fine. Here is the output of a few commands I executed via adb shell - if more output is required I'll provide it ASAP (Mobile-Data IP address is modified by me while pasting):

# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0       *            255.255.255.0   U     0      0        0 bt-pan
123.456.78.16      *            255.255.255.252 U     0      0        0 rmnet_data0

# ip route get 192.168.10.5
192.168.10.5 via 123.456.78.18 dev rmnet_data0  src 123.456.78.17  uid 0 
cache 

# ip rule show
0:  from all lookup local 
10000:  from all fwmark 0xc0000/0xd0000 lookup legacy_system 
10500:  from all oif dummy0 uidrange 0-0 lookup dummy0 
10500:  from all oif rmnet_data0 uidrange 0-0 lookup rmnet_data0 
10500:  from all oif bt-pan uidrange 0-0 lookup bt-pan 
13000:  from all fwmark 0x10063/0x1ffff lookup local_network 
13000:  from all fwmark 0x100b9/0x1ffff lookup rmnet_data0 
13000:  from all fwmark 0x100ba/0x1ffff lookup bt-pan 
14000:  from all oif dummy0 lookup dummy0 
14000:  from all oif rmnet_data0 lookup rmnet_data0 
14000:  from all oif bt-pan lookup bt-pan 
15000:  from all fwmark 0x0/0x10000 lookup legacy_system 
16000:  from all fwmark 0x0/0x10000 lookup legacy_network 
17000:  from all fwmark 0x0/0x10000 lookup local_network 
19000:  from all fwmark 0xb9/0x1ffff lookup rmnet_data0 
19000:  from all fwmark 0xba/0x1ffff lookup bt-pan 
22000:  from all fwmark 0x0/0xffff lookup rmnet_data0 
23000:  from all fwmark 0x0/0xffff uidrange 0-0 lookup main 
32000:  from all unreachable

In the dnsmasq configuration for the bt-pan0 device I set an empty default-gw (dhcp-option 3) and a proper DNS server (dhcp-option 6). I read about setting dhcp-option 121 (classless static routes) but that didn't work neither (the DHCP-request does not query for 121)...

If someone has any idea what is going on or knows why this setup is causing issues - I would be very happy to finally figure that out.

[1] https://github.com/mk-fg/fgtk/blob/master/bt-pan

I never figured out why the routing doesn't work.

Fortunately I can set the SocketFactory of the library I'm using (Paho MQTT). Thus - I use the ConnectivityManager to find the bluetooth device and its 'Network' object (which offers 'getSocketFactory') to set the SocketFactory in MQTT. Then the connection works.

ConnectivityManager cm =(ConnectivityManager)ctx.getSystemService(CONNECTIVITY_SERVICE);
Networks[] nets = cm.getAllNetworks();

... find Bluetooth device and use 'getSocketFactory'. To find the Bluetooth Device

cm.getNetworkInfo(network);

is helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM