Python BLE simple example for sending messages - python-3.x

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?

Related

Has anyone used bleak to connect multiple BLE devices and receive notifications from all of them simultaneously?

I am able to use bleak and get data from all 5 BLE sensors. But the problem is that I am unable to identify which data is from which device. I mean i need a string representing the address of the device alongwith the data itself.
I was able to get data simultaneously from all the BLE modules using bleak in Windows, as well as on raspberry pi.
The only problem with Windows is that if you use Bluetooth 4.2 adapter & above. It will be much better for the high speed data rate, and proper devices connection handling. As with the 4.0 adapter, I have always gone through one or two exceptions each time i started the script, and the maximum number of connections I got was 3.
When i tried this script on Raspberry pi 3b+, it has on-board chip of Bluetooth 4.2. It was able to give high speed data rate, and my 5 sensors were connected to it simultaneously.
Also, The two_devices examples in bleak source code on Github, is very good example for starting with further coding.
And if you run the script and found the data on console, but you were unable to get that which data is from device. Then you need to use functools, (what it does is it will inject the client you're connected to at present, with the callback function, and it will make the work much easier).
Check this out on Github: https://github.com/hbldh/bleak/issues/601

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

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.

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

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

Integrating Arduino code into C++

What library/libraries do I need to add in order to communicate with an Arduino from my desktop Visual C++ application? At this point, all I really want to do is make an LED flash (write high/low to a pin). I already have the desktop application going, but would like it to interact with the Arduino. No, Arduino Uno is not suitable for my case.
Edit 1 Also: I am using a serial USB COM port to connect to the Arduino.
Edit 2 While I am programming in Windows, the application will be designed for Windows or Android.
In the Arduino side, you can just use the built-in Serial to send and receive data.
In the desktop side, refer to this question, which tells you how you can talk to Serial ports in Visual C++
EDIT:
If you are planning to use Java, then you can use the Java Serial port library without any changes to the Arduino code.
Also I would recommend you to use the Android APIs if you want to create an Android app. Using NDK is a real pain and unless you know what you are doing, it will be very tough.
With the arduino connected as a serial device you can have software in the middle which connects it to a network socket.
If you google it you'll find several free programs that do this.
You could even write this yourself in a higher programming language (python, perl, php etc..) very easily, it's just a handful of lines of code.
Then it's quite painless to connect to the arduino, whether its c++ or javascript.

Resources