Get message from Gadgetbridge

Posted on
  • I know how to push something from Bangle to Gadgetbridge.

    Bluetooth.println(JSON.stringify({t:"music", n:"play"}));
    

    How can I get something from Gadgetbride to Bangle (pulled from Bangle, not pushed by Gadgetbridge)?
    I would like to read music information in an app on the Bangle.

    Thanks

  • You just need to define a function called GB:

      global.GB = (event) => {
        switch (event.t) {
          case "musicinfo":
            ...
            break;
          case "musicstate":
            ...
            break;
        }
      };
    

    More info on what form the data takes is at http://www.espruino.com/Gadgetbridge

  • This would override the GB function if it's already defined, correct? What if multiple apps want to receive events?

  • Okay, I saw that code in the BG widget and I am using it in my app.
    However, I do not know how to trigger it.

    The problem is, that I have to read what is playing, when I start the app.
    Otherwise I cannot show title information and I do not know what the musicstate is.

    global.GB = function(j) {
      if (debug ==1) print("global.GB");
      switch (j.t) {
        case "musicinfo":
          musicInfo = j;
          draw();
          break;
        case "musicstate":
          musicState = j.state;
          draw();
          break;
      }
    };
    
  • This would override the GB function if it's already defined, correct? What if multiple apps want to receive events?

    Yes - however since only one app runs at a time you're kindof ok. The only potential conflict is Widgets - if you're making a 2nd widget that exists alongside Gadgetbridge, or if your app is loading widgets then there's a problem.

    What I'd suggested in another post was just to change everything to do:

    (function() {
      var _GB = global.GB;
      global.GB = function(j) {
        // ...
        if (_GB)_GB(j);
      }
    })()
    

    Since we're never going to have loads of them active it might be the most sensible solution for now.

    However, I do not know how to trigger it.

    Just make sure Gadgetbridge is connected to Bangle.js and you're sorted!

    The problem is, that I have to read what is playing, when I start the app.

    I think the best way to do this is just to send a pause command to gadgetbridge, followed by a play command. That'll cause an update in play state, which will send new data.

  • I'm having the exact same problem as Purple-Tentacle was having. I can get gadgetbridge to control music on my Android phone, but I can't figure out how to get it to return info such as the artist name, etc. How do you use the GB function to do that? The following (taken from the espruino gadgetbridge docs) just gives "My Artist" and not the name of the artist of the music being played.

    GB({"t":"musicinfo","artist":"My Artist","album":"My Album","track":"Track One","dur":241,"c":2,"n":2})
    
  • The code you posted above is just for testing (if you paste that in the left of the web IDE). In reality Gadgetbridge should send a string a bit like that, but with artist/etc all filled in correctly. At least it does for me when music is playing. You could check with the GB Debug app.

  • I found the problem - it was the music-player I was using. I switched to the current most popular Android one and the data then started turning up. Oddly, the 'Gadgetbridge Music Controls' app had worked when I tried it before, but was a bit flaky. Anyway, thanks for the help.

  • Great! Just so we know...

    I switched to the current most popular Android one

    Which one was that?

  • Muzio Player. (On a second look, probably not the most popular player.) The one not working was Pulsar.

  • Those were from the Play Store. So I've spent a bit too much time since trying the different Music Players on F-Droid, of which there's twenty or so! Most are Gadgetbridge friendly, although not all of them behave as they should with music events. ie. You'd ask for the previous track and the player would switch to it but no musicinfo event would be received acknowledging the change. But a lot did behave, such as VCL and a more lightweight one called Auxio. I've written a little app for testing music players, which I'll get around to adding to github soon.

    I've set myself the task of getting the Bangle 2 to handle music like my aging Pebble does. With the Pebble, I can not only control the phone's music players and FM Radio, but switch between them, thanks to the Android app Music Boss. I'm guessing switching between apps like that isn't possible with gadgetbridge yet?

  • I'm guessing switching between apps like that isn't possible with gadgetbridge yet?

    I think it might be - at least for some players. Gadgetbridge can now send 'intents': http://www.espruino.com/Gadgetbridge#intents

    And many Android apps provide ways for you to send an intent to get them to start up and do what you want. Worst case you could just send an intent to Tasker, and get Tasker to do what you want

  • Thanks! I'll look into that.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Get message from Gadgetbridge

Posted by Avatar for Purple-Tentacle @Purple-Tentacle

Actions