I am working on a project to communicate between a raspberry pi & a series of Arduinos, using the HC-05 bluetooth modules, over a bluetooth connection. I am able to pair the arduinos using bluetoothctl and the communicating using python scripts, but I would like to include the pairing process in my scripts as well but I have not found a solution that includes the bluetooth pairing pin in the scripts.
What I've tried:
The PyBluez library, but it is incapable of pairing.
Subprocess, but I can't respond to the pin request (code below), but this results in an error
of too many arguments (for bluetoothctl).
import subprocess, shlex
addr = "00:14:03:06:12:84"
pinCode = "1234"
args = ["bluetoothctl", f"pair {addr}", pinCode]
args = shlex(args)
subprocess.Popen(args)
I also tried using the bluetoothctl wrapper, but there's is no pin option here either.
Is pairing via python possible?
The way Bluez expects this to be done is with the D-Bus Agent API which is documented at https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/agent-api.txt
There is also a Python example in the Bluez source tree: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/simple-agent
As pairing is normally a one-off provisioning/security step where keys are exchanged and a device is established as trusted, I question the value of automating the pairing process. Do you really want to pair with devices that randomly turn up and are within range?
Subsequent connections between the RPi and HC-05 don't need to have the pairing step happen first. The Raspberry Pi would only be required to call the connection command because the two devices are already paired and trusted.
Related
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.
I'm trying to pair from a Linux host (ARM based, Angstrom distribution) to a MCU driven embedded device using BLE Just Works Secure Connection. As a device I'm currently using an ESP32 dev kit flashed with the GATT security example. However, so far my tries weren't successful and I failed to find the according documentation, either.
I managed to pair my Android smartphone with the device, so pairing on the device side, in general, seems to work. I also tried to conduct the pairing without a Secure Connection (setting Authorization Request to SP_LE_AUTH_BOND) which worked with bluetoothctl or btmgmt.
I'm grateful for any documentation pointer how to perform pairing from the command line, Python scripting or any C/C++ code.
Have a look at the answer below and the included references; these cover pairing using BlueZ/Linux:-
Raspberry Pi BLE Encryption/Pairing
If this still doesn't work, please launch "btmon" on another terminal before starting the pairing process as that will give you an indication as to what is going wrong.
I hope this helps.
I am working on a project with the beagle bone black wireless, where I need to be able to send music control commands to a phone. Note, I don't want to stream music to my beaglebone. I have spent about a week looking online, and found very little about this.
The OS for the beaglebone is Debian Jessie. I can get things like hci0tool, Bluetoothctl, hciconfig to work. I can detect and pair to a device. It seems though that my connection only lasts for the pairing process, and fails every time afterwards.
My current process is executing:
sudo su
bluetoothctl
power on
agent on
default-agent
scan on
I get the mac address
scan off
pair <MAC Address>
trust <MAC Address>
connect <MAC Address>
As of now pairing and trust succeed, though the connection ends after pairing finishes. And I have no idea of where to start for sending a command to a phone.
Connect call on Device1 interface will tries to connect all the profiles supported between the device and adapter. This happens by negotiating or exchanging the supported profiles.
Connection may not be possible when Adapter doesn't support the minimal requirement of profiles which is needed by the Device. In this case, you may need A2DP provider in adapter end to get connection successful. Yes, this is contradictory to this statement.
If you don't want to connect with all the profiles between Device and Adapter, then you can use ConnectProfile method in Device1 interface.
But bluetoothctl doesn't provide commands to achieve neither ConnectProfile nor you AVRCP commands.
You need to use D-Bus calls to get the communication with Bluetoothd. If your application is command line/shell based, you can use dbus-send/gdbus commands to address the D-Bus interface.
Although bluez-tools implements media control AVRCP commands, it doesn't provide any utility which uses it. Either you can compile bluez-tools as library and develop application using the media control API or use dbus-send/gdbus.
I have started with some samples using GDBUS, but not yet for AVRCP controls. See here : https://gist.github.com/parthitce
and Documentation here: https://www.linumiz.com/category/blog/
Ass everyone saying in their first posts "I'm new one" and trying to figure out one complexity.
I want to scan and create list of available wifi networks in Raspberry area and send the list to the phone via bluetooth.
Next - select one of the networks in the list on the phone, enter a password to selected network and send back via bluetooth to Raspberry
I'm using Raspberry pi3 with bluetooth on board, Raspbian OS and nodejs v7.4.0
I choose wifi-control to work with wifi network and it works greate. One thing - I should run npm run with sudo to get all networks, not just current one;
Then I'm trying to work with bluetooth via bluetooth-serial-port lib.
First of all I did all preparations that was written in documentation.
var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();
btSerial.inquire();
And it does nothing. At least I don't see any effect - my phone doesn't "see" Raspberry in available bluetooth devices list;
I thought that my Raspberry has porblems with bluetooth, but then I run
bluetoothctl -> power on -> discoverable on
And Raspberry appeared on phone.
What should I do to "turn on" Bluetooth control and add my Raspberry to list of available bluetooth devices?
Peace!
Got the exact same problem. My solution (which might or might not be fitting for your needs) was to pair the phone via bluetoothctl beforehand. (this already is you why this solution kinda sucks: you can't come and use a different phone/pi the next day, stuff got completely screwed when changing to a different pi :D)
Oh and the main script should run as root, else all of this wont work.
1) Pairing your device
$ bluetoothctl
[bluetoothctl] agent on
[bluetoothctl] discoverable on
[bluetoothctl] pairable on
[bluetoothctl] scan on
(now you turn on your bluetooth on your phone and search for devices, in bluetoothctl you should see your device and mac to be shown)
[bluetoothctl] pair XX:XX...(MAC of your phone)
(phone is going to show the "yo this device wants to pair"-dialog and bluetoothctl wants you to confirm the pairing too)
Now you can always connect to the pi via bluetooth, if it is discoverable or not. (I'm using Serial Bluetooth Terminal)
2) Actual talking between devices
NONE of the npm packages supposed to work with bluetooth worked for me. Not a single one. So in the end I used rfcomm and it's ability to start a program on connection. Together with serialport I let rfcomm run node myscript.js, which establishes the actual serial connection like so:
2.1) rfcomm waiting for connections
const rfcommProc = spawn(
'rfcomm',
['watch', 'hci0', '1', 'node', `${__dirname}/myscript.js`]
);
2.2) myscript.js opening the port
const port = new SerialPort('/dev/rfcomm0', spError => {
if (spError) {
process.send(spError);
}
})
Look up on the npmjs-page how to receive and send stuff now.(:
Hope this gives you some ideas and/or helped.
3) Note
The bluetooth service starts quite late. Making a service requiring it didn't do the job for me, but adding it to rc.local seems to be "late enough" to have it running and listening on startup.
The 3G USB modem I'm using creates /dev/ttyUSB which I can send AT commands to it as a serial port.
But is there another API to communicate to a USB Modem? is it always working as a serial COM?
How to get the API like AT command list from different vendors?
I'm writing a program in C to make calls, data test and so on but the AT commands doesn't always work, many times I needed to unplug/plug the modem to get it to work again so I thought there could be another way to do that.
Debian Linux is the platform by the way.
Appreciate your answers.