Arduino - Raspberry Pi, Bluetooth Connection Using D-bus Api
Task is to automate pairing and connection process between Arduino and Raspberry Pi over Bluetooth using D-BUS API based on python script. The Bluetooth module connected to Arduino
Solution 1:
I think you had answered your own question. The reason that a Connect
doesn't work is because you have no Serial Port Profile (SPP) running on the Raspberry Pi. If you have btmon
running you will see that the client disconnects because there is no matching profile with what the Arduino is offering.
The port number used in the Python Socket connection needs to match the port number on the Arduino Bluetooth module. This is probably why only 1
is working.
For reference I tested this with a Raspberry Pi and HC-06 I had laying around. I removed all the scanning code to try to get to the minimum runnable code. Here is the code I used:
import socket
from time import sleep
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GLib
BUS_NAME = 'org.bluez'
AGENT_IFACE = 'org.bluez.Agent1'
AGNT_MNGR_IFACE = 'org.bluez.AgentManager1'
ADAPTER_IFACE = 'org.bluez.Adapter1'
AGENT_PATH = '/ukBaz/bluezero/agent'
AGNT_MNGR_PATH = '/org/bluez'
DEVICE_IFACE = 'org.bluez.Device1'
CAPABILITY = 'KeyboardDisplay'
my_adapter_address = '11:22:33:44:55:66'
my_device_path = '/org/bluez/hci0/dev_00_00_12_34_56_78'
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
classAgent(dbus.service.Object):
@dbus.service.method(AGENT_IFACE,
in_signature='o', out_signature='s')defRequestPinCode(self, device):
print(f'RequestPinCode {device}')
return'0000'classDevice:
def__init__(self, device_path):
dev_obj = bus.get_object(BUS_NAME, device_path)
self.methods = dbus.Interface(dev_obj, DEVICE_IFACE)
self.props = dbus.Interface(dev_obj, dbus.PROPERTIES_IFACE)
self._port = 1
self._client_sock = socket.socket(socket.AF_BLUETOOTH,
socket.SOCK_STREAM,
socket.BTPROTO_RFCOMM)
defconnect(self):
# self.methods.Connect()
self._client_sock.bind((my_adapter_address, self._port))
self._client_sock.connect((self.address, self._port))
defdisconnect(self):
self.methods.Disconnect()
defpair(self):
self.methods.Pair(reply_handler=self._pair_reply,
error_handler=self._pair_error)
def_pair_reply(self):
print(f'Device trusted={self.trusted}, connected={self.connected}')
self.trusted = Trueprint(f'Device trusted={self.trusted}, connected={self.connected}')
while self.connected:
sleep(0.5)
self.connect()
print('Successfully paired and connected')
def_pair_error(self, error):
err_name = error.get_dbus_name()
print(f'Creating device failed: {err_name}')
@propertydeftrusted(self):
returnbool(self.props.Get(DEVICE_IFACE, 'Trusted'))
@trusted.setterdeftrusted(self, value):
self.props.Set(DEVICE_IFACE, 'Trusted', bool(value))
@propertydefpaired(self):
returnbool(self.props.Get(DEVICE_IFACE, 'Paired'))
@propertydefconnected(self):
returnbool(self.props.Get(DEVICE_IFACE, 'Connected'))
@propertydefaddress(self):
returnstr(self.props.Get(DEVICE_IFACE, 'Address'))
if __name__ == '__main__':
agent = Agent(bus, AGENT_PATH)
agnt_mngr = dbus.Interface(bus.get_object(BUS_NAME, AGNT_MNGR_PATH),
AGNT_MNGR_IFACE)
agnt_mngr.RegisterAgent(AGENT_PATH, CAPABILITY)
device = Device(my_device_path)
if device.paired:
device.connect()
else:
device.pair()
mainloop = GLib.MainLoop()
try:
mainloop.run()
except KeyboardInterrupt:
agnt_mngr.UnregisterAgent(AGENT_PATH)
mainloop.quit()
Post a Comment for "Arduino - Raspberry Pi, Bluetooth Connection Using D-bus Api"