9

I am working on a Python script to search for bluetooth devices and connect them using RFCOMM. This devices has Passkey/Password. I am using PyBlueZ and, as far as I know, this library cannot handle Passkey/Password connections (Python PyBluez connecting to passkey protected device).

I am able to discover the devices and retrieve their names and addresses:

nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
                                                      flush_cache=True, lookup_class=False)

But if tried to connect to a specific device using:

s = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
s.connect((addr,port)) 

I get an error 'Device or resource busy (16)'.

I tried some bash commands using the hcitool and bluetooth-agent, but I need to do the connection programmatically. I was able to connect to my device using the steps described here: How to pair a bluetooth device from command line on Linux.

I want to ask if someone has connected to a bluetooth device with Passkey/Password using Python. I am thinking about to use the bash commands in Python using subprocess.call(), but I am not sure if it is a good idea.

Thanks for any help.

Community
  • 1
  • 1
Juan C. Vanegas
  • 373
  • 1
  • 4
  • 12

2 Answers2

17

Finally I am able to connect to a device using PyBlueZ. I hope this answer will help others in the future. I tried the following:

First, import the modules and discover the devices.

import bluetooth, subprocess
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
                                                      flush_cache=True, lookup_class=False)

When you discover the device you want to connect, you need to know port, the address and passkey. With that information do the next:

name = name      # Device name
addr = addr      # Device Address
port = 1         # RFCOMM port
passkey = "1111" # passkey of the device you want to connect

# kill any "bluetooth-agent" process that is already running
subprocess.call("kill -9 `pidof bluetooth-agent`",shell=True)

# Start a new "bluetooth-agent" process where XXXX is the passkey
status = subprocess.call("bluetooth-agent " + passkey + " &",shell=True)

# Now, connect in the same way as always with PyBlueZ
try:
    s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    s.connect((addr,port))
except bluetooth.btcommon.BluetoothError as err:
    # Error handler
    pass

Now, you are connected!! You can use your socket for the task you need:

s.recv(1024) # Buffer size
s.send("Hello World!")

Official PyBlueZ documentation is available here

Juan C. Vanegas
  • 373
  • 1
  • 4
  • 12
  • Hi Yahya. Yes, it is a command. `bluetooth-agent` is a tool to manage the pairing code. You can read more about it: https://askubuntu.com/questions/763939/bluetoothctl-what-is-a-bluetooth-agent https://wiki.debian.org/BluetoothUser#Pairing_using_CLI – Juan C. Vanegas Mar 06 '19 at 15:49
  • Alright, thanks, so it's basically: `your bluetooth agent` for example `bluetoothctl` – Yahya Mar 06 '19 at 15:51
  • Yes. You can use `bluetoothctl` if you do not have `bluetooth-agent` – Juan C. Vanegas Mar 06 '19 at 15:54
  • So is there a `bluetooth-agent` package that I can install or it is just a name the refer to the category which includes as I mentioned for example `bluetoothctl`? – Yahya Mar 06 '19 at 16:40
  • According to http://manpages.ubuntu.com/manpages/trusty/en/man1/bluetooth-agent.1.html, if you have `bluez` installed is possible you already have `bluetooth-agent`. In Debian (https://wiki.debian.org/BluetoothUser#Pairing_using_CLI) they said "Bluetooth-agent is part of package bluez. So, if you use Debian testing or unstable, it should already be installed." – Juan C. Vanegas Mar 06 '19 at 17:01
  • 3
    Any by the way for future readers, not all debians dists have `bluetooth-agent` , it's clearly mentioned that: "If `bluetooth-agent` is not available, try `bluetoothctl`" – Yahya Mar 06 '19 at 18:29
  • is there something similar to `bluetoothctl` or `bluetooth-agent` to use in windows 10 – moe asal Sep 03 '19 at 10:25
  • Hi. I am not sure if there is something similar for Windows 10. Maybe you can check this link http://bluetoothinstaller.com/bluetooth-command-line-tools/ – Juan C. Vanegas Sep 04 '19 at 15:15
  • Hi ! In my case, the remote device will prompt its security passcode only when it gets a connexion request. How to manage this with this code ? Thanks ! – Vincent Monteil Sep 05 '19 at 21:16
  • Hi Vincent, the prompt is on a display? If this is the case, I understand that the user needs to handle it. – Juan C. Vanegas Sep 09 '19 at 12:50
0

Is there a way to connect two phones via Bluetooth , the script should be running on a Linux host. Any suggestions of using pybluez or any other APIs?

I have seen some examples where a Linux host is used as Client and is connect a phone (which is a server), but here I'm want to use Linux host as just a device to run the script and make two phones connect via Bluetooth.

avy_hus
  • 53
  • 2
  • 5