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.
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/