How to set up Bluetooth NAP (Network Access Point) with bluez 4.96

本帖最後由 howdy 於 2011-11-22 22:05 編輯

I am writing this after not having much luck in finding any up-to-date instruction on how to setup bluetooth NAP with the current stable BlueZ  (v4.96 as of Nov2011). Most are still making use of the deprecated pand/dund, which its use is discouraged.

Note that there seems to be some nice GUI (connman, NetworkManager etc.) providing easier point-and-click setup of NAP. For those heading that direction, the following howto is probably not what you are after.

Acknowledgments
BlueZ developer Mr. Johan Hedberg for his kind assistance, esp in pointing me to the test scripts in the repository

Preamble
What I have:
a newly setup Ubuntu 11.10 machine (the host) – minimal installation, no graphical user interface
The host is connected to an NAT router via eth0.  IP is allocated via DHCP from the NAT router.

What I want to do:
Setup a bluetooth NAP to provide intranet/internet access for PAN users (PANUs) (laptop, Android tablet etc.)

Why not wifi/<insert whatever substitute>, <insert whatever reason>
I'm doing this just for fun at home.

Bluez installation
  1. apt-get install bluetooth
複製代碼

This should get bluez and its utilities installed on a debian-based distro. Please refer to your distro-specific instructions to get bluez installed.

It is outside the scope of this simple guide on getting your bluetooth hardware to be detected and configured.  Here we assume you have a working bluetooth hardware, and that the command
“hcitool dev” should return the device address, while “sdptool browse local” should return a list of announced services.
  1. #hcitool dev
  2. Devices:
  3.         hci0    00:65:56:BB:AA:12
複製代碼
  1. # sdptool browse local
  2. Browsing FF:FF:FF:00:00:00 ...
  3. Service Name: Headset Audio Gateway
  4. Service RecHandle: 0x10000
  5. Service Class ID List:
  6.   "Headset Audio Gateway" (0x1112)
  7.   "Generic Audio" (0x1203)
  8. Protocol Descriptor List:
  9.   "L2CAP" (0x0100)
  10.   "RFCOMM" (0x0003)
  11.     Channel: 12
  12. Profile Descriptor List:
  13.   "Headset" (0x1108)
  14.     Version: 0x0102

  15. Service Name: Hands-Free Audio Gateway
  16. Service RecHandle: 0x10001
  17. Service Class ID List:
  18.   "Handsfree Audio Gateway" (0x111f)
  19.   "Generic Audio" (0x1203)
  20. Protocol Descriptor List:
  21.   "L2CAP" (0x0100)
  22.   "RFCOMM" (0x0003)
  23.     Channel: 13
  24. Profile Descriptor List:
  25.   "Handsfree" (0x111e)
  26.     Version: 0x0105
  27. …...
  28. …...
  29. …...
複製代碼
Setting up the network bridge:

The original /etc/network/interfaces file:
  1. # The loopback network interface
  2. auto lo
  3. iface lo inet loopback

  4. # The primary network interface
  5. auto eth0
  6. iface eth0 inet dhcp
複製代碼
We define a new bridge interface br0, and add the existing eth0 as the first interface
The modified /etc/network/interfaces file:
  1. # The loopback network interface
  2. auto lo
  3. iface lo inet loopback

  4. # The bridge br0
  5. auto br0
  6. iface br0 inet dhcp
  7.         bridge_ports    eth0
  8.         bridge_fd       5
  9.         bridge_stp      yes
複製代碼
Restart networking
Warning: A wrong networking config may block you from accessing the machine via the network (e.g. SSH). Please make sure you have access to the host machine console before proceeding!

Restart networking according to your distro-specific way (service networking restart etc...)

ifconfig should now return something like this (note the new bridging interface br0):
  1. #ifconfig
  2. br0       Link encap:Ethernet  HWaddr 00:1c:c0:0e:12:32  
  3.    inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
  4.    inet6 addr: fa90::31c:61ef:ef0e:1c23/64 Scope:Link
  5.    UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  6.    RX packets:56 errors:0 dropped:0 overruns:0 frame:0
  7.    TX packets:63 errors:0 dropped:0 overruns:0 carrier:0
  8.    collisions:0 txqueuelen:0
  9.    RX bytes:6584 (6.5 KB)  TX bytes:9262 (9.2 KB)

  10. eth0      Link encap:Ethernet  HWaddr 00:1c:c0:0e:12:32  
  11.    UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  12.    RX packets:261295 errors:0 dropped:0 overruns:0 frame:0
  13.    TX packets:430307 errors:0 dropped:0 overruns:0 carrier:0
  14.    collisions:0 txqueuelen:1000
  15.    RX bytes:138426648 (138.4 MB)  TX bytes:170481060 (170.4 MB)
  16.    Interrupt:43

  17. lo        Link encap:Local Loopback  
  18.    inet addr:127.0.0.1  Mask:255.0.0.0
  19.    inet6 addr: ::1/128 Scope:Host
  20.    UP LOOPBACK RUNNING  MTU:16436  Metric:1
  21.    RX packets:811 errors:0 dropped:0 overruns:0 frame:0
  22.    TX packets:811 errors:0 dropped:0 overruns:0 carrier:0
  23.    collisions:0 txqueuelen:0
  24.    RX bytes:68501 (68.5 KB)  TX bytes:68501 (68.5 KB)
複製代碼
brctl should display the configured bridge:
  1. #brctl show
  2. bridge name     bridge id    STP enabled    interfaces
  3. br0        8000.001cc00e2c23       yes       eth0
複製代碼

Enable IP forwarding
We need to tell the kernel to enable IP forwarding of course!
  1. echo 1 > /proc/sys/net/ipv4/ip_forward
複製代碼
BlueZ configurations
Note that pand is obsolete, and we do NOT need them. Instead, we need to use the BlueZ D-Bus interface to register the NAP service.
A very handy python script to register the NAP service via the D-Bus interface is available in the BlueZ GIT repository under the “test” directory. This script doesn't seems to be distributed by any major distro.

test-nap from BlueZ GIT repository
  1. #!/usr/bin/python

  2. import sys
  3. import time
  4. import dbus
  5. from optparse import OptionParser, make_option

  6. bus = dbus.SystemBus()

  7. manager = dbus.Interface(bus.get_object("org.bluez", "/"),
  8.                                  "org.bluez.Manager")

  9. option_list = [
  10.       make_option("-i", "--device", action="store",
  11.                    type="string", dest="dev_id"),
  12.       ]
  13. parser = OptionParser(option_list=option_list)

  14. (options, args) = parser.parse_args()

  15. if options.dev_id:
  16.         adapter_path = manager.FindAdapter(options.dev_id)
  17. else:
  18.         adapter_path = manager.DefaultAdapter()

  19. server = dbus.Interface(bus.get_object("org.bluez", adapter_path),
  20.                                   "org.bluez.NetworkServer")

  21. service = "nap"

  22. if (len(args) < 1):
  23.         bridge = "tether"
  24. else:
  25.         bridge = args[0]

  26. server.Register(service, bridge)

  27. print "Server for %s registered for %s" % (service, bridge)

  28. print "Press CTRL-C to disconnect"

  29. try:
  30.         time.sleep(1000)
  31.         print "Terminating connection"
  32. except:
  33.         pass

  34. server.Unregister(service)
複製代碼
Run test-nap with the bridging interface as the first argument will register the NAP service
  1. #./test-nap br0
  2. Server for nap registered for br0
  3. Press CTRL-C to disconnect
複製代碼
Note that the script “test-nap” needs to be running as long as you need the NAP service. BlueZ will automatically unregister the service if the process is killed. You may want to customize the script to your need.

At this point, our bluetooth NAP service should be configured on the host machine. We will now try to get a client(PANU) to connect and pair with the host machine.

Pairing with another device

Enable discovery for pairing
  1. bluez-test-adapter pairable on
  2. bluez-test-adapter discoverable on
複製代碼
Run bluez-simple-agent (or simple-agent), and start pairing from your bluetooth client(PANU). Bluez-simple-agent will present you with the passkey confirmation and authorization question during the pairing process.
  1. #bluez-simple-agent
  2. Agent registered
  3. RequestConfirmation (/org/bluez/25148/hci0/dev_00_1E_33_F6_64_3A, 996660)
  4. Confirm passkey (yes/no): yes
  5. Authorize (/org/bluez/25148/hci0/dev_00_1E_33_F6_64_3A, 0000000f-0000-1000-8000-00805f9b34fb)
  6. Authorize connection (yes/no): yes
複製代碼
Upon sucessful pairing, you may want to trust the bluetooth client(PANU) to avoid the need to grant authorization every single time in the future. (replace the hardware address accordingly)
  1. bluez-test-device trusted 00:1E:33:F6:64:3A yes
複製代碼
At this point, you should be able to connect your NAP client (PANU) to the host machine and access your intranet/internet resources. “brctl show” should show something like this upon any client connection.
  1. #brctl show
  2. bridge name     bridge id    STP enabled    interfaces
  3. br0        8000.0015834bb269    yes         bnep0
  4.                                          eth0
複製代碼

本帖最後由 killerpub 於 2011-11-21 00:57 編輯

多口問 throughput 有幾多?

It works, but how viable ?
Would you mind give us your throughput for reference?
Thanks.

TOP

回復 2# killerpub
I didn't do any measurements or benchmarks haha.  But when I was playing with it during that few minutes, I didn't notice any significant slowdown in simple web browsing.
I maybe able to provide more info later on if I am going to use that again.

TOP

本帖最後由 hlink10 於 2014-11-17 17:18 編輯

I am writing this after not having much luck in finding any up-to-date instruction on how to setup bluetooth NAP with the current stable BlueZ  (v4.96 as of Nov2011). Most are still making use of the deprecated pand/dund, which its use is discouraged.

Note that there seems to be some nice GUI (connman, NetworkManager etc.) providing easier point-and-click setup of NAP. For those heading that direction, the following howto is probably not what you are after.

Acknowledgments
BlueZ developer Mr. Johan Hedberg for his kind assistance, esp in pointing me to the test scripts in the repository

Preamble
What I have:
a newly setup Ubuntu 11.10 machine (the host) – minimal installation, no graphical user interface
The host is connected to an NAT router via eth0.  IP is allocated via DHCP from the NAT router.

What I want to do:
Setup a bluetooth NAP to provide intranet/internet access for PAN users (PANUs) (laptop, Android tablet etc.)
ERP
海外升學
[url=
business english
Why not wifi/<insert whatever substitute>, <insert whatever reason>
I'm doing this just for fun at home.
...
howdy 發表於 2011-11-20 22:22


Thanks for sharing

仲煩緊點SETUP個BLUETOOTH..

TOP