我们仍然使用BlueZ来建立连接。
BlueZ给我们提供了方便的DBus调用接口,使得我们可以方便地使用Python来进行调用。
对象的获取流程如下:(可以使用D-Feet来进行查看)
1、bus -> manager -> adapter -> device
2、得到device对象后,可以获取 AudioSink/Audio/Headset/Control等对象
3、对AudioSink/Audio/Headset /Control等对象进行操作
=== 一段Python脚本例子 ===
mac = 'XX:XX:XX:XX:XX:XX'
import dbus
bus = dbus.SystemBus()
### bus -> manager -> adapter -> device ###
manager = dbus.Interface(bus.get_object('org.bluez', '/'), 'org.bluez.Manager')
adapters = manager.ListAdapters()
print adapters
if (len(adapters) < 1):
print "Please insert an adapter, and Run Me again."
sys.exit(1)
adapter = dbus.Interface(bus.get_object("org.bluez", manager.DefaultAdapter()),
"org.bluez.Adapter")
print adapter
try:
device = adapter.FindDevice(mac)
print "FindDevice: %s" % device
except:
device = adapter.CreateDevice(mac)
print "CreateDevice: %s" % device
device.SetProperty("Trusted", dbus.Boolean(1))
### AudioSink ###
audiosink = dbus.Interface(bus.get_object('org.bluez', device), 'org.bluez.AudioSink')
try:
audiosink.Connect()
print "Connect to AudioSink OK"
except:
if ( audiosink.IsConnected() ):
print "Connect to AudioSink Already"
else:
print "Connect to AudioSink Failed"
pass