class SimulatedKeyboardDevice: def __init__(self): os.system("hciconfig hci0 up") os.system("hciconfig hci0 class 0x002540") os.system("hciconfig hci0 name " + DEVICE_NAME) os.system("hciconfig hci0 piscan") opts = { "ServiceRecord": read_service_record(), "Role": "server", "RequireAuthentication": False, "RequireAuthorization": False, } bus = dbus.SystemBus() manager = dbus.Interface(bus.get_object("org.bluez", "/org/bluez"), "org.bluez.ProfileManager1") profile = KeyboardProfile(bus, PROFILE_DBUS_PATH) manager.RegisterProfile(PROFILE_DBUS_PATH, UUID, opts) self.control_socket = BluetoothSocket(L2CAP) self.interrupt_socket = BluetoothSocket(L2CAP) self.control_socket.setblocking(0) self.interrupt_socket.setblocking(0) self.control_socket.bind(("", CONTROL_PORT)) self.interrupt_socket.bind(("", INTERRUPT_PORT)) def listen(self): print "Waiting for a connection" self.control_socket.listen(1) self.interrupt_socket.listen(1) self.control_channel = None self.interrupt_channel = None gobject.io_add_watch( self.control_socket.fileno(), gobject.IO_IN, self.accept_control) gobject.io_add_watch( self.interrupt_socket.fileno(), gobject.IO_IN, self.accept_interrupt) def accept_control(self, source, cond): self.control_channel, cinfo = self.control_socket.accept() print "Got a connection on the control channel from " + cinfo[0] return True def accept_interrupt(self, source, cond): self.interrupt_channel, cinfo = self.interrupt_socket.accept() print "Got a connection on the interrupt channel from " + cinfo[0] return True def send(self, message): if self.interrupt_channel is not None: self.interrupt_channel.send(message)
def get_local_address(): hci = BluetoothSocket(HCI) fd = hci.fileno() buf = array.array('B', [0] * 96) ioctl(fd, _bluetooth.HCIGETDEVINFO, buf, 1) data = struct.unpack_from("H8s6B", buf.tostring()) return data[2:8][::-1]