Replies: 3 comments 8 replies
-
It should be possible. |
Beta Was this translation helpful? Give feedback.
3 replies
-
This is by no means production code but rather a quick solution so I can pair with devices in python. Agent Module to pair (runs in a thread) import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GLib
import threading
BUS_NAME = 'org.bluez'
ADAPTER_IFACE = 'org.bluez.Adapter1'
ADAPTER_ROOT = '/org/bluez/hci'
AGENT_IFACE = 'org.bluez.Agent1'
AGNT_MNGR_IFACE = 'org.bluez.AgentManager1'
AGENT_PATH = '/my/app/agent'
AGNT_MNGR_PATH = '/org/bluez'
CAPABILITY = 'KeyboardDisplay'
DEVICE_IFACE = 'org.bluez.Device1'
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
passkey = None
def set_trusted(path):
props = dbus.Interface(bus.get_object(BUS_NAME, path), dbus.PROPERTIES_IFACE)
props.Set(DEVICE_IFACE, "Trusted", True)
class Agent(dbus.service.Object):
@dbus.service.method(AGENT_IFACE,
in_signature="", out_signature="")
def Release(self):
print("Release")
@dbus.service.method(AGENT_IFACE,
in_signature='o', out_signature='s')
def RequestPinCode(self, device):
print(f'RequestPinCode {device}')
return '0000'
@dbus.service.method(AGENT_IFACE,
in_signature="ou", out_signature="")
def RequestConfirmation(self, device, passkey):
print("RequestConfirmation (%s, %06d)" % (device, passkey))
set_trusted(device)
return
@dbus.service.method(AGENT_IFACE,
in_signature="o", out_signature="")
def RequestAuthorization(self, device):
print("RequestAuthorization (%s)" % (device))
auth = input("Authorize? (yes/no): ")
if (auth == "yes"):
return
raise Rejected("Pairing rejected")
@dbus.service.method(AGENT_IFACE,
in_signature="o", out_signature="u")
def RequestPasskey(self, device):
global passkey
print("RequestPasskey (%s)" % (device))
print(passkey)
if(passkey==None):
return dbus.UInt32("000000")
else:
return dbus.UInt32(passkey)
#set_trusted(device)
return dbus.UInt32(passkey)
@dbus.service.method(AGENT_IFACE,
in_signature="ouq", out_signature="")
def DisplayPasskey(self, device, passkey, entered):
print("DisplayPasskey (%s, %06u entered %u)" %
(device, passkey, entered))
@dbus.service.method(AGENT_IFACE,
in_signature="os", out_signature="")
def DisplayPinCode(self, device, pincode):
print("DisplayPinCode (%s, %s)" % (device, pincode))
class Adapter:
def __init__(self, idx=0):
bus = dbus.SystemBus()
self.path = f'{ADAPTER_ROOT}{idx}'
self.adapter_object = bus.get_object(BUS_NAME, self.path)
self.adapter_props = dbus.Interface(self.adapter_object,
dbus.PROPERTIES_IFACE)
self.adapter_props.Set(ADAPTER_IFACE,
'DiscoverableTimeout', dbus.UInt32(0))
self.adapter_props.Set(ADAPTER_IFACE,
'Discoverable', True)
self.adapter_props.Set(ADAPTER_IFACE,
'PairableTimeout', dbus.UInt32(0))
self.adapter_props.Set(ADAPTER_IFACE,
'Pairable', True)
class BluetoothAgent:
def __init__(self, idx=0):
self.agent = Agent(bus, AGENT_PATH)
self.agnt_mngr = dbus.Interface(bus.get_object(BUS_NAME, AGNT_MNGR_PATH),
AGNT_MNGR_IFACE)
self.passkey = None
def agent_loop(self):
try:
self.mainloop.run()
except Exception:
self.agnt_mngr.UnregisterAgent(AGENT_PATH)
self.mainloop.quit()
def start(self):
self.agnt_mngr.RegisterAgent(AGENT_PATH, CAPABILITY)
self.agnt_mngr.RequestDefaultAgent(AGENT_PATH)
adapter = Adapter()
self.mainloop = GLib.MainLoop()
th = threading.Thread(target=self.agent_loop)
th.start()
def stop(self):
self.agnt_mngr.UnregisterAgent(AGENT_PATH)
self.mainloop.quit()
def set_passkey(self,user_passkey):
global passkey
passkey= user_passkey |
Beta Was this translation helpful? Give feedback.
5 replies
-
Does bleak development team have plan to integrate class BluetoothAgent to bleak formal release? Or does bleak development team have plans to support pair with key features? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am looking to pair with a device I know its passkey and I would like to know if its possible to pair with a given passkey.
Beta Was this translation helpful? Give feedback.
All reactions