Programming a BLE server and a client both in a Raspberry Pi - bluetooth

I want to implement a BLE in a Raspberry which sends the result of a sensor apart from it's characteristics and make another Raspberry to obtain that data.
Because the language that offers the possibility to read data from the sensor is written in C, C++ and Python, I have been searching through multiple libraries like pygattlib, pygatt, pybluez and bluepy with no result to know how to send data in addition with their characteristics.
Is there any option to reproduce my request?.
I also read about iBeacon and Eddystonne protocol from Apple and Google, however my first point is to comunicate between two Raspberry (server and client).

If you are using rpi you should have Bluez preinstalled. Bluez provides API through D-Bus which you can use to add GAP and GATT functionality. The documentation is in source code of Bluez.
BLE advertising (GAP profile) documentation: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/advertising-api.txt
BLE data transfer (GATT profile) documentation: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/gatt-api.txt
Of course it easier to have an example. They are in Bluez repo too! They are written in Python but it should be easy to translate it to different language because they are using only D-Bus.
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/example-gatt-server
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/example-gatt-client
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/example-advertisement

I'll mark this as an answer because I could make it possible with the libraries written in javascript noble and bleno

Related

Python BLE simple example for sending messages

I want to implement a simple ble python program that will allow me to use my raspberrypi4 as a peripheral and my manjaro laptop as a central entity so I can send messages back and forth. Preferably both of these endpoints would be written python. I'm quite new to Bluetooth programming so I have tried to use the examples at pybluez (the refcomm server/client).
https://github.com/pybluez/pybluez/tree/master/examples/simple
After taking a closer look I saw this it doesn't use ble at all. Any good alternatives?

How to interact with BLE devices through Linux using C programs?

I am fairly new to the BLE world. Assuming I have a BLE device Eg: A heart rate monitor, How do I interact with that BLE device using C code.
I am able to see the Heart rate data using the command line tool 'bluetoothctl'. If I want connect to the same device through C code and see the Notified data, how do I go about it?
Though some codes on the Internet and GitHub helped me to scan for the device, I do not have any idea on how to connect to the device, read the GATT characteristics of the BLE device. I'd even appreaciate it if anyone points me to the API documentations of BlueZ, I literally have no clue what the programs are all about. Any assistance would be of great help.
The preferred API for BlueZ uses the D-Bus bindings. The documentation for those APIs are at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc
There are Python examples of how to use the D-Bus API at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test
bluetoothctl is written in C and the source is available at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/client

Receiving data from a BLE device to a raspberry Pi3

I never used a raspberry pi before and I have a project part of which includes receiving data from a BLE blood pressure device over Bluetooth to a raspberry pi 3. I can pair the device to my raspberry but I have no idea what to do next. I need to be able to receive the measurements in my raspberry please help and thank you in advance.
PS: the device I'm trying to use is very similar to this one:
https://ibb.co/71365k5
Thanks for your answers, I have tried "Gatt" and "Gatttool" but the problem is after pairing the device is meant to send the measurement to its application on an android phone, without it I cannot read the blood measurement characteristic or it just does not exist.
Linux uses BlueZ as a default bluetooth stack. It exposes DBus APIs for implementing software using Bluetooth.
If you don't want to use this API directly, which can sometimes feel a bit low level you have many libraries that wrap it, such as https://github.com/getsenic/gatt-python that also comes with examples.
All other solutions requires replacing the Bluetooth stack on Linux and hence your other existing Bluetooth pairings to the system will stop working.

Hardware communication with Python

Is it possible to write an API with Python so you can connect a physical ON and OFF switch via USB to a PC and when user presses the switch to ON or OFF, the python program detects it and send a signal to a web app and shows ON or OFF message on the website?
I am sorry if what I am asking its not clear enough!
Yes, it is possible. Reading USB devices can be done with Python. In linux USB device inputs can be found in some files(e.g. /dev/ttyUSB0). By reading those files you can get the information that you need. Putting here link that will be helpful
similar post
Firstly, you can't write an API to interact with hardware in python. You would have to use the pre-existing windows API(or the API provided by the Operating system that you are using) in order to interact with hardware in such a high-level language.
If you want to interact with hardware in python, and detect switch presses, releases etc, I would recommend you used a microcontroller such as a raspberry pi(for python) or an arduino(for C++). The respberry pi provides a very easy way to interact with hardware in python. If you still want to interact with a USB stick in python(but not acting as a switch) you can use the pyusb library.

Bluetooth Low-Energy on Linux API

I have a device with a few custom GATT services, and I would like to write a Linux program to interact with it. After some searching I found out that Linux is using BlueZ to handle the Bluetooth LE protocol. I'm using Ubuntu 15.10 with BlueZ 5.35, but I cannot figure out how use this BlueZ from a user-space program. I cannot find an API documentation anywhere, no tutorials, examples, nothing. Is it even possible to use this BlueZ stack to do anything other than just connecting to Bluetooth devices with default services? And if so, where is the documentation? (Preferably C/C++ API but at this point anything goes)
Have a look at attrib/gatttool.c in the bluez sources [1]. Gatttool is a command line utility for connecting to BTLE devices using the C "API". The GATT interface is not exposed in libbluetooth though.
A newer alternative to gatttool and thus another example to learn
from is the btgatt-client, which you can find in
tools/btgatt-client.c (to enable compilation configure bluez with
--enable-experimental).
Besides the C interface bluez integrated a DBUS interface.
bluetoothctl is an example tool using the DBUS interface. The code of
bluetoothctl can be found in client/ [2].
Another example program using the C interface of bluez is the Anki
Drive SDK [3]. It packaged the bluez GATT C interface in its own
library libbzle [4]. When using the C interface you have to connect a
socket when establishing a BTLE connection. The gatttool does this
via the GATT interface, which in turn uses glib iirc. But you can
also do this using syscalls (socket, connect, ...) as explained e.g.
here [5]. This document also explains:
Unfortunately, as of now there is no official API reference to refer to, so more curious readers are advised to download and examine the BlueZ source code.
Gilbert Brault also extracted the GATT interface from bluez [6] and links to a rudimentary doxygen documentation of the GATT interface [7] with the following disclaimer:
This is a work in progress with the intent of documenting all important functions and data structures
Also Szymon Janc gave a nice overview in his talk "Bluetooth on Modern Linux" at the Embedded Linux Conference 2016 [8]. Starting at 42:00 he talks about the unexposed C interface. But in general he seems to recommend the DBUS API (see "Tips" slide at 45:30). Some DBUS documentation can be found in doc/gatt-api.txt [9] and Python examples using the DBUS interface can be found in test/.
Hope this helps.
[1] http://git.kernel.org/cgit/bluetooth/bluez.git/tree/attrib/gatttool.c
[2] http://git.kernel.org/cgit/bluetooth/bluez.git/tree/client/
[3] https://github.com/anki/drive-sdk/
[4] https://github.com/anki/drive-sdk/tree/master/deps/bzle/
[5] https://people.csail.mit.edu/albert/bluez-intro/c404.html
[6] https://github.com/gbrault/gattclient
[7] http://gbrault.github.io/gattclient/index.html
[8] https://www.youtube.com/watch?v=tclS9arLFzk
[9] http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/gatt-api.txt
I feel your pain. I needed to add user input from a custom BLE peripheral, a simple remote pushbutton, to an embedded program running under Linux (Stretch) on a Raspberry Pi. I was stunned by the needless complexity and Spartan (not a compliment) documentation of the BlueZ API. All the BlueZ “examples” are written from the perspective that Bluetooth is the center of the universe and the user wants to support every Bluetooth device ever invented. In my case I knew exactly the device, service, and GATT characteristics I needed to interact with, and I wanted a minimum overhead task that would do its thing in a low priority thread.
It turns out a BLE central client is pretty straightforward using BlueZ, but it was an arduous road starting with the source for the BlueZ utility bluetoothctl in release 5.49. I accomplished my needs using only three unmodified source files from the BlueZ distribution and excerpts from an additional three source files. Since the BlueZ source is inextricably dependent on D-Bus and the Gnome GLib main loop, I grudgingly included them.
Following OlivierM's generous lead, and in hopes that my embarrassingly massive investment in time saves someone else a month of their life, I have posted my example Bluetooth BLE client on GitHub: https://github.com/jjjsmit/BluetoothBLEClient
It would arguably be simpler and quicker to write a shell script on Linux to do what you need to do. The BlueZ commands are relatively simple and straightforward, and there are many tutorials and questions on how to use it.
Tutorials:-
http://www.jaredwolff.com/blog/get-started-with-bluetooth-low-energy/
https://learn.adafruit.com/reverse-engineering-a-bluetooth-low-energy-light-bulb/control-with-bluez
https://lilyhack.wordpress.com/2014/02/03/ble-read-write-arduino-raspberry-pi/
http://joost.damad.be/2013/08/experiments-with-bluetooth-low-energy.html
Questions:-
Using Bluetooth low energy in linux command line
Bluetooth Low Energy: listening for notifications/indications in linux
How can I connect to the FitBit Zip over Bluetooth 4.0 LE on Linux with bluez?
Once you are more familiar with using the commands manually you can then write a minimal shell script so that this is automated for you.
I had a similar issue which is to interact with a BLE device with a GATT C/C++ API. I have realized there was no such API existing.
The way I fixed my issue was to write my own GATT library. I have pushed the code on Github: https://github.com/labapart/gattlib
I use this library in my own BLE project and it fulfils my needs. I created few examples https://github.com/labapart/gattlib/tree/master/examples that use the library to encourage people to use it and have better feedback.
I recently found out that Qt has Bluetooth Low Energy support as host since Qt 5.7. Qt Bluetooth LE. It is available under LGPLv3 or commercial license, and exposes a C++ API.

Resources