Bluetooth

To have Bluetooth devices inter-operable, different profiles exist: Hands-free (HFP), Object Push (OPP), File transfer (FTP), Dail-up Networking (DUN), Synchronization (SYNC) and Serial Port (SPP).

Bluetooth is well supported by Linux. Cheap sub miniature and bigger devices as D-Link DBT-122 work well. The devices are Host Controller Interface and appear as /sys/class/bluetooth/hci0

For Gentoo there is a bluetooth useflag to get support and configure kernel as shown in the Gentoo Bluetooth guide (https://wiki.gentoo.org/wiki/Bluetooth

net-wireless/bluez is the package to be used with the kernel.

Start the Bluetooth daemon:

/etc/init.d/bluetooth start or systemctl start bluetooth

Figure 8.3. Bluetooth daemon

Bluethooth daemon


See what the hciconfig finds:

hci0: Type: USB

BD Address: 00:19:5B:10:E7:81 ACL MTU: 1017:8 SCO MTU: 64:0

UP RUNNING PSCAN ISCAN

RX bytes:382 acl:0 sco:0 events:17 errors:0

TX bytes:318 acl:0 sco:0 commands:17 errors:0

Note

The interface must be UP RUNNING. If not hciconfig hci0 up

The BD Address is the MAC address of your host device.

Edit /etc/bluetooth/pin to give it a personalized id number, that serves as a password when two bluetooth devices get paired. The run:

/etc/init.d/bluetooth restart

rc-update add bluetooth default

Look for active devices around (there is also a virtual device in the kernel)

hcitool scan

Scanning ...

00:1D:3B:82:1C:9D Nokia 2760

And notice the ID found.

Now do a ping and wait for 3 responses.

l2ping -c3 00:1D:3B:82:1C:9D

For debugging reasons packages can be captured install bluez-hcidump and run hcidump

Phones are quite different in what they support so:

sdptool browse 00:1D:3B:82:1C:9D | grep "Service Name" to see what it supports. When no response is received than it still supports something, but it is just unable to respond. Maybe it is a simple device as bluetooth headset.

Bluetooth headsets

As other bluetooth devices headsets (or bluetooth to audio converters) need to be paired as well. Therefore they need a pin. Devices as computer pop up windows where you can enter the pin. A headset however has no keypad and no display. Look at its manual where you should find the pin. Often pins as 0000 are used since it is not considered to do much damage with the headset device. However if you connect to somebodies headset device you might listen things that he is not aware. A2DP is the audio protocol that bluetooth uses.

After pairing it must be connected, to have this working the kernel must have the uinput module compiled Device drivers > Input device support > Miscellaneous devices > User level driver support that creates /dev/input/uinput (uinput is its module name when not included into the kernel)

The next step is that alsa needs to be aware about the new sound device. Create ~/asoundrc with

pcm.bluetooth {
   type bluetooth
   device 00:02:72:EE:B3:OC
}

The restart bluethooth /etc/init.d/bluetooth restart and mplayer -ao alsa:device=bluetooth /usr/share/sounds/alsa/*

Bluetooth connection to the phone

To connect devices, the application rfcomm can be used.

A configuration file /etc/bluetooth/rfcomm.conf can hold the necessary settings so they do not have to insert at command line:

rfcomm0 {
# Automatically bind the device at startup         
# Creates the device node, /dev/rfcomm0 at start up)
bind yes;
# Bluetooth address of the device you want to connect to
device 00:1D:3B:82:1C:9D;
}        

then type rfcomm connect 0 to make use of the configuration file or type

rfcomm connect 0 00:1D:3B:82:1C:9D 1 to do the settings on the command line to connect to your device using channel 1

rfcomm should show now the device and the file /dev/frcomm0 should be created.

If it fails, verify by ls -l /dev | grep rfcomm if the file already exists. Some desktop bluetooth applications might get a hold of your host bluetooth device. Also a mismatch between the pins might occur, there is a pin under /etc/bluetooth/pin, gnome desktop has and creates randomly pins and the phone itself has some pin.

Having the connection, different protocol might make use it. Phones highly differ in the protocols they support. There is a high chance that the phone understands some of the many AT commands.

To test it, a terminal as gtkterm can be used. However gtkterm fails if rfcomm0 is the only port, in this case use minicom -s to start minicom and go directly to its settings or plug in a USB RS232 adapter to make gtkterm happy.

If the connection is there type in at and ok should come back. On Nokia phones type ati3 and Nokia 2760 the model number should come back.

Gnome bluetooth

Install gnome-bluetooth to get common bluetooth support and gnome-phone-manager to exchange messages. To exchange data with a phone install gnome-user-share then it should be able to set in System -> Preferences -> Personal File Sharing exchange data via bluetooth.

On Android phones the necessary bluetooth software might not be installed when it comes out of the box.

Kdebluetooth

kbluetooth

Other bluetooth software

There is: blueman

Bluetooth mesh

https://www.bluetooth.com/learn-about-bluetooth/feature-enhancements/mesh/ and https://www.bluetooth.com/bluetooth-mesh-networking-primer/ https://blog.drogue.io/bluetooth-mesh/ https://pidiylab.com/raspberry-pi-bluetooth-mesh/

Bluetooth mesh is supported when the device supports low energy. Therefore run btmgmt info and check for le in supported settings: le

btmgmt is deprecated and old, bluetoothctl replaces it and starts as console application when no option is passed. Type menu mgmt and then info to see if le is supported. USB-Bluetooth-devices newer than Bluetooth 4.x is required and a chip from Intel-, Realtek- or MediaTek is recommended.

The BlueZ https://github.com/bluezstack supports it from >5.47 using the now deprecated meshctl tool. 5.53 used the new mesh-cfgclient tool.

/usr/libexec/bluetooth/bluetooth-meshd --help

but there is also a service ls -l /usr/lib/systemd/system/*mesh*

Important

There are two daemons the bluetoothd and bluetooth-meshd and also two services. Stop bluetoothd and run the bluetooth-meshd

systemctl cat bluetooth-mesh

mesh-cfgclient create to create the mesh network on the provisioner and its cat ~/.config/meshcfg/config_db.json

discover-unprovisioned on

Now wait to discover devices sending broadcast "please provision me" packets containing their UUID as 0EF827B94FA04859A4F2C80312C2724E. most tutorials use microcontrollers for the mesh nodes as Espressif ESP32: Supports BLE Mesh via the ESP-IDF esp_ble_mesh component. The ESP32-DevKitC works out of the box with the Espressif BLE Mesh examples. But it is also possible to use the meshcli for that.

When having received the "please provision me" beacon do provision <UUID>

There is python-bluetooth-mesh and its option python-bluetooth-mesh-bluez. To get both pip install bluetooth-mesh[bluez] it is actually a wrapper for bluetooth-mesh-messages and bluetooth-mesh-network. python-bluetooth-mesh comes with the command meshcli

Messages are sent out with a TTL (time-to-live) value.

Check quality of the bluethooth signals

Important

Bluetooth uses the same frequencies as 2.4GHz WLAN. Bluetooth uses Adaptive Frequency Hopping (AFH) to ways avoiding conflicts with WLAN.

bluetoothctl to get the MAC address of the device to be observed then hcitool rssi <MAC address> or btmon

Things to observe are:

  • RSSI (Received Signal Strength Indicator) for a dBm value, the lower the better

  • TX Power (Sending power), might be adapted automatically to what is necessary, below the maximum possible is a good sign.

  • Packet Error Rate (PER) shows how much packages got lost and had to be re-sent

Chips with combined WLAN/bluetooth share the same antenna. Wi-Fi/BT Coexistence in the driver should be activated. Depending on the chip manufacturer the device driver option are: bt_coex_active, ant_sel, coex. To have awareness to coordinate the access to the antenna.

modinfo <driver> will show the available parameters

ls /sys/module/<driver>/parameters/ is the place to see what is active


Linurs startpage