image/svg+xml

Control Kodi with Tasker and Amazfit BIP

A couple weeks ago I have purchased the Xiaomi Amazfit BIP smartwatch. It is a very cheap (50$) and simple watch with a GPS sensor, heart rate monitoring, sleep tracking, step counter and an enormous battery life of multiple weeks! The long battery life is possible through the always-on display which seems to be based on an e-ink display of ebook readers like the Amazon Kindle. In contrast to the ebook readers this display also shows a low range of colors.

Xiaomi Amazfit BIP

I thought it would be very convenient to pause and resume the Kodi playback on my Raspberry Pi. This can be realized with the Android app Tasker and the Tools & Amazfit.

To get it work create on your phone a new Javascript file with this content:

const REQ_URL = 'http://IP_ADDRESS_OF_KODI:PORT/jsonrpc?request=';

(function () {
    flash("Button count: " + button_count);
    // ================ BUTTON COUNTER ================ //
    if (button_count == 2)
    {
        var url = REQ_URL + '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}';

        fetchURL(url, data => {
            if (data.result.length > 0)
                playOrPause();
        });
    }

    exit();
})();

// ================ HELPER FUNCTIONS ================ //
function playOrPause()
{
    var isPlayingURL = REQ_URL + '{"jsonrpc": "2.0", "method": "XBMC.GetInfoBooleans", "params": {"booleans": ["Player.Playing"]}, "id": 1}';
    flash("PlayPause");
    fetchURL(isPlayingURL, data => {
        if (data.result['Player.Playing'] === true)
        {
            var pauseURL = REQ_URL + '{"jsonrpc":"2.0","method":"Player.PlayPause","params":{"playerid":1,"play":false},"id":1}';

            var fetchPause = function()
            {
                fetchURL(pauseURL, data => {
                    // Check if playback has been really paused
                    fetchURL(isPlayingURL, data => {
                        flashLong("Is Playing: " + data.result['Player.Playing']);
                        if (data.result['Player.Playing'] === true)
                        {
                            // Still not paused, fetch pause again
                            fetchPause();
                        }
                    });
                });
            }

            fetchPause();
        }
        else
        {
            var resumeURL = REQ_URL + '{"jsonrpc":"2.0","method":"Player.PlayPause","params":{"playerid":1,"play":true},"id":1}';
            var data = fetch(resumeURL);
        }
    });
}

function fetchURL(url, callback)
{
    fetch(url)
        .then(response => response.json())
        .then(data => {
            callback(data);
    });
}

Don’t forget to replace the IP address of your Kodi machine in the first line. We are working here with the JSON-RPC API of Kodi. You can test the API by calling this URL:

http://IP_ADDRESS_OF_KODI:PORT/jsonrpc?request={"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}

More examples of API calls can be found in the Kodi wiki. The whole possibilities of the API can be seen here.

When we press the button on the Amazfit BIP two times in a row, the variable button_count gets filled with the amount of button presses. Then we fetch the URLs for the current running media and the status of it (playing or paused). After this a command to either play or pause the media will be sent. The code section with the fetchPause() function catches the moment when the pause has not been performed properly. This happens when the playback has been accelerated, then we need to press pause two times. But sadly this seems only to work in the browser. On my BIP I need to press the button 4 times to pause an accelerated playback.

Now create a Tasker profile with an event when the button on the Amazfit BIP is pressed. Create after this a new task for a Javascript file and link to the file with the code above. Make sure to remove the checkmark on “Auto Exit”. Return to Taskers main screen and apply the changes by pressing the checkmark at the top. You are done!

The possibilities with Tasker and Tools & Amazfit are endless. If you would like to you can change the pause and resume with another Kodi action.