hantescu
Posts: 7
Joined: Tue Jan 19, 2016 11:33 am

Bluetooth Python script.

Tue Jan 19, 2016 10:07 pm

I fallowed this tutorial http://blog.davidvassallo.me/2014/05/11 ... unication/ and after i run rfcomm-server.py from /usr/share/doc/python-bluez/examples/simple/ it gives me the following error:

Code: Select all

Traceback (most recent call last):
  File "/usr/share/doc/python-bluez/examples/simple/rfcomm-server.py", line 20, in <module>
    profiles = [ SERIAL_PORT_PROFILE ], 
  File "/usr/lib/python2.7/dist-packages/bluetooth/bluez.py", line 176, in advertise_service
    raise BluetoothError (str (e))
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
Can someone help me?

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Tue Jan 19, 2016 11:00 pm

I'm guessing that you don't have the Serial Port Profile loaded? To do that, you'll need to

Code: Select all

sudo sdptool add SP
To do THAT, you need to run the Bluetooth daemon in 'compatibility' mode. Edit /etc/systemd/system/dbus-org.bluez.service and add '-C' after 'bluetoothd'. Reboot.
Last edited by Douglas6 on Mon Aug 01, 2016 7:43 pm, edited 1 time in total.

hantescu
Posts: 7
Joined: Tue Jan 19, 2016 11:33 am

Re: Bluetooth Python script.

Tue Jan 19, 2016 11:46 pm

Douglas6 wrote:I'm guessing that you don't have the Serial Port Profile loaded? To do that, you'll need to

Code: Select all

sudo sdptool add SP
To do THAT, you need to run the Bluetooth daemon in 'compatibility' mode. Edit /lib/systemd/system/bluetooth.service and add '-C' after 'bluetoothd'. Reboot.
It works now, thanks a lot! :D

joga
Posts: 4
Joined: Sat Mar 05, 2016 8:22 pm

Re: Bluetooth Python script.

Mon Aug 01, 2016 7:29 pm

is there any consecuences initializing the daemon in compatible mode?

joga
Posts: 4
Joined: Sat Mar 05, 2016 8:22 pm

Re: Bluetooth Python script.

Mon Aug 01, 2016 7:31 pm

Douglas6 wrote:I'm guessing that you don't have the Serial Port Profile loaded? To do that, you'll need to

Code: Select all

sudo sdptool add SP
To do THAT, you need to run the Bluetooth daemon in 'compatibility' mode. Edit /lib/systemd/system/bluetooth.service and add '-C' after 'bluetoothd'. Reboot.
is there any side effects initializing the deamon in compatible mode?

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Mon Aug 01, 2016 7:34 pm

None that I am aware of. Perhaps a slightly larger memory footprint.

By the way, that post of mine is no longer accurate. The file to edit should be /etc/systemd/system/dbus-org.bluez.service.

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth Python script.

Mon Mar 06, 2017 10:52 pm

Is this really the only way to get SP enabled? Plenty of googling has left me coming up short - it seems really strange that there isnt an way of enabling SP without putting bluetoothd into compatibility mode.

Im trying to create a simple to use bluetooth module and this is a barrier to making the setup simple.

Any thoughts?
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Mon Mar 06, 2017 11:12 pm

It should be possible to add Bluetooth profiles programmatically, without using the sdptool utility. It involves sending a service record (in XML or binary format) to a BlueZ D-Bus interface.

Using sdptool is just much easier. My question is why they decided to remove it from BlueZ 5 in the first place.

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth Python script.

Tue Mar 07, 2017 8:05 am

Douglas6 wrote:Using sdptool is just much easier. My question is why they decided to remove it from BlueZ 5 in the first place.
I have the same question, it just seems very odd.
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth Python script.

Tue Mar 07, 2017 8:14 am

Douglas6 wrote:It should be possible to add Bluetooth profiles programmatically, without using the sdptool utility. It involves sending a service record (in XML or binary format) to a BlueZ D-Bus interface.
Do you have any background / information / links re how this might be achieved? My google skills are clearly lacking this morning I couldnt find anything specific re what d-bus api or what the format of the request should look like.
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Tue Mar 07, 2017 2:34 pm

I can reply with some fairly useless information and general hints. :)

Here's a link to some code I played with years ago that added an HID service record. Just a few lines in the addService() routine did the trick. Some caveats: 1. I was adding a HID service, not SPP. 2. It was written for BlueZ 4, not 5, and it looks like things have changed substantially since then. Link: viewtopic.php?p=509679#p509679

I'd start looking in the BlueZ source tree, here: http://git.kernel.org/cgit/bluetooth/bluez.git/tree. In the test/ directory, there's a sample service-spp.xml file; I don't know if that needs tweaking. As I recall, if you have sdptool running on your dev machine, you can use it to export service records in several formats (don't recall if the service needs to be added first), so maybe you can compare with that. In the BlueZ 4 tree there was an example (in Python) of adding a record; that seems to have been removed or refactored (Maybe test/test-profile?).

In the doc/ directory are sparse documentation on the D-Bus interfaces. I would start with profile-api.txt and the ProfileManager1.RegisterProfile() method (I guess); it takes a ServiceRecord parameter.

I'll see if I can find time to play with this stuff, but hopefully you have a few more Google search terms to play with. But... There's not much out there.

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth Python script.

Tue Mar 07, 2017 3:59 pm

Thanks a lot for the information, this is a big help.

This looks interesting - a python script for adding the Serial Port profile to bluez (albeit v4.98)
https://www.apt-browse.org/browse/ubunt ... le-service
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Tue Mar 07, 2017 4:16 pm

Yeah, that's how I did it, but things have changed in BlueZ 5. It shouldn't be drastically different, but the D-Bus interfaces have changed.

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth Python script.

Tue Mar 07, 2017 10:43 pm

So, Im not sure if i am making progress, but I can now call bluez through d-bus and not get an errors, so I'm considering that a success!

Code: Select all

import dbus
bus = dbus.SystemBus()
profilemanager1 =  dbus.Interface(bus.get_object("org.bluez", "/org/bluez"),
						"org.bluez.ProfileManager1")
print(profilemanager1)
opts = {}
opts["path"] = "/home/pi"
opts["Name"] = "bluebutton"
profilemanager1.RegisterProfile(opts["path"], "0x1101", opts)
The information here (http://www.bluez.org/bluez-5-api-introd ... ing-guide/) proved useful in understanding the differences between bluez 4 & 5 and making some progress in getting this far.

There is also a useful example here (https://kernel.googlesource.com/pub/scm ... st-profile) which shows how to call the bluez 5 interfaces.
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Wed Mar 08, 2017 1:39 am

This seems to be working for me. I'm able to run a 'sudo rfcomm watch hci0' command on the Pi, and connect with my phone, I can connect with my phone and see that the /dev/rfcomm0 port is created.

Code: Select all

import dbus
import time

service_record = """
<?xml version="1.0" encoding="UTF-8" ?>
<record>
  <attribute id="0x0001">
    <sequence>
      <uuid value="0x1101"/>
    </sequence>
  </attribute>

  <attribute id="0x0004">
    <sequence>
      <sequence>
        <uuid value="0x0100"/>
      </sequence>
      <sequence>
        <uuid value="0x0003"/>
        <uint8 value="1" name="channel"/>
      </sequence>
    </sequence>
  </attribute>

  <attribute id="0x0100">
    <text value="Serial Port" name="name"/>
  </attribute>
</record>
"""

bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object("org.bluez", "/org/bluez"),
        "org.bluez.ProfileManager1")

path = "/bluez"
uuid = "00001101-0000-1000-8000-00805f9b34fb"
opts = {
    "AutoConnect" : True,
    "ServiceRecord" : service_record
}

manager.RegisterProfile(path, uuid, opts)

time.sleep(600)
You may need to remove pairings (on both ends) and re-pair while this is running. Also, it only works for 10 minutes; once the Python program ends, the service is removed. Don't know it you can handle that.

User avatar
uk_baz
Posts: 6
Joined: Thu Oct 22, 2015 9:22 pm

Re: Bluetooth Python script.

Wed Mar 08, 2017 10:07 am

Hello,

Sorry, rather late to this discussion so apologies if my interruption is a distraction rather than helping.

I've been doing a fair bit of work with BlueZ on Raspberry Pi of late so I want to help if I can.
Most of my efforts have been with connecting the RPi and the BBC micro:bit which uses Bluetooth Low Energy (BLE). BLE is > 4.1 of the Bluetooth spec where the discussion above is mainly talking about using what is referred to as Bluetooth Classic (~2.1 of the spec) so my experience may not be directly relevant.

I did also want to echo what @Douglas6 said about the DBus API in BlueZ changing significantly between 4 & 5. Indeed, even between versions of 5 there are some some big changes in the API. This results in lots of out of date information on the web when you do a search. This also means when you look at the online docs and examples you need to look for the version of BlueZ you are running.
By default on the RPi it is 5.23 so look at this version of the
docs:
http://git.kernel.org/cgit/bluetooth/bl ... txt?h=5.23
Examples:
http://git.kernel.org/cgit/bluetooth/bl ... est?h=5.23

From version 5.43 the DBus API seems to have become stable and that is the version which is in Debian Stretch.

It would be good to know what hardware you plan to have at the other end of Bluetooth link. I saw at one point that it was a button. For things like buttons you may find using BLE far easier than having a serial connection if you have that option.

You can get help directly from BlueZ developers and there are various ways to contact them. They can be helpful if you ask specific questions. More information at:
http://www.bluez.org/contact/

I've also found it helpful when working with Bluetooth to treat the scan and connecting to devices as separate from exchanging data with them when starting out. When I'm starting off I use 'bluetoothctl' on the command line to check that I can scan and connect to the device. I then write the code for exchanging data. Then got back and code the scanning and connecting piece.
If you can connect to the remote hardware with bluetoothctl then you should be able to (with Bluetooth classic) use python sockets or evdev libraries to exchange data. Again, it would be good to know what is the other end of the Bluetooth link as BLE may be an easier option.

I did a talk at CamJam recently about controlling objects over BLE with Python which may offer some useful background information: https://youtu.be/x9Zdz57h3kc

User avatar
uk_baz
Posts: 6
Joined: Thu Oct 22, 2015 9:22 pm

Re: Bluetooth Python script.

Wed Mar 08, 2017 11:56 am

I've found the same question on the BlueZ mailing list:
http://marc.info/?l=linux-bluetooth&m=1 ... 529947&w=2

The answer pointed them at this example:
http://git.kernel.org/cgit/bluetooth/bl ... ile?h=5.23

They never answered as to if it worked

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth Python script.

Wed Mar 08, 2017 12:07 pm

I saw this, and used it... It ran and didn't fall over, although some info would be helpful re how to call it.

It didnt solve the root issue I was having which was that when using pybluez, unless you add the SP profile using sdptool, you get an error when trying to advertise_service().

It does feel like we are taking some steps forward tho. I need to do some testing tonight, but its looking positive that I can now pair a device and communicate over serial without having to go through the rigour of putting bluetoothd into compatibility mode and then running sdptool.
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Wed Mar 08, 2017 3:07 pm

@uk_baz: I appreciate the attempt to help, and I'm glad to see more people using BLE on the Pi. But that really has no relevance to this thread, and would be more helpful elsewhere, rather than hijacking this thread with duplicated advice.

User avatar
uk_baz
Posts: 6
Joined: Thu Oct 22, 2015 9:22 pm

Re: Bluetooth Python script.

Wed Mar 08, 2017 6:02 pm

But that really has no relevance to this thread, and would be more helpful elsewhere, rather than hijacking this thread with duplicated advice.
@Douglas6: Apologies if my contribution has come across a hijacking this thread, it was not my intention. I raised BLE because that is a better technology fit for reporting things like button presses. I agree that it was not the original question but I thought it might be useful information about how to solve the application problem.
Not sure what the "duplicated advice" is referring to. I appreciate that I pointed to the original version of the test-profile script which is very similar to what had already been linked to. However it was more to show how to access docs and tests that were with the version that was part of the 5.23 release of BlueZ rather than the latest which are likely to be different.

BlueZ API's are going through rapid change at the moment making many sources of information and libraries out of date. My aim was to make more specific references.

Again, apologies and I will be more considered in future postings.

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Wed Mar 08, 2017 6:07 pm

Not a problem. Bluetooth issues are widely varied, but they seem all to be congealed into a few long threads on this forum. I'm probably over-sensitive. My apologies.

yash012
Posts: 12
Joined: Thu Mar 09, 2017 12:08 pm

Re: Bluetooth Python script.

Thu Mar 09, 2017 1:12 pm

Hi i'm new here i am working on a project , i need to detect a ibeacon on Raspberry pi 3 .
it is detected and i can see the MAC address well in Terminal using Bluetooth scan on command but i want to do that in python ,
i used this:

Code: Select all

nearby_devices = bluetooth.discover_devices(lookup_names = True)
but in output it only shows other Bluetooth device, ibeacon is not detected.

can anyone help me on this.

User avatar
Douglas6
Posts: 4765
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth Python script.

Thu Mar 09, 2017 1:35 pm

Sigh. Please do not hijack threads of a completely different topic. If your question does not relate to the thread (as in this case), please start a new thread. I don't think I'm being over sensitive this time.

ykasidit
Posts: 21
Joined: Sun Jul 14, 2013 10:58 pm
Location: Thailand
Contact: Website

Re: Bluetooth Python script.

Sun Jun 11, 2017 4:37 am

Hello,

Just in case it might help someone here (as this is the original thread I was reading and trying to solve as I also encountered too many issues trying to get 'rfcomm' working on 'newer' bluez versions - I'm using 5.45 - *without using deprecated tools* and seeing quite a few queries on this too.)

Just got some 'rfcomm'-like thing working between Android and bluez - requires bluez 5.45 - you might give it a try by following the steps in the 'Setup and examples' part at:
https://github.com/ykasidit/bluez-compassion

Note0: it also has 'more easily scriptable' commands to power on/off the bt device, set discoverable, auto accept pairing/authen requests - without having to use/parse/pipe 'bluetoothctl'. Also you don't have to worry about SDP as the 'test-profile' example (adjusted into 'rfcomm.py') automatically handles SDP already.

Note1: tested on Armbian and Ubuntu PC but not on a real RPi yet - but this is likely a 'bluez' thing and could likely work on RPi too.

Note2: This is quite new and can likely have more issues - and still needs to use stdin/stdout at the moment - the pipe method like /dev/rfcomm0 is not 'working' yet.

Note3: At this stage, I still think that using older bluez versions for rfcomm-like operations might be more stable - or might not ;-)

All the best wishes and kind regards.

Gabrieel04
Posts: 3
Joined: Sun Jul 09, 2017 1:19 am

Re: Bluetooth Python script.

Wed Jul 19, 2017 5:21 pm

I'm having the same problem, any solution?

Return to “General discussion”