Read battery status bluetooth headset linux - linux

I use Bluetooth headset that use Bluetooth version 4.1. When I connect with android it shows the battery status. But with Linux (Ubuntu 18.04) I cannot get battery status. I tried with bluetoothctl, looking file in /sys/class/power_supply as in other questions, But they didn't help me.
askubuntu.com/questions/53880/is-there-any-way-to-check-the-battery-percentage-of-apple-wireless-peripherals
stackoverflow.com/questions/49078659/check-battery-level-of-connected-bluetooth-device-on-linux
My bluetooth device don't use GATT profile. It uses A2DP sink for streaming audio.
I looked Bluez documentation. They all said is do with GATT profile and get the attributes.
Is there a way to read battery status even with pragmatically in Linux properly? How does android device get the battery status? Is it a weakness of Linux Bluetooth stack?

Since this pull request by Dmitry Sharshakov, PipeWire has support for reporting battery status (with devices that use Apple HFP AT commands). It uses bluez's Battery Provider API, which is still experimental and is only available if bluetoothd is started with the -E flag.
On Arch Linux, it should be enough to run
cp /usr/lib/systemd/system/bluetooth.service /etc/systemd/system/
sed -i -r 's/ExecStart=.+/& -E/' /etc/systemd/system/bluetooth.service
systemctl daemon-reload
systemctl restart bluetooth
And the headset battery level should appear. To get the battery level programatically, you can then use UPower's DBus API.

Related

OpenSUSE 15.0 LEAP pulseaudio unable to detect bluetooth speaker

I would like to stream my music through my Bluetooth speaker but PulseAudio can't see the speaker. When I open the sound settings (pavucontrol) I can't change the output device to my speaker since it's not displayed.
What I already checked
The speaker is paired and connected through the Bluetooth interface in GNOME. I verified the connection using the bluetoothctl tool and made sure that the device is connected and trusted by OpenSUSE.
I checked if PulseAudio can see the device through the command line using pactl list sources short and pactl list sinks short and it's not there either.
I verified if PulseAudio has the right Bluetooth addon installed and it has.
Removing the configuration in ~/.config/pulse doesn't help
This question: Pulseaudio not detecting bluetooth headset couldn't help me either.
It worked a couple months ago without any issue but now it doesn't. Any advice to force PulseAudio to recognize my Bluetooth speaker?
Setup:
UE Boom Bluetooth speaker
GNOME OpenSUSE Leap 15.0
Pulseaudio 11.1
BlueZ 5.48
Try these steps to resolve the bluetooth audio issue on Opensuse Linux- These would work in ubuntu and other linux too. I am running on Opensuse linux 15.
First comment out the line
load-module module-bluetooth-discover
by editing the
sudo vim /etc/pulse/default.pa
Now you would need to reboot after this change is done. After rebooting,
we configure the bluetoothctl by running the following sequence of commands in a terminal:
> pactl load-module module-bluetooth-discover
> bluetoothctl
>> power on
>> agent on
>> default-agent
>> discoverable on
>> scan on
scanon will return with the name of the bluetooth speaker along with a code in the format '11:11:11:11:11:11' - thats ur bluetoooth speakers id. Note that down and use it in the next command trust and connect.
>> trust 11:11:11:11:11:11
>> connect 11:11:11:11:11:11
>> scan off
Do not close this terminal.
And these are all thats required to connect to a bluetooth speaker/headphone.
YOu can put the above in a script say - connectbluettooth.sh and run it everytime you login.
Last step change the speakers settings, your applications uses by running the below command:-
pavucontrol - Change the default audio device to the bluetooth speaker.
You can see my bluetooth speaker - 'Juarez' set in the pavucontrol tool in the screenshot.

Analyzing Serial data over USB on Linux

I have a USB to RS232 adapter which I'm currently using to communicate with a legacy device. I want to analyze the data going to and from the legacy device from my Ubuntu machine over USB. The adapter is getting detected automatically without installing any driver as shown in lsusb output below:
Bus 006 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
I tried using the USB packet capture in Wireshark, but it shows a bunch of URB_CONTROL, URB_BULK packets which is not showing anything meaningful. So if there are any better utilities available which can capture the USB2Serial data for a Linux system, please let me know. TIA!
After trying various methods, I finally got what I needed using strace. The following command helped me capture all the termios settings for the serial port and also all the IOCTL calls after doing some greps on the file handle of the serial port:
sudo strace -p <pid> -t -v -o dump.txt
This will dump all the system calls that your application makes on Linux into the file dump.txt.

How to implement Bluetooth LE with Bluez in Linux

I'm working on setting up two Linux systems for a BLE demo. Obviously one system will be the peripheral and one will be the central devices. I have several questions surrounding both of these configurations.
Environment
2x Ubuntu 14.04 systems
2x Pluggable USB-BT4LE dongles (http://plugable.com/products/usb-bt4le)
Peripheral Device Setup
The first order of business is getting the peripheral system setup and advertising with a GATT server configured. At this time, it does not seem possible to configure a GATT server from the command line. So, while it is a simple task bringing a USB dongle up and advertising it, this does not allow the creation of custom services and characteristics. The only example of a GATT server I could find was the gatt-example.c file in the Bluez package. So I downloaded and built the latest bluez-5.23 source. (http://www.linuxfromscratch.org/blfs/view/svn/general/bluez.html). Additionally configured using the --enable-maintainer-mode flag to force build the gatt-example.c plugin into bluetoothd. And I validated post-build from the ~/bluez-5.23/plugins directory that there was a bluetoothd-gat-example.o file. Which tells me that the gatt-example was at least successfully built.
I then modified the configuration file to enable LE and the attribute server.
$ sudo vi /etc/bluetooth/main.conf
EnableLE = true // Enable Low Energy support. Default is false.
AttributeServer = true // Enable the GATT attribute server. Default is false.
Then simply reboot or restart the bluetooth daemon...
Central Device Setup
As the central device does not need any special plugins built like the peripheral, I just installed bluez using apt-get. This appears to have installed v4.101 according to bluetoothd -v.
Session Setup
The connection process then should be fairly simple. I setup the peripheral to advertise and then connect with the central device:
Peripheral:
$ sudo hciconfig hci0 up // Make sure the interface is up
$ sudo hciconfig hci0 leadv // Set the interface to advertise
Central:
$ sudo hcitool -i hci0 lescan // Scan for nearby devices advertising
LE Scan ...
00:02:72:C9:5E:0F (unknown) // Not sure why two of the same MAC are found?
00:02:72:C9:5E:0F (unknown) // but I know this is my device...
$ sudo gatttool -i hci0 -b 00:02:72:C9:5E:0F -m 48 --interactive // Connect interactively
[ ][00:02:72:C9:5E:0F][LE]> connect
[CON][00:02:72:C9:5E:0F][LE]> primary
attr handle: 0x0001, end grp handle: 0x0008 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0010, end grp handle: 0x0010 uuid: 00001801-0000-1000-8000-00805f9b34fb
[CON][00:02:72:C9:5E:0F][LE]> characteristics
handle: 0x0004, char properties: 0x02, char value handle: 0x0006, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0007, char properties: 0x02, char value handle: 0x0008, uuid: 00002a01-0000-1000-8000-00805f9b34fb
And we see not one of the services or characteristics from the gatt-example are available.
Questions
--Peripheral Device
How would I go about creating my own custom GATT server? Can it be a stand-alone C application or does it need to be built into bluetoothd as a plugin like the gatt-example? The answer to this question (Creating a Gatt Server?) implies you do the following: "start by initializing the GATT library and additional modules" and then "register your GATT database". But there isn't a single example of how to implement those generic statements and the link provided is simply a URL to the Bluetooth website.
The GATT specifications (https://developer.bluetooth.org/gatt/Pages/default.aspx) provide numerous "adopted" services and characteristics that are downloadable in XML format. But there is no instructions for how to use them?!
How do a validate my GATT server is running?
--Central Device
Why is my central device not seeing the services and characteristics from the GATT server running on the peripheral?
I can provide any additional information necessary. Thanks.
To create a GATT server into a separate process you have (at least) two cases:
Bluez v4.x: your GATT service must be a Bluez plugin
Bluez v5.x: your GATT service should use the new GATT DBus API (but it is recommended to use at least Bluez v5.39 (from April 2016). Otherwise it is safer (in term of Bluez GATT Server API) to use the Bluez v4.x plugin approach.
If your Central Device does not see the newly exported GATT service is probably an issue on the periphal rather than to be an issue on the Central Device.
When you will need to implement the GATT client on the Central Device you still have two cases:
Bluez v4.x: Bluez does not expose the GATT API. Either you use a shell script to launch gatttool commands, or you use a GATT library such as gattlib to interact with the BLE device
Bluez v5.x: Same thing, if you cannot migrate to Bluez v5.39 then it is better to use Bluez v4.x methodology.

Using Bluetooth low energy in linux command line

I am working on using the Bluetooth low energy modem with Linux. I am using the command line option for that i.e. hcitool . I am able to the find the devices using the command: $ hcitool scan
It is working fine for me, also I am able to broadcast my device using :
sudo hciconfig hci0 leadv
It is also working fine. But I want to add the services and characteristic to the modem device which can be detected by other device. I have tried sdptool add but it is not working for me. Does anyone know how to add the services and characteristics to the peripheral using the command line tools in ubuntu?
Edit: My modem is broadcasting but not able to explore the services and characteristic to the other BLE device. Now I am able to set the name of device using hcio name command
Edit: Now I am able to render the services and characteristic, by simultaneously running sudo hcidump command. But I am not able to track from where I am getting those services and characteristics. One definite observation is those services are rendering from the machine.
SDP is absent in BLE. Broadcast/advertise frame and GATT client/server are used instead.
Several links:
BlueZ gatttool: command line tool to run common GATT procedures
BlueZ GATT's ready profiles
hint: DBUS
GATT and DBUS example
How can I connect to the FitBit Zip over Bluetooth 4.0 LE on Linux with bluez?
Bluetooth Low Energy: listening for notifications/indications in linux
http://comments.gmane.org/gmane.linux.bluez.kernel/29547
I used to broadcast BT services by following this article. This page not only shouws you how to advertise a profile, but also gives you an example on how to implement the HSP profile.
To know the bt class you need to announce, you can check this other page.

Bluetooth Dongle and iOS Simulator

I have bought an IOGEAR GBU521 bluetooth dongle as per the instruction on Apple's technote:
http://developer.apple.com/library/ios/#technotes/tn2295/_index.html
I have also configured it as instructed and I'm trying to get the temperaturesensor demo to work but the Bluetooth under settings never seems to turn on (keeps spinning). Looking at the console, here is what I see:
2012-11-02 4:17:08.114 PM Preferences[862]: BTM: setting discoverable status enabled
2012-11-02 4:17:08.114 PM Preferences[862]: BTM: setting connectable enabled
2012-11-02 4:17:08.115 PM Preferences[862]: BTM: enabling device scanning
2012-11-02 4:17:08.115 PM Preferences[862]: BTM: failed to start scanning with error 111
Any thoughts? I've seen people ask about this but has anyone got this approach to work?
Best,
Throw away the IOGEAR and pick up a Cirago BTA8000.
I bought the IOGEAR you mention, in addition to the Cirago BTA8000, and the MediaLink 4.0 adapter. Turns out that anything based on the Broadcom chip won't work despite my MacBook having the exact same one built in, which means the MediaLink and IOGEAR don't work. My Cirago shipment just arrived this morning, and the Bluetooth slider in the iOS Simulator flicks to "ON" in less than a second.
If you want to experiment, I'd love to hear if other non-Broadcom chips work too.
I just bought a GBU521 from amazon to upgrade the BT in my MacBook8,3
I had the same problem of it not showing up. Reading the same article you referenced (TN2295) I did a sudo nvram bluetoothHostControllerSwitchBehavior="always" which tells the system to ALWAYS switch to a bluetooth controller if one is plugged in.
The default option:
when a new HCI is connected, the built-in driver only disconnects from the built in HCI and attaches to the external HCI if the new module is not an Apple module.
I presume that because the vendor IDs matched (0xa5c) that it was not switching over to the new dongle. Setting it to always causes OSX to always switch to a new BT adapter. This is confirmed by the following statement in TN2295:
Open the System Information application to verify that the system Bluetooth driver is matched to the built-in Bluetooth host controller interface (HCI). For the Hardware->Bluetooth setting, verify that the Vendor ID is "0x5AC".
Rebooting after the nvram update and I was able to re-pair all of my devices without incident.
HOWEVER this will not work for iOS BT LE development due to the following statement in TN2295:
If the system Bluetooth controller is matched to the Bluetooth LE USB adapter, then the iOS simulator will not be able to use the external Bluetooth controller for Bluetooth LE services.
Actually, you can mod the Info.plist inside the BroadcomBluetoothHostControllerUSBTransport.kext found here:
/System/Library/Extensions/IOBluetoothFamily.kext/Contents/PlugIns/BroadcomBluetoothHostControllerUSBTransport.kext/Contents/Info.plist...
Just copy another Broadcom dictionary and modify it or use the following additon to the Info.plist. You'll need to do this as an admin (i.e. sudo nano or vi this file after making a copy of the original).
<key>Add an appropriate name here</key>
<dict>
<key>CFBundleIdentifier</key>
<string>com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport</string>
<key>IOClass</key>
<string>BroadcomBluetoothHostControllerUSBTransport</string>
<key>IOProviderClass</key>
<string>IOUSBDevice</string>
<key>idProduct</key>
<integer>8680</integer>
<key>idVendor</key>
<integer>2652</integer>
</dict>
May need to reboot after the edit. Did this on 2012 MacBook Pro, ran XCode, and it allowed access to the Bluetooth dongle in iOS Simulator!
I hope this helps!

Resources