Trying to open a URL on Android using an intent

Posted on
  • I love tinkering with the Bangle2. It's so fun.

    My goal is to write an app that opens a URL in the browser on my Android device. I was hoping the right way to do this is to send an intent with code like this. I'm unsure what the correct intent would look like, and tried a variety of ideas but none seemed to work. However, there is a weird thing in the Android ADB logs that suggest perhaps this isn't the issue anyway.

    const reddit = JSON.stringify({t:"intent",       
                                   category:"android.intent.category.DEFAUL­T", 
                                   target:"activity", 
                                   data: "https://reddit.com", 
                                   action: "android.intent.action.VIEW"});
    
    const reddit2 = JSON.stringify({
      t:"intent", 
      data: "https://reddit.com", 
      action: "ACTION_VIEW"});
    
    const reddit3 = JSON.stringify({t:"intent", data: "https://reddit.com", action: "android.intent.action.VIEW"});
    
    E.showPrompt("Open url?",{
      title:"Open URL",
      buttons : {"One":1,"Two":2,"Three": 3}
    }).then(function(v) {
      if (v===1) {
       E.showMessage("reddit");
       Bluetooth.println(reddit);
      } else if (v===2) {
       E.showMessage("reddit2");
       Bluetooth.println(reddit2);
      } else {
       E.showMessage("reddit3");
        Bluetooth.println(reddit3));
      }
    });
    

    Strangely, when I look at the android adb logs I see this:

    10-05 00:02:32.980 16912 10602 D nodomain.freeyourgadget.gadgetbridge.ser­vice.btle.BtLEQueue: characteristic changed: 6e400003-b5a3-f393-e0a9-e50e24dcca9e value: 0x7b 0x22 0x74 0x22 0x3a 0x22 0x69 0x6e2
    10-05 00:02:32.980 16912 10602 I nodomain.freeyourgadget.gadgetbridge.ser­vice.devices.banglejs.BangleJSDeviceSupp­ort: RX: {"t":"intent","data"
    10-05 00:02:32.983 16912 10602 D nodomain.freeyourgadget.gadgetbridge.ser­vice.btle.BtLEQueue: characteristic changed: 6e400003-b5a3-f393-e0a9-e50e24dcca9e value: 0x3a 0x22 0x68 0x74 0x74 0x70 0x73 0x3af
    10-05 00:02:32.983 16912 10602 I nodomain.freeyourgadget.gadgetbridge.ser­vice.devices.banglejs.BangleJSDeviceSupp­ort: RX: :"https://reddit
    10-05 00:02:32.984 16912 10602 D nodomain.freeyourgadget.gadgetbridge.ser­vice.btle.BtLEQueue: characteristic changed: 6e400003-b5a3-f393-e0a9-e50e24dcca9e value: 0x64 0x61 0x74 0x61 0x3d 0x22 0x2c 0x224
    10-05 00:02:32.985 16912 10602 I nodomain.freeyourgadget.gadgetbridge.ser­vice.devices.banglejs.BangleJSDeviceSupp­ort: RX: ","action":"ACT
    10-05 00:02:32.986 16912 10602 D nodomain.freeyourgadget.gadgetbridge.ser­vice.btle.BtLEQueue: characteristic changed: 6e400003-b5a3-f393-e0a9-e50e24dcca9e value: 0x49 0x4f 0x4e 0x5f 0x56 0x49 0x45 0x57a
    10-05 00:02:32.986 16912 10602 I nodomain.freeyourgadget.gadgetbridge.ser­vice.devices.banglejs.BangleJSDeviceSupp­ort: RX: ION_VIEW"}
    10-05 00:02:32.986 16912 10602 I nodomain.freeyourgadget.gadgetbridge.ser­vice.devices.banglejs.BangleJSDeviceSupp­ort: UART RX LINE: >{"t":"intent","data":"https://reddit.co­m","action":"ACTION_VIEW}
    10-05 00:02:32.987 16912 10602 I nodomain.freeyourgadget.gadgetbridge.ser­vice.devices.banglejs.BangleJSDeviceSupp­ort: UART RX line started with 62 - ignoring
    

    In other words, the code to receive the JSON is getting a "<" character at the front of it, which means it hits this line and does not try to parse it.

    https://github.com/gfwilliams/Gadgetbrid­ge/commit/60c7f79366f3d62df711bd0d87c333­bd8e67f11d#diff-28d26af2b02f3df362db1ead­5920145a8d90650859a256590ddf1e0926d3368b­R425

    (

    LOG.info("UART RX line started with "+(int)line.charAt(0)+" - ignoring");
    

    )

    Is there something I'm doing wrong with Bluetooth.print() or JSON.stringify() that is adding this extra character? Or, a work around?

  • Hi!

    What Gadgetbridge version do you use? What android version are you on? Have you given Gadgetbridge all the permissions it asks for? Have you cheched the box for 'Allow Intents' in Bangle.js specific settings in Gadgetbridge?

    Does the log say "Sending intent:" or "Executing intent:" somewhere?

    If you are on a newer nightly release of Gadgetbridge there are small changes to how things are handled. Mainly, the category keyword is changed to categories and now takes a list of strings instead of just a string and the user can set any flags they want. See more here and here.

    Looking at your test code I think your first attempt looks alright, I would test it myself but my bangle is acting up at the moment. The other two will not work per design. With both reddit2 and reddit3 Gadgetbridge will default to broadcasting while what you need is to start an activity with target:"activity". On reddit2 the action can't be the short representation,"ACTION_VIEW", used - I believe.

  • I tried your intent and got the attached log. At the end there I get an instruction to use the flag FLAG_ACTIVITY_NEW_TASK. If you do to you're probably on a newer nightly Gadgetbridge release and you need to add that flag yourself with flags:["FLAG_ACTIVITY_NEW_TASK"].


    1 Attachment

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

Trying to open a URL on Android using an intent

Posted by Avatar for user149778 @user149778

Actions