4

I'm working on a HID bluetooth device with the code on the device in Python; at the moment it can connect to a PC by running:

os.system("hciconfig hcio class 0x002560")
os.system("hciconfig hcio name DataPaqWalk")

Then we can use pybluez to connect the sockets and wait for a connection:

print("Waiting for connections")

self.scontrol=BluetoothSocket(L2CAP)
self.sinterrupt=BluetoothSocket(L2CAP)
self.scontrol.listen(1) # Limit of 1 connection
self.sinterrupt.listen(1)
self.ccontrol,cinfo = self.scontrol.accept()
self.cinterrupt, cinfo = self.sinterrupt.accept()

This works and we have a thread polling with hcitool con to detect the Windows PC (adapter) disconnecting where we dump the sockets and listen again. The hci is setup with no security so the PC connecting to it automatically pairs - this all works.

However the issue comes when the device is powered off; the PC correctly detects that the device is gone and it remains in the paired state. What I want to do is to get the device to automatically connect to the PC it is paired with. I've obviously got the Mac address of the PC and I am trying to connect using: (P_CTRL is 17 and P_INTR is 19)

self.ccontrol,cinfo = self.scontrol.connect(('C8:FF:28:79:05:D4', self.P_CTRL))
self.controlClientMac = cinfo[0]
self.controlClientPsm = cinfo[1]
print ('control is ' + self.controlClientMac + " " + str(self.controlClientPsm))

self.cinterrupt,cinfo = self.scontrol.connect(('C8:FF:28:79:05:D4', self.P_INTR))
self.interruptClientMac = cinfo[0]
self.interruptClientPsm = cinfo[1]
print ('interrupt is ' + self.interruptClientMac + " " + str(self.interruptClientPsm))

This basically tries to connect and gives me back:

Traceback (most recent call last):
  File "server/btk_server.py", line 267, in <module>
    myservice = BTKbService();
  File "server/btk_server.py", line 226, in __init__
    self.device.listen();
  File "server/btk_server.py", line 174, in listen
    self.ccontrol,cinfo = self.scontrol.connect(('C8:FF:28:79:05:D4', self.P_CTRL))
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: (111, 'Connection refused')

In the bluetooth windows dialog you can see that it flicks to connected but straight back to paired. The question is; how do I get the device to connect to the paired windows adapter? Note that I'm getting a similar response in bluetoothctl.

Neil Benn
  • 904
  • 1
  • 12
  • 31
  • Hey, Neil. If you ever made any progress on this I'd love to know because I'm trying to do the same thing. I have a python application that acts as a bluetooth slave and I would like it to be able to reconnect to the master when the app is restarted. I think the app needs to initiate the bluetooth connection as a slave and then somehow indicate to the master that it should reconnect to a specific service uuid that the slave is providing. This seems to be a pretty standard use case with my real bluetooth devices, but reverse engineering it has been challenging. – shwoseph May 24 '21 at 19:16
  • Sorry but nothing, I asked around and kept trying but it is actually quite difficult to even get someone to understand the problem. I had to give up in the end and I used a laird module which is crazy but it was the only way to get it working. – Neil Benn May 26 '21 at 10:02

1 Answers1

0

Wouldnt you be able to run a script on the rpi upon startup automatically that starts looking for a bluetooth socket? https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/

user P520
  • 317
  • 2
  • 10
  • Thanks; that is not the problem - I can run a script at startup (actually include this reconnection as part of the python script detailed above). The question is - how to reconnect to the PC; just making a socket and calling connect from the device to the PC doesn't work; it connects and disconnects immediately because (I assume) the master/slave relationship is the wrong way round. Remember the the RPi is the device and the client is (typically) a windows PC. – Neil Benn Sep 28 '18 at 21:52
  • TBH that wasn't the correct answer but seeing as you are the only person who answered - I'll give the +100 rep to you. Thanks for replying. – Neil Benn Oct 09 '18 at 09:10