android - Linux 到 Android 蓝牙网络共享 (PAN)

标签 android linux networking bluetooth dnsmasq

我有一个启用了蓝牙的 [嵌入式] Linux 设备,我想通过蓝牙将它的内部网络(未连接到互联网)共享到 Android 设备(尝试过 Android 6.x 和现在的 7.1.2 - Lineage OS [根])。该设置已经适用于 Linux 或 Windows 客户端。 我通读了大量的帖子和文档,但我就是无法让它在 Android 设备上运行:

我的 Linux 设备:dnsmasq 作为 DHPC 服务器,启用蓝牙并且“bt-pan”[1] 已启动并运行(桥接接​​口称为 bt-pan0)。通过 Dbus-Events,我自动“信任”任何新连接的设备。设备类(如果重要的话)使用 hciconfig 配置为 0x020000。

在我的 Android 手机上,我与我的 Linux 设备(热点)配对(自动运行)并连接(使用“互联网访问”)。 在 dnsmasq 日志中,我看到连接的设备正确获取了 IP 地址。

从 linux-box 我可以在发布的 IP 地址上 ping 手机,我看到一个新的 bnep0 设备被创建并添加到 bt-pan0 网桥.

当我通过 adb shell 连接到我的手机时,我还看到正在使用我的 dnsmasq 提供的正确 IP/网络掩码设置创建一个 bt-pan 设备。 不幸的是,当启用 Wifi 或移动数据时 - 我无法通过它的 IP 地址 ping linux-box。禁用 Wifi 和移动数据时 - 设置工作正常。 这是我通过 adb shell 执行的一些命令的输出 - 如果需要更多输出,我会尽快提供(移动数据 IP 地址在粘贴时由我修改):

# 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

bt-pan0 设备的 dnsmasq 配置中,我设置了一个空的 default-gw(dhcp-option 3)和一个正确的 DNS 服务器(dhcp-option 6)。 我阅读了有关设置 dhcp 选项 121(无类静态路由)的信息,但这也不起作用(DHCP 请求不查询 121)...

如果有人知道发生了什么或知道为什么此设置会导致问题 - 我会很高兴最终弄清楚。

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

最佳答案

我一直不明白为什么路由不起作用。

幸运的是,我可以设置我正在使用的库 (Paho MQTT) 的 SocketFactory。 因此 - 我使用 ConnectivityManager 查找蓝牙设备及其“网络”对象(提供“getSocketFactory”)以在 MQTT 中设置 SocketFactory。然后连接工作。

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

... 找到蓝牙设备并使用“getSocketFactory”。 查找蓝牙设备

cm.getNetworkInfo(network);

有帮助

关于android - Linux 到 Android 蓝牙网络共享 (PAN),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502036/

相关文章:

javascript - WebSocket TCP 数据包聚集在一起?

ios - 如何打开iOS状态栏中的网络指示灯?

android - 如何使用 SafeArgs 将 `null` 设置为可空参数的默认值?

java - 如何在来电时停止音乐?

java - 如何将数组从 JNI 返回到 Java?

c - ‘struct file_operations’ 的强制函数

android.graphics.View invalidate(int, int, int, int) Canvas 的一部分,但是 onRedraw() 整个 Canvas ?

c - 在linux中使用管道同时写入文件

linux - 使安装错误 'nothing to be done'

android - 在 3G 网络中使用 Retrofit 时流被重置为 : PROTOCOL_ERROR,