Linux command to connect to a Bluetooth device - linux

I have this Pi project where DBUS is not an option, but bluetooth connections are needed, in an embedded-like enviroment where services are mostly unwanted.
I was able to setup the BT hardware on the Pi by loading it's modules, and attaching it to hci:
sudo modprobe hci_uart
sudo modprobe btbcm
sudo hciattach /dev/ttyAMA0 bcm43xx 921600 noflow -
I can see it's working without problems with:
sudo hciconfig -a
I can easily bring up the device like this:
sudo hciconfig hci0 up
I can even scan devices with sudo hcitool scan, and I can see the target devices in range. Great so far!
The problem comes when I try to connect the Pi3b+ to them. There used to be hidd --connect <address>, but the hidd command has been deprecated in favor of some mumbo-jumbo using DBUS and systemd services. I guess a service is needed to manage connections, etc, but all I need here is to manually stablish an one-time BT connection manually. Simple.
So, what should I use in place of hidd --connect <address> now? I have tried sudo hcitool cc <address>, but it doesn't stablish any connections (connections can be verified with hcitool con).
Please don't tell me to use the bluetoothd service, that's not an option.

Related

bcm43xx_init Initialization timed out with buildroot raspberry pi 3 hciattach

I am trying to get the Bluetooth to work in Raspberry Pi 3.
I am using buildroot with:
BlueZ 5.45 ( changed the package to get the latest)
Not using systemd, but I run bluetoothd because
the bluez script is for systemd.
In buildroot I enabled the bt firmware.
I can see the hci_uart.ko file and the Bluetooth firmware hcd file
I removed the console=ttyAMA0,115200 from cmdline.txt
I added rfkill for busybox but I only see the wifi interface, no Bluetooth interface.
running:
hciattach /dev/ttyAMA0 bcm43xx 921600 noflow -
output showed:
bcm43xx_init
Initialization timed out
tried 115200, the same.
Notes:
When using BlueZ 3 I am getting invalid id
I read that it is somehow related to BlueZ version so I did take the latest
All the above was taken after deep search.
So what I am missing here?
How can I get the Bluetooth to start running in my Raspberry Pi 3
with buildroot as image creator?
Thank you all!
The main problem is that buildroot adds the pi3-miniuart-bt overlay
in config.txt.
To remove it we need to remove if from the post-image.sh script as parameter to the above script.
In raspberry pi 3 the ttyAMA0 was assigned to bluetooth and the pi3-miniuart-bt restored it back to be serial com port, which disconnects the bluetooth again.
To summarize the steps in buildroot.
Add the bluetooth firmware.
Add bluez 5 utils.
Change the package from bluez 5.44 to 5.45
Add the above sleep. (delete the .stamp_build in bluez to build it again)
remove the /dev/ttyAMA0 from cmdline.txt
After building, check on the target (pi3)
the BCM4343 hcd firmware
hci_uart.ko should be there
hci_uart is not running in case of busybox init (only in systemd)
so just modprobe hci_uart. run lsmod to see that it is running.
To start the service and load the firmware, use hciattach:
hciattach /dev/ttyAMA0 bcm43xx 921600 noflow -
Start the bluetoothd daemon:
/usr/libexec/bluetooth/bluetoothd &
Power up the bluetooth hardware using bluetoothctl (power on and agent on).
Now you should see it using hcitool dev.
Again, if you don't remove the overlay you will get timeout.
That it. the hci device should be seen now.
you can see it using:
rfkill list ( install it from busybox-menuconfig)
hciconfig -a
The above steps does not get the console to work properly on the Pi 3 but this is a different story.
As described in the commit adding the firmware, you unfortunately need to patch bluez5_utils to get the firmware to work:
https://git.buildroot.net/buildroot/commit/?id=1f94f1aaab2e786542a83327358570a828d68f45

Pulseaudio no audio via bluetooth a2dp connection buildroot

Edit:
I discovered, that if I run parec | paplay --raw it does play back properly.
How is this possible? There should be just a simple module-loopback error or something like that.
I´m using the CHIP SBC from getchip.com to build an embedded bluetooth audio receiver.
getchip.com offers a debian linux and a customizable buildroot distribution.
I got everything working on the debian distribution with one exception. The volume control with iOS devices don´t work because of the lack of uinput inside the debian kernel.
That´s why i tried to get it to work with the buildroot distribution. It took me a while to get bluetoothd running with simple-agent, as well as pulseaudio built with bluetooth support and the dbus configuration.
I finally got everything ready.
But:
I can connect to the sbc, but there is no sound playing on the headphone jack.
My debugging steps so far:
I´m able to play a local .wav file via paplay
just fine.
I allowed every communication via dbus
the pulseaudio logs show, that sound is coming from the bluetooth source.
the module-switch-on-connect does spawn a loopback from the bluetooth source to the same audio sink as paplay uses.
Debug Logs:
Boot log
Pulseaudio verbose startup
/usr/bin/pulseaudio -vvvv --system --daemonize --disallow-exit
Bluetoothd verbose connection
/usr/libexec/bluetooth/bluetoothd -C -d &
and the bt-agent:
/usr/bin/bt-agent -d -c NoInputNoOutput -p /home/cubbo/.config/bluetooth-default-pin
Pulseaudio verbose connection
Configuration:
Pulseaudio system.pa
Pulseaudio daemon.conf
dbus system.conf
dbus-1/system.d/bluetooth.conf
dbus-1/system.d/pulseaudio-bluetooth.conf
bluetooth/audio.conf
bluetooth/main.conf
I´m pretty sure that I just miss some little configuration and it´ll play just fine. But after nearly one week of debugging I wanted to ask you for help.
I hope I´ve done everything right with attaching the logs via pastebin.
I would appreciate your help!
Thanks!
Greetings, Phil.
I had a similar problem on Antergos, A2DP was not available in pulseaudio.
A2DP was available only when restarting bluethooth service by "systemctl restart bluethooth"
It resulted to be a problem caused by GDM that opened another instance of pulseaudio and captured the bluethooth device. I prevented pulseaudio capturing bluethooth device by masking it for GDM user.
https://wiki.archlinux.org/index.php/Bluetooth_headset#Connecting_works.2C_but_I_cannot_play_sound
$ mkdir -p ~/.config/systemd/user
$ ln -s /dev/null ~/.config/systemd/user/pulseaudio.socket
[restart required]

Scan networks in AP mode (hostapd)

I want to scan networks when our devices is in Access Point. When the device is in client mode, it works, but i cannot scan networks if hostapd is running as Access Point:
Try 1:
pi $ sudo iwlist wlan0 scan
wlan0 Interface doesn't support scanning : Operation not supported
Try 2:
pi $ sudo iw dev wlan0 scan
command failed: Operation not supported (-95)
Any ideas?
On Raspberry PI/wheezy, you may need to get the libnl library:
apt-get install libnl-dev
I had the same problem. So I added ap-force to the command:
sudo iw dev wlan0 scan ap-force
and that worked for me.
SOLUTION:
Download iw v4.X from:
https://kernel.org/pub/software/network/iw/
Compile it and run:
./iw dev wlan0 scan ap-force
it works!

Bluetooth Low Energy in C - using Bluez to create a GATT server

I'm attempting to use GATT on my Linux box to set up a custom service with a load of characteristics.
Using this question, and the ones it links to, I was able to identify the code that I would need to write (making use of the gatt_service_add() function).
I've created a separate file called gatt_service.c, and written the code I think that I need. However, I cannot work out how to link my code to the Bluez libraries in order to compile and run my solution. For example this site (whilst not being for BLE development) links against libbluetooth using -lbluetooth as a gcc parameter, and I cannot work out what to link against to make my code work.
I haven't posted any samples because I'm not sure what to post - if there is any that is required, or I've failed to mention something, please let me know.
Thanks in advance.
EDIT - more information:
Following the comments, I've used plugins/time as a base to write my own file for my own "Broadcaster service". Full code located: here (I don't know which bit of the code to put in the answer!).
My compilation command is: gcc gatt_broadcaster_service.c -Wall -o gatt_broadcaster_service -lbluetooth 'pkg-config --cflags --libs glib-2.0' -I/home/alexander/Documents/bluez-5.29/lib (including the glib bit to fix the issue reported here).
The error I get is: gatt_broadcaster_service.c:11:27: fatal error: lib/bluetooth.h: No such file or directory #include "lib/bluetooth.h"
My C file is stored in Documents, and my research tells me that it can't find lib/bluetooth.h because it's not looking in the correct place (this and this talk about using include flags for the compiler when a file isn't in the general locations, but I can't make that work.
Thanks again!
I got the example GATT server running for BlueZ 5.31 (latest as of this post):
My environment:
Vagrant
Virtual Box
Ubuntu Trusty Tahr as a guest OS (v. 14.04 32-bit OS)
Updated to linux kernel 3.19
Installed packages:
* libglib2.0-dev
* libdbus-1-dev
* libudev-dev
* libical-dev
* libreadline-dev
Downloaded BlueZ 5.31 from here: https://www.kernel.org/pub/linux/bluetooth/bluez-5.31.tar.xz
Installation of updated kernel (v. 3.19):
sudo apt-get update
sudo apt-get install --install-recommends linux-generic-lts-vivid
A reboot is necessary. I'm using Vagrant and lost shared folder access. If this happens to you, wait for vagrant to report the error and go into the VM anyway (vagrant ssh). In the VM, issue this command to fix the shared folder problem:
sudo /etc/init.d/vboxadd setup
I would reboot again (probably not necessary), to check that the shared folder is active again.
Once back in the VM, continue the installation of BlueZ 5.31:
cd ~
sudo apt-get install libglib2.0-dev libdbus-1-dev libudev-dev libical-dev libreadline-dev
wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.31.tar.xz
tar xvf bluez-5.31.tar.xz
cd bluez-5.31
./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --disable-systemd --enable-experimental --enable-maintainer-mode
make
sudo make install
sudo cp attrib/gatttool /usr/bin
Installation completed. Check it as follows:
hciconfig
You should get the follow response (or something similar):
hci0: Type: BR/EDR Bus: USB
BD Address: 00:1A:7D:DA:71:0C ACL MTU: 310:10 SCO MTU: 64:8
DOWN
RX bytes:15528 acl:126 sco:0 events:683 errors:0
TX bytes:6459 acl:146 sco:0 commands:234 errors:0
Configure the Bluetooth adapter, start advertising, start example GATT server (heart rate service) with verbose on (my adapter is hci0):
cd BlueZ 5.31 directory
sudo tools/btmgmt -i hci0 power off
sudo tools/btmgmt -i hci0 le on
sudo tools/btmgmt -i hci0 connectable on
sudo tools/btmgmt -i hci0 name "some friendly name"
sudo tools/btmgmt -i hci0 advertising on
sudo tools/btmgmt -i hci0 power on
tools/btgatt-server -i hci0 -s low -t public -r -v
Go to another device (I've used an iPod, an Android -- Samsung Galaxy 5S and Nexus tablet -- and another PC running BlueZ) and connect to the service. Here is how I did it on another PC running BlueZ:
gatttool -b MAC address of GATT server -I
connect
primary
characteristics
You can issue other commands to read and write to the GATT server.
I've also created a custom GATT server (your original request) by copying and editing this file: tools/btgatt-server.c. You can edit the Makefile.tools file to include your custom server in the build. You'll have to run automake, make, and sudo make install to get it running.
1) goto Bluez folder
2) sudo ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --disable-systemd --enable-experimental --enable-maintainer-mode
3) sudo make all
4) Advertise connectable packets
activate bluetooth
sudo hciconfig hci0 up
set advertise data: "hello world"
sudo hcitool -i hci0 cmd 0x08 0x0008 48 45 4c 4c 4f 57 4f 52 4c 44
start advertising as connectable
sudo hciconfig hci0 leadv 0
5) sudo service bluetooth stop
6) sudo src/bluetoothd -d -n
7) From other PC, type (Change MAC id gatt server mac)
gatttool -b gatt_server_mac --interactive
step 6 is for in case you want to compile plugins/gatt-example.c
if you want to compile server.c from profile/time or profle/alert(replace with alert in place of time) or anyother file in profile folder replace step 6
sudo src/bluetoothd --plugin=time -n
I landed on this question a few times in my search for a standalone GATT server for C. The answers here require building your GATT server with the BlueZ stack. BlueZ added D-Bus support to allow standalone GATT servers and even includes a standalone sample GATT server for Python, but not for C.
Still needing a server, I started working on one with the intention of releasing a sample, but even a minimum implementation is still more code than I would be comfortable calling a sample.
The initial implementation is done now and has been released under LGPL (edit: the license was changed from GPL-3 to LGPL):
https://github.com/nettlep/gobbledegook
If it helps somebody, cool. If it helps somebody who has released code that has helped me, even better.
ya creating a time server is good to start with .profile/time has good custom example,clean-up code and try to maintain a minimal code and run with gatttool as client.If you want both a custom client and custom server than you can see tools/btgatt-client.c and tools/btgatt-server.c .
Run tools/btgatt-server.c in one pc with sudo ./btgatt-server in one pc
and sudo ./btgatt-client -d server_mac in another pc

Linux script for Bluetooth operation

I want to automate bluetooth operation on my Ubuntu system.
Is there any script or application which I can use to perform basic Bluetooth operations like enable, discover, file transfer and disable.
You can enable your bluetooth stick by running
$ hciconfig hciX up
where hciX is a bluetooth device name (e.g hci0)
To obtain a list of devices, just type
$ hciconfig -a
(http://linux.die.net/man/8/hciconfig)
With the BlueZ package installed, you can discover devices nearby using
$ hcitool inq
(http://linux.die.net/man/1/hcitool)
If you wish to use OBEX transfer (the protocol used while sending a file from a bt device to another), you should install the OpenOBEX package with which you may type
$ obexftp -b 00:11:22:33:44:55 --put /some/file/to.put
to send /some/file/to.put to the device with bt address 00:11:22:33:44:55
(http://linux.die.net/man/1/obexftp)
Finally, if you would like to disable your bluetooth adapter, just call
$ hcitool hciX down
UPDATE
The following packages should be installed on your system in order to have the commands listed above available. Some of them may involve kernel extensions (like additional modules).
BlueZ (http://www.bluez.org/)
OpenOBEX (http://sourceforge.net/projects/openobex/)
Regards

Resources