# Host Bluetooth Connection Problems

**URL:** <https://forums.balena.io/t/host-bluetooth-connection-problems/93327>\
**Category:** balenaOS\
**Created:** [April 13, 2020, 8:43pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327 "2020-04-13T20:43:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 13, 2020, 8:43pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/1 "2020-04-13T20:43:04Z")

</div>

Hi,

I am new to using Balena and have hit an odd problem I’m hoping others may have solved. I have setup a single container application on Balena. The core of my application is written in Python 2.7. I have been able to build, deploy and run my application so far with minimal changes to the core code (which ran bare metal on RPI3). While the application runs fine for a period of time, eventually the container stops getting connections to Bluetooth devices and throws connection exceptions. If I restart the app container the problem continues. If I restart the host, the problem clears. It appears to happen after a period of time running the container, perhaps exhausting underlying connections at the OS level? My application is designed to retry BT connections (they are inherently unreliable with some devices, therefore catch a failure, close connection and retry is standard practice.). I am using the latest version of the BluePy library ([https://github.com/IanHarvey/bluepy](https://github.com/IanHarvey/bluepy)). Running in a bare metal configuration this library has not had connection problems, it just started w/the deployment to Balena. One thing that may be unique to my configuration is that I use Python multiprocessing to spawn sub processes that can run in parallel. I use process forking instead of threading due to some underlying behaviors of a library I’m using - has a bad habit of leaving abandoned sockets open. My using sub processes the orphans are destroyed w/the sub processes on exit. That approach works fine on bare metal, not sure what is happening w/balena under the covers when using this and potentially a problem w/BT connections? Any help to figure out what is happening would be appreciated.

job\_thread = multiprocessing.Process(target=job\_func)  
job\_thread.start()

---

<div class="post-metadata">

**Author:** ![anujdeshpande](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/anujdeshpande/32/3155_2.png) [@anujdeshpande](https://forums.balena.io/u/anujdeshpande)\
**Post date:** [April 14, 2020, 9:23am UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/2 "2020-04-14T09:23:18Z")

</div>

Hi

Have you tried the exact same method for BT under Raspbian? I am a little confused about what you meant - I couldn’t understand if you meant sub-processes in general, or sub-processes for BT related stuff work fine on Raspbian.  
If that’s the case, this could be an interesting issue with how balena works.

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 14, 2020, 12:03pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/4 "2020-04-14T12:03:48Z")

</div>

Hi,

Yes, the same exact code runs under Raspbian without issue. What I mean by subprocesses is that the module of code that interacts w/bluetooth is run as a subprocess instead of a thread. See more about subprocess vs. threads here [https://docs.python.org/2/library/multiprocessing.html](https://docs.python.org/2/library/multiprocessing.html)

---

<div class="post-metadata">

**Author:** ![robertgzr](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/robertgzr/32/1387_2.png) [@robertgzr](https://forums.balena.io/u/robertgzr)\
**Post date:** [April 14, 2020, 12:13pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/5 "2020-04-14T12:13:26Z")

</div>

How did you setup the bt connectivity on balena. I assume you found some of the examples for how to expose host dbus to the container?

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 14, 2020, 12:30pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/10 "2020-04-14T12:30:56Z")

</div>

I’m still new to Balena, not sure what you mean. Below is the device configuration I see after the app is deployed to the container. Is three additional parameters I should set in the Dockerfile or compose file? Keep in mind, this works fine for a period of time, then the host stops allocating connections.

 ![Screen Shot 2020-04-14 at 8.27.55 AM](https://global.discourse-cdn.com/balena/original/2X/b/b9eb54bf137d4fc7781613033f2c2a8a792a02c9.png)

---

<div class="post-metadata">

**Author:** ![robertgzr](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/robertgzr/32/1387_2.png) [@robertgzr](https://forums.balena.io/u/robertgzr)\
**Post date:** [April 14, 2020, 12:33pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/11 "2020-04-14T12:33:32Z")

</div>

Maybe have a look at the bluetooth-audio section of the balena-sound project. [https://github.com/balenalabs/balena-sound/tree/master/bluetooth-audio](https://github.com/balenalabs/balena-sound/tree/master/bluetooth-audio)

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 14, 2020, 12:54pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/14 "2020-04-14T12:54:15Z")

</div>

Thanks for sending over. That is using a different BT library and connection model is very different (incoming audio connection to RPI vs. outbound connection model for my app.) The only real difference I see is the base image it starts with vs. the python image I am using. I have tried the Balena Python 32 and 64 bit image and experience the same issue. I’ll try the alternative base image, any other ideas out there?

---

<div class="post-metadata">

**Author:** ![robertgzr](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/robertgzr/32/1387_2.png) [@robertgzr](https://forums.balena.io/u/robertgzr)\
**Post date:** [April 14, 2020, 1:03pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/17 "2020-04-14T13:03:22Z")

</div>

Hm, I was more thinking of how your container is set up to talk to the bluetooth subsystem on the host os. I had a quick look at bluepy and saw that they use bluez (the bleutooth-dbus interface) too.

Your container will need access to the host’s dbus specifically, that’s what is happening [here](https://github.com/balenalabs/balena-sound/blob/master/docker-compose.yml#L9) and [here](https://github.com/balenalabs/balena-sound/blob/master/bluetooth-audio/Dockerfile.template#L3)

From your description it sounds like something about the configuration makes it so that the container will not be notified when the BT device goes away and comes back. This kind of problem usually points to some problem with how udev is set up, since docker doesn’t really take care of dynamically appearing devices when you’re not running the container as `privileged`.

Maybe you can share the host-level bluetoothd logs with us from when the device is in the fail-state, that might tell us a bit more about what’s going on…

On a side note: you might want to look at moving your project over to python 3 since we will be deprecating the python 2 support from our official base images in the coming months.

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 14, 2020, 1:24pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/18 "2020-04-14T13:24:10Z")

</div>

Still new to how Balena works - where is the log file on the host OS? My host is in a failure loop right now, perfect timing.

I have "io.balena.features.dbus: “1"” set in my docker-compose under label and I have “privileged: true” set under the service definition. I do not have the ENV variable set, will try that next.

---

<div class="post-metadata">

**Author:** ![robertgzr](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/robertgzr/32/1387_2.png) [@robertgzr](https://forums.balena.io/u/robertgzr)\
**Post date:** [April 14, 2020, 1:30pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/19 "2020-04-14T13:30:07Z")

</div>

You can login to the host via the dashboard terminal or the balena-cli. Then it works just like on raspbian via `journalctl --no-pager -u bluetooth.service`

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 14, 2020, 1:46pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/20 "2020-04-14T13:46:17Z")

</div>

> [@robertgzr](#):
>
> journalctl --no-pager -u bluetooth.service

no log files?  
root@6c9ca41:~# journalctl -u bluetooth.service  
– Logs begin at Mon 2020-04-13 17:58:06 UTC, end at Tue 2020-04-14 13:45:31 UTC. –  
– No entries –  
root@6c9ca41:~#

---

<div class="post-metadata">

**Author:** ![tmigone](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/tmigone/32/2814_2.png) [@tmigone](https://forums.balena.io/u/tmigone)\
**Post date:** [April 14, 2020, 3:26pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/22 "2020-04-14T15:26:41Z")

</div>

Hi Jeff, is this with both the dbus label and env var set?

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 14, 2020, 3:38pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/23 "2020-04-14T15:38:16Z")

</div>

Right now only w/the dbus label set. I am creating a test harness to replicate the error condition. once I have the harness and proven use case to create failure will start introducing new variables - or start by setting the env. var.

---

<div class="post-metadata">

**Author:** ![tmigone](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/tmigone/32/2814_2.png) [@tmigone](https://forums.balena.io/u/tmigone)\
**Post date:** [April 14, 2020, 3:58pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/24 "2020-04-14T15:58:40Z")

</div>

Okay that sounds good. What I can tell you is that if your software requires dbus to talk to the bluetooth service then for sure you will need both (label and env var) for it to work. Keep us posted on your findings!

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 15, 2020, 1:16am UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/26 "2020-04-15T01:16:25Z")

</div>

I’m seeing this error in the logs after running the regression for \<100 iterations. This is without the env. setting above - will add that next. This error looks like a an open issue with some kernels, have you seen it before?

Apr 15 01:10:44 a9901ab c2afe69795e2[814]: [58B blob data]  
Apr 15 01:10:51 a9901ab kernel: Bluetooth: hci0: Frame reassembly failed (-84)  
Apr 15 01:10:51 a9901ab kernel[722]: [2664.643213] Bluetooth: hci0: Frame reassembly failed (-84)  
Apr 15 01:10:53 a9901ab kernel: Bluetooth: hci0: command 0x0406 tx timeout  
Apr 15 01:10:53 a9901ab kernel[722]: [2666.722774] Bluetooth: hci0: command 0x0406 tx timeout  
Apr 15 01:11:44 a9901ab c2afe69795e2[814]: [58B blob data]  
Apr 15 01:12:20 a9901ab 1a3e92352b25[814]: [api] GET /v1/healthy 200 - 9.000 ms

---

<div class="post-metadata">

**Author:** ![richbayliss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/richbayliss/32/1054_2.png) [@richbayliss](https://forums.balena.io/u/richbayliss)\
**Post date:** [April 15, 2020, 11:52am UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/29 "2020-04-15T11:52:17Z")

</div>

Which BalenaOS version are you using?

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 15, 2020, 12:11pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/30 "2020-04-15T12:11:42Z")

</div>

2.47.0

The host is in failure mode now. There is a new error in the host logs that coincides w/the container requests for BT connection. Here is the host exception being thrown. Interesting note, the application code is trying to connect to 2 unique devices, but sequentially (connect, get characteristics, disconnect, try next device.). What’s interesting is the device is not able to connect to 1 of the 2 devices, every time it tries to connect the error below is in the host log file and the application BT connection code fails. When the code attempts to connect to the 2nd device it works as expected and no error is throw in the host log.

Apr 15 12:09:45 a9901ab kernel[722]: [42199.013931] [\<8013c114\>] (process\_one\_work) from [\<8013d20c\>] (worker\_thread+0x60/0x5b8)  
Apr 15 12:09:45 a9901ab kernel[722]: [42199.022141] [\<8013d20c\>] (worker\_thread) from [\<80142cec\>] (kthread+0x16c/0x174)  
Apr 15 12:09:45 a9901ab kernel[722]: [42199.029645] [\<80142cec\>] (kthread) from [\<801010ac\>] (ret\_from\_fork+0x14/0x28)  
Apr 15 12:09:45 a9901ab kernel[722]: [42199.036967] Exception stack(0xb9505fb0 to 0xb9505ff8)  
Apr 15 12:09:45 a9901ab kernel[722]: [42199.042087] 5fa0: 00000000 00000000 00000000 00000000  
Apr 15 12:09:45 a9901ab kernel[722]: [42199.050381] 5fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  
Apr 15 12:09:45 a9901ab kernel[722]: [42199.058673] 5fe0: 00000000 00000000 00000000 00000000 00000013 00000000  
Apr 15 12:09:45 a9901ab kernel[722]: [42199.065434] kobject\_add\_internal failed for hci0:64 with -EEXIST, don’t try to register things with the same name in the same directory.  
Apr 15 12:09:45 a9901ab kernel: Bluetooth: hci0: failed to register connection device  
Apr 15 12:09:45 a9901ab kernel[722]: [42199.077908] Bluetooth: hci0: failed to register connection device  
Apr 15 12:09:47 a9901ab 1a3e92352b25[814]: [api] GET /v1/healthy 200 - 15.794 ms

---

<div class="post-metadata">

**Author:** ![richbayliss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/richbayliss/32/1054_2.png) [@richbayliss](https://forums.balena.io/u/richbayliss)\
**Post date:** [April 15, 2020, 1:43pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/31 "2020-04-15T13:43:44Z")

</div>

Could you provide the output from `cat /etc/os-release` as this will give us the full versions 👍

---

<div class="post-metadata">

**Author:** ![jdr](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jdr](https://forums.balena.io/u/jdr)\
**Post date:** [April 15, 2020, 1:46pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/32 "2020-04-15T13:46:53Z")

</div>

root@a9901ab:~# cat /etc/os-release  
ID=“balena-os”  
NAME=“balenaOS”  
VERSION=“2.47.0+rev1”  
VERSION\_ID=“2.47.0+rev1”  
PRETTY\_NAME=“balenaOS 2.47.0+rev1”  
MACHINE=“raspberrypi3”  
VARIANT=“Development”  
VARIANT\_ID=“dev”  
META\_BALENA\_VERSION=“2.47.0”  
RESIN\_BOARD\_REV=“a08e3d7”  
META\_RESIN\_REV=“8b48eee”  
SLUG=“raspberrypi3”

---

<div class="post-metadata">

**Author:** ![richbayliss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/richbayliss/32/1054_2.png) [@richbayliss](https://forums.balena.io/u/richbayliss)\
**Post date:** [April 15, 2020, 2:23pm UTC](https://forums.balena.io/t/host-bluetooth-connection-problems/93327/33 "2020-04-15T14:23:01Z")

</div>

So looking at this link, [https://github.com/raspberrypi/firmware/issues/1150#issuecomment-510483643](https://github.com/raspberrypi/firmware/issues/1150#issuecomment-510483643) it seems a fix for this issue was pushed to the firmware repo. I am trying to work out if the commit of the firmware we use in this OS version has the patch.

For the records; the firmware commit we use is `7163480fff007dc98978899b556dcf06f8a462c8` dated `20190823`.

[Next page](https://forums.balena.io/t/host-bluetooth-connection-problems/93327.md?page=2)
