Search code examples
raspberry-pidbusa2dpavrcpmediaplayback

Raspberry Pi + Bluez + A2DP + AVRCP


In the past 2 days i've been googling for a way to control the media playback from my iPhone through my Raspiberry pi headless speaker.

I've successfully setup the pi using the following tutorial: Headless A2DP Audio Streaming on Raspbian Stretch and now i'm looking for a way to control the playback of the audio stream from my raspberry pi.

I've been looking into MediaTransport1, MediaPlayer1, MediaControl1 but unfortunately i wasn't successful.

to give you a few examples of the trial and error I've performed:

for:

dbus-send --system --print-reply --dest=org.bluez /org/bluez/hci0/dev_C8_85_50_B1_C8_6B org.bluez.MediaPlayer1.Pause

i received:

Error org.freedesktop.DBus.Error.UnknownMethod: Method "Pause" with signature "" on interface "org.bluez.MediaPlayer1" doesn't exist

for

qdbus --system org.bluez /org/bluez/hci0/dev_C8_85_50_B1_C8_6B/fd0 org.freedesktop.DBus.Properties.Set org.bluez.MediaTransport1 Volume 127

i received

Error: org.freedesktop.DBus.Error.InvalidSignature
Invalid signature for 'Volume'

Additionally, i've found a player that i thought i could use to build my own script as an example Here but i've got an error:

pi@raspberrypi:~/blueutils $ python simpleplayer.py


Available commands:
        PropertiesChanged(interface, properties)
        help(cmd)

Use python syntax to pass arguments to available methods.
E.g.: PropertiesChanged({'Metadata' : {'Title': 'My title',             'Album': 'my album' }})

>>> <Interface <ProxyObject wrapping <dbus._dbus.SystemBus (system) at 0xb61968d0> :1.13 /org/bluez/hci0 at 0xb619db70> implementing 'org.bluez.Media1' at 0xb619dc50>
Traceback (most recent call last):
  File "simpleplayer.py", line 197, in <module>
    media.RegisterPlayer(dbus.ObjectPath(path), player.properties)
  File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
    return self._proxy_method(*args, **keywords)
  File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
    **keywords)
  File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: org.bluez.Error.NotSupported: Operation is not supported

when i do an introspect

dbus-send --system --print-reply --dest=org.bluez /org/bluez/hci0 org.freedesktop.DBus.Introspectable.Introspect

i get his:

<node>
    <interface name="org.freedesktop.DBus.Introspectable">
        <method name="Introspect">
            <arg name="xml" type="s" direction="out"/>
        </method>
    </interface>
    <interface name="org.bluez.Adapter1">
        <method name="StartDiscovery"></method>
        <method name="SetDiscoveryFilter">
            <arg name="properties" type="a{sv}" direction="in"/>
        </method>
        <method name="StopDiscovery"></method>
        <method name="RemoveDevice">
            <arg name="device" type="o" direction="in"/>
        </method>
        <property name="Address" type="s" access="read"></property>
        <property name="Name" type="s" access="read"></property>
        <property name="Alias" type="s" access="readwrite"></property>
        <property name="Class" type="u" access="read"></property>
        <property name="Powered" type="b" access="readwrite"></property>
        <property name="Discoverable" type="b" access="readwrite"></property>
        <property name="DiscoverableTimeout" type="u" access="readwrite"></property>]

        <property name="Pairable" type="b" access="readwrite"></property>
        <property name="PairableTimeout" type="u" access="readwrite"></property>
        <property name="Discovering" type="b" access="read"></property>
        <property name="UUIDs" type="as" access="read"></property>
        <property name="Modalias" type="s" access="read"></property>
    </interface>
    <interface name="org.freedesktop.DBus.Properties">
        <method name="Get">
            <arg name="interface" type="s" direction="in"/>
            <arg name="name" type="s" direction="in"/>
            <arg name="value" type="v" direction="out"/>
        </method>
        <method name="Set">
            <arg name="interface" type="s" direction="in"/>
            <arg name="name" type="s" direction="in"/>
            <arg name="value" type="v" direction="in"/>
        </method>
        <method name="GetAll">
            <arg name="interface" type="s" direction="in"/>
            <arg name="properties" type="a{sv}" direction="out"/>
        </method>
        <signal name="PropertiesChanged">
            <arg name="interface" type="s"/>
            <arg name="changed_properties" type="a{sv}"/>
            <arg name="invalidated_properties" type="as"/>
        </signal>
    </interface>
    <interface name="org.bluez.GattManager1">
        <method name="RegisterApplication">
            <arg name="application" type="o" direction="in"/>
            <arg name="options" type="a{sv}" direction="in"/>
        </method>
        <method name="UnregisterApplication">
            <arg name="application" type="o" direction="in"/>
        </method>
    </interface>
    <interface name="org.bluez.Media1">
        <method name="RegisterEndpoint">
            <arg name="endpoint" type="o" direction="in"/>
            <arg name="properties" type="a{sv}" direction="in"/>
        </method>
        <method name="UnregisterEndpoint">
            <arg name="endpoint" type="o" direction="in"/>
        </method>
        <method name="RegisterPlayer">
            <arg name="player" type="o" direction="in"/>
            <arg name="properties" type="a{sv}" direction="in"/>
        </method>
        <method name="UnregisterPlayer">
            <arg name="player" type="o" direction="in"/>
        </method>
    </interface>
    <node name="dev_40_9C_28_07_63_DD"/>
    <node name="dev_C8_85_50_B1_C8_6B"/>
</node>

Can anybody help me figure this issue out, or does anybody know a procedure to control the playback of the music on the phone, using the raspberry pi ?

Regards.

Update 1: Thank you so much @Parthiban for it works perfectly.

Additionally, i have to say that i reinstalled the bluez version to 5.50 by following the tutorial here: Install bluez on the Raspberry Pi as i wasn't able to see /playerX (0 in my case). Before i compiled the sources, i did a little extra (not sure it's relevant but it doesn't hurt), i added the .auto_connect = true

the the avrcp profiles as indicated :

Here

hope this helps everybody that may face my problem


Solution

  • You need to specify the signature to set the volume as below,

    dbus-send --system --print-reply --type=method_call --dest='org.bluez' '/org/bluez/hci0/dev_C8_85_50_B1_C8_6B/fd0' org.freedesktop.DBus.Properties.Set string:"org.bluez.MediaTransport1" string:"Volume" variant:uint16:127
    

    This is because, as mentioned in dbus specification you need to specify three arguments when using set,

    1. Interface Name
    2. Property Name
    3. Property value as variant

    For the pause method, explore the "org.bluez.MediaPlayer1" interface of your device using "org.freedesktop.DBus.Introspectable.Introspect". The object path which you have used to access the method "Pause" is wrong. As stated here, you need to use "[variable prefix]/{hci0,hci1,...}/dev_XX_XX_XX_XX_XX_XX/playerX".

    So in your case, it should be,

    dbus-send --system --print-reply --type=method_call --dest='org.bluez' '/org/bluez/hci0/dev_C8_85_50_B1_C8_6B/playerX' org.bluez.MediaPlayer1.Pause
    

    Where playerX needs to be replaced by your device player (you can get that from Introspect or watch on "InterfaceAdded" Signal.