Revival of Official Bluetooth Binding

I don’t think I want to go the same route as Vlad did with regards to using a characteristic database since it is only capable of handling devices which choose to encode their data as characteristics. Quite a few devices, as you have noticed, broadcast their data or even require specific data to be sent to the device as a query.

Until I can come up with a better xml schema I think the best thing I can do right now is add features to the generic binding that would allow people to support their own devices through rules. These rules should be able to serve as a prototype bindings for devices which hopefully people will eventually turn into actual bluetooth binding extensions.

So there are a few features that I’m going to add that should be very beneficial.

  • Allow users to supply their own characteristic xml files just like Vlad’s binding allowed.
  • Add a configuration to raw characteristic channels to turn on notifications.
  • Add a connection channel and a configuration for connection-mode (ALWAYS, ON_DEMAND, NEVER, and MANUAL).
  • Add channel for received broadcast data.
  • After all of the above, then I may attempt a unified schema for defining bluetooth bindings. Which would probably be a separate binding.

Let me know if you have any additional features that you would like.

2 Likes

If the device refuses connection during discovery, or the connection fails for whatever reason, then the device will be discovered as a beacon, not a generic. Once I add support for non-connection based generic bluetooth devices then I’ll make sure that every device would be discovered as generic instead of beacon.

But in general gatt discovery requires connecting to the device and will fail if authentication is involved. The bluetooth binding currently doesn’t support handling authentication, and until I use a device that requires it, it won’t be a priority of mine to implement it.

Hi Connor,
sorry for the late reply…

Thanks for your comprehensive answer and the efforts you make for improving the Bluetooth binding :+1:

This would be great. Probably openHAB’s transformation services could come handy for doing it (XML/XPath or Javascript?) in addition to rules…

Could you share some example already, how should it look like e.g. for a yet known device from Vlad’s work?

Yes, please.

Agree too. As mentioned above, this might be essential for battery powered BLE sensors for saving precious power.

If I understood correctly, this is the main feature I’m currently missing to integrate the majority of the Xiaomi sensors again.

I assume the above give us all options we need to re-use all the BLE devices (“connectable” and “non-connectable/broadcast-only”) again as we did with 3rd party binding. I would be really happy with that!

Beside the generic devices:
One feature which could be useful is the extension of the “beacon-only” part of the binding to decode additional payload from the beacon data of the two prevalent beacon types “Apple iBeacon” and “Google Eddystone” and make them available as channels as mentioned previously:

Hi guys, feels like I missed something? Did you manage to complete working on this binding? is it working now? Can it read xiaomi bluetooth temp&Humidity sensors? What about Mi Kettle?

Hello,
I’ve read through all of the posts on this forum thread but i’m still a bit confused on what the latest procedure is to get Bluetooth working on openhabian 2.5.10.

I started by installing the Bluetooth binding from paper UI. Afterwards I installed bluez on my raspberry pi 4 with the following commands:

sudo apt-get install -y bluez python-bluez python-dev libbluetooth-dev raspberrypi-sys-mods

sudo apt-get install -y pi-bluetooth

sudo apt-get install libcap2-bin

At this point, I was able to scan for Bluetooth devices with my PI using hcitool lescan

[14:28:55] openhabian@openHABianDevice:~$ bluetoothctl
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller DC:A6:32:E1:0F:05 Discovering: yes
[NEW] Device F5:1E:F8:91:63:F3 Kontakt
[NEW] Device 52:19:C6:0E:83:F7 52-19-C6-0E-83-F7
[NEW] Device 53:08:EA:44:41:E8 53-08-EA-44-41-E8
[NEW] Device 75:8C:34:B1:48:8C 75-8C-34-B1-48-8C
[NEW] Device F0:18:98:2A:98:53 F0-18-98-2A-98-53
[NEW] Device EA:E4:EC:C2:0C:18 Kontakt
[NEW] Device 59:15:47:6D:B6:24 59-15-47-6D-B6-24
[NEW] Device 4E:AA:21:69:40:AC 4E-AA-21-69-40-AC
[NEW] Device 53:88:04:47:1B:C8 53-88-04-47-1B-C8
[NEW] Device 75:E9:02:A2:6D:93 75-E9-02-A2-6D-93

When I search for Bluetooth devices on Paper UI it cannot find any things. My Bluetooth service seems to be running fine however:

[14:44:19] openhabian@openHABianDevice:~$ sudo systemctl status bluetooth
[sudo] password for openhabian:
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2020-12-07 14:26:56 AEST; 17min ago
Docs: man:bluetoothd(8)
Main PID: 602 (bluetoothd)
Status: “Running”
Tasks: 1 (limit: 4915)
CGroup: /system.slice/bluetooth.service
└─602 /usr/lib/bluetooth/bluetoothd
Dec 07 14:26:56 openHABianDevice systemd[1]: Starting Bluetooth service…
Dec 07 14:26:56 openHABianDevice bluetoothd[602]: Bluetooth daemon 5.50
Dec 07 14:26:56 openHABianDevice systemd[1]: Started Bluetooth service.
Dec 07 14:26:56 openHABianDevice bluetoothd[602]: Starting SDP server
Dec 07 14:26:56 openHABianDevice bluetoothd[602]: Bluetooth management interface 1.14 initialized
Dec 07 14:26:56 openHABianDevice bluetoothd[602]: Sap driver initialization failed.
Dec 07 14:26:56 openHABianDevice bluetoothd[602]: sap-server: Operation not permitted (1)
Dec 07 14:26:57 openHABianDevice bluetoothd[602]: Failed to set privacy: Rejected (0x0b)

I am running bluez on version 5.5. I understand that the doc says that it needs to be an older version but I saw that @Constantinos_Contis was able to get his working with the same version

I also installed the same jar as him by running the following:

bundle:install -s https://repo1.maven.org/maven2/com/github/hypfvieh/bluez-dbus-osgi/0.1.3/bluez-dbus-osgi-0.1.3.jar

After a restart I manually added the controller to the paper ui which gave me this error in the logs

2020-12-07 14:31:22.740 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method ‘ThingHandler.initialize()’ on ‘org.openhab.binding.bluetooth.bluez.handler.BlueZBridgeHandler@18eefb1’: BlueZ JNI connection cannot be established.
java.lang.IllegalStateException: BlueZ JNI connection cannot be established.
at org.openhab.binding.bluetooth.bluez.handler.BlueZBridgeHandler.initialize(BlueZBridgeHandler.java:73) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_272]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_272]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_272]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_272]
at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_272]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_272]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_272]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_272]
Caused by: java.lang.UnsatisfiedLinkError: tinyb.BluetoothManager.getNativeAPIVersion()Ljava/lang/String;
at tinyb.BluetoothManager.getNativeAPIVersion(Native Method) ~[?:?]
at tinyb.BluetoothManager.getBluetoothManager(BluetoothManager.java:304) ~[?:?]
at org.openhab.binding.bluetooth.bluez.handler.BlueZBridgeHandler.initialize(BlueZBridgeHandler.java:68) ~[?:?]
… 10 more
2020-12-07 14:31:22.752 [ERROR] [core.thing.internal.ThingManagerImpl] - Exception occurred while initializing handler of thing ‘bluetooth:bluez:5ae775d0’: BlueZ JNI connection cannot be established.
java.lang.IllegalStateException: BlueZ JNI connection cannot be established.
at org.openhab.binding.bluetooth.bluez.handler.BlueZBridgeHandler.initialize(BlueZBridgeHandler.java:73) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_272]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_272]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_272]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_272]
at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_272]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_272]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_272]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_272]
Caused by: java.lang.UnsatisfiedLinkError: tinyb.BluetoothManager.getNativeAPIVersion()Ljava/lang/String;
at tinyb.BluetoothManager.getNativeAPIVersion(Native Method) ~[?:?]
at tinyb.BluetoothManager.getBluetoothManager(BluetoothManager.java:304) ~[?:?]
at org.openhab.binding.bluetooth.bluez.handler.BlueZBridgeHandler.initialize(BlueZBridgeHandler.java:68) ~[?:?]

I checked my HCIUART and that also seems to be running fine when I use:

sudo systemctl start hciuart

I’m kind of out of ideas on how to fix this.

When I’m trying to connect Tile in a beacon mode with Bluegiga I’m getting UNKNOWN status. Is it because is not recognized?

Yes, I didn’t use the Bluetooth binding so, but I have flashed OTA my Xiaomi temperature with that https://github.com/atc1441/ATC_MiThermometer
and read it with a script https://github.com/JsBergbau/MiTemperature2
They consume far less power.

Would be good to support ATC mode in openhab maybe?

1 Like

I looks like the old version of the bluez binding is still running. Did you update it or is the old one still installed as well? You need to make sure that you uninstall of the old one through the openhab-cli otherwise it will not work.

I haven’t tested the binding with Tile yet so I can’t say for sure. If it is stuck in UNKNOWN then that probably means that it hasn’t sensed the presence of the device yet. Bluegiga doesn’t have greatest range so make sure it is close enough.

The binding currently (as of OH3 M3) supports reading xiaomi bluetooth devices. Whether or not the devices have normal gatt channels or raw channels, however, I cannot say since I don’t have those devices.

Thanks for the quick reply! I went into the openhab-cli and removed the bluez adapter bundle. The only Bluetooth bundles I have now are these:

238 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: Bluetooth Binding
239 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: Airthings Bluetooth Adapter
240 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: AM43 Bluetooth Adapter
241 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: BlueGiga Bluetooth Adapter
243 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: Blukii Bluetooth Adapter
244 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: DaikinMadoka Bluetooth Adap
245 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: Roaming Bluetooth Adapter
246 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: RuuviTag Bluetooth Adapter
247 | Active | 80 | 2.5.10 | openHAB Add-ons :: Bundles :: ZWave Binding
248 | Active | 80 | 0.1.3 | bluez-dbus-osgi

I removed the Bluetooth controller that I had manually added before. When I search I don’t see any devices or controllers appear.

I am adding the device manually by saying that its a “connected bluetooth device” which gives me this error:

2020-12-08 16:42:59.180 [hingStatusInfoChangedEvent] - ‘bluetooth:connected:38fba18d’ changed from INITIALIZING to OFFLINE (CONFIGURATION_ERROR): Not associated with any bridge

I don’t think this is right since it should be a controller but I don’t see how I can put the Bluetooth address (DC:A6:32:E1:0F:05) when I add a device as “Roaming Bluetooth Controller” (Only lets me put in UID as far as I can see)

It’s close to Bluegiga. With the same configuration it worked with @vkolotov binding on OH2.4.
I can provide some logs if needed - just let me know what to debug.

Hi @cpmeister,
As I said, I’ve been able to connect one of my LYWSD03MMC Bluetooth thermometers. It got discovered and connected by the binding.

As I’ve feared yet:

The permanent connection drained the coin cell within few days completely…
Regarding the proposed channel for broadcast data

This seems being strongly needed for some of the BLE sensor devices which were supported by Vlad’s binding (e.g. Xiaomi MiFlower, the round Xiaomi BLE thermometer, …)

Fortunately Tukks shared the information about a custom firmware for the LYWSD03MMC

… which turns that device into a “broadcasting mode” too, meaning it may behave similar to the round Xiaomi thermometer: Just broadcasting it’s data on UUID 0x181A.

I gave it a try: I’ve flashed one of my devices and can receive now the data as descibed by the developer, see:

image

The “encoding” is documented by the developer too, so it should be easy for us to get the readings out of broadcast through a transformation or similar:

The format is like this:
Byte 5-10 mac in correct order
Byte 11-12 Temperature in int16
Byte 13 Humidity in percent
Byte 14 Battery in percent
Byte 15-16 Battery in mV uint16_t
Byte 17 frame packet counter

So curious to hear: Are you working on the received broadcast data feature already?

I’m moving in that direction, before I can create that feature I need to finish the connection-mode feature which I’m currently working on.

3 Likes

Is there something what I can debug to verify if beacon is visible?

Has anyone been able to successfully pair the Raspberry Pi Bluetooth to OpenHAB as a controller? I still can’t seem to get it when scanning for new Bluetooth devices. Can it be added manually?

Guys, checked latest binding, still no success with any data from connected devices, only RSSI…(( I’m using BLE Xiaomi humidity&temperature sensors flashed with custom firmware. Continue using ESP32 bridge to MPPT to get those…

I did not get bluetooth working well untill i upgraded to the brandnew openhab v3.
Am using debian x86 and tried downgrading to bluez 5.43 and 5.47.
I am now on bluez 5.50 and openhab v3, and got my three am43’s working like a charm by just updating bluez and openhab to v3.

I too have got 2 am43 that I intend to connect with OH for my home automation. May I ask you to explain in detail how you got them working? Assume I am an idiot. Both my am43 blind motors are still in the box, although I tried once with OH 2.5 and I just could not get it working.

much thanks, and compliments of the season!

The problem is i don’t know what i exactly did, since i tried a lot and only started working after upgrading to openhab v3. I don’t know what you are running, i assume it is debian linux.
If you are running a raspberry there probably is a bluetooth howto on the internet.

But here is what i think you should do:

  1. Make sure your usb bluetooth is supported by linux.

  2. Install bluez if not installed.
    There is a lot of info to downgrade bluez to a version betweer 5.43 and 5.47. This is not necesary anymore with openhab v3.
    Try this manual:
    Bluetooth on Debian or Ubuntu - Some Dude Says

  3. Follow this manual to configure for openhab:
    Bluetooth BlueZ Adapter - Bindings | openHAB

  4. I would backup your current openhab config.

  5. Upgrade to openhab v3.
    Guide: Installing and testing OH3 snapshots/milestones using APT and YUM

  6. Add the bluetooth binding using the UI if not already there.

  7. I already did a manual setup with openhab v2.xx which is a hassle, and openhab v3 picked it up just fine, but openhab 3 should just autodiscover the am43.

2 Likes