reestablish dropped bluetooth connection in python 3
I have written a little program which emulates a bluetooth keyboard (executed by a RPi) in Python 3 using the built-in socket library. It works fine despite one feature:
When a real Bluetooth keyboard connects to a device, both the device and the keyboard somehow remember the other device. when the keyboard then is shut down and restarted, it automatically reconnects to the devece without having to trigger it explicitely. (I've tried to just connect to the device again [without binding, listening, accepting] but it doesn't work)
So, how can I achieve such a behavior? When starting the script, it should just connect without a manual connection request on the device-site.
EDIT: Since it was asked for, here is the (shortened) code for the connection process:
import socket
import subprocess
import dbus
[...]
class btsSocket():
#define some constants
P_CTRL =17 #Service port - must match port configured in SDP record
P_INTR =19 #Service port - must match port configured in SDP record#Interrrupt port
def __init__(self, btAddress, btUuid, btServiceRecordUrl, btDeviceName, btDeviceClass):
self._init_bluez_profile(btUuid, btServiceRecordUrl) #set up a bluez profile to advertise device capabilities from a loaded service record
self._init_bt_device(btDeviceName, btDeviceClass) #configure the bluetooth hardware device
self._listen(btAddress)
#configure the bluetooth hardware device
def _init_bt_device(self, deviceName, deviceClass):
#set the device class to a keybord and set the name
subprocess.call(["hciconfig", "hcio", "class", deviceClass])
subprocess.call(["hciconfig", "hcio", "name", deviceName])
#make the device discoverable
subprocess.call(["hciconfig", "hcio", "piscan"])
#set up a bluez profile to advertise device capabilities from a loaded service record
def _init_bluez_profile(self, uuid, serviceRecordUrl):
[...]
#listen for incoming client connections
def _listen(self, address):
self.scontrol = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_L2CAP)
self.sinterrupt = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_L2CAP)
#bind these sockets to a port - port zero to select next available
self.scontrol.bind((address, self.P_CTRL))
self.sinterrupt.bind((address, self.P_INTR))
#Start listening on the server sockets
self.scontrol.listen(1) # Limit of 1 connection
self.sinterrupt.listen(1)
self.ccontrol, cinfo = self.scontrol.accept()
print ("Got a connection on the control channel from " + cinfo[0])
self.cinterrupt, cinfo = self.sinterrupt.accept()
print ("Got a connection on the interrupt channel from " + cinfo[0])
#send a string to the bluetooth host machine
def send_string(self, message):
self.cinterrupt.send(message)
Creating an object of this class sets the Pi in a listening mode until I navigate with the device (which I want to connect to) in the respective menue, search for nearby Bluetooth keyboards (aka the Pi) and click on it. The real Bluetooth keyboard just connects when turning it on (if it was already connected properly before).
链接地址: http://www.djcxy.com/p/58322.html上一篇: 我如何在C ++中编写简短的文字?