Gear 2 – How to Bring your App to ForeGround and Autolaunch.

gearwear

Many times you may be working on an app that needs to show some notifications to users as activities occur – even when a different app is on the foreground. E.g an app that reminds you to take your meds every 4 hours. Sometimes, you may need your gear app to launch its host counterpart (even when it is not currently running), and vice versa. This post  provides simple sample code to wake up your gear app and bring it to the foreground . It also covers how to autolaunch your host side app from the gear app  .

Use the Tizen Application Launch Command.

1.) Note your Application id. you can find this in your config.xml file like this

<tizen:application id="ZeGjfywvaa.gearapp" package="ZeGjfywvaa" required_version="2.2"/>

2.) Use the tizen.application.launch command to launch your app from background to foreground. This command takes your app id (above) as an argument .

tizen.application.launch("ZeGjfywvaa.gearapp", onsuccess);
function onsuccess() {
console.log("Application launched successfully");
}

Note that you can call this in some background process that evaluates your notification condition e.g a setInterval timer .

3.) Add the necessary priviledges to your config.xml

<tizen:privilege name="http://tizen.org/privilege/application.launch"/>
<tizen:setting background-support="enable" encryption="disable" hwkey-event="enable"/>

The first privilege allows you perform an application launch (from background to foreground) . The second allows your app to keep running in the background. e.g code that evaluates your notification condition.

See Also : How to Store Data in your Gear 2 Apps

Note: For best user experience always ensure your UI flow gives the user a direct and easy way to dismiss the notification. Feel free to share how you have used this snippet in the comments below.

One More thing – You can pass data when you Launch Your Gear App

Sometimes when you use the launch command to bring your gear app to the foreground, you may want a particular page to show up rather than the default homepage. Also when you launch a different app, you may also want to pass some data to it. This can be done using the appControl Object.

var appControlData = [new tizen.ApplicationControlData("values", ["Bango", "Bingo"])] ;
var appControl = new tizen.ApplicationControl( "http://tizen.org/appcontrol/operation/pick", null, "image/jpeg", null, appControlData);

tizen.application.launchAppControl(
appControl,
appid,
function() { console.log("launch application control succeed"); },
function(e) {console.log("launch application control failed. reason: " + e.message);
},appControlReplyCallback );

To read the data passed to an app when it is launched, you use the getRequestedAppControl() method.

var reqAppControl = tizen.application.getCurrentApplication().getRequestedAppControl();  if (reqAppControl) {
alert("Requester AppID : " + reqAppControl.appControl.operation );
var data = reqAppControl.appControl.data alert("Requester AppID : " + data[0].value[0]); }

More detail on this can be found in the tizen sdk help reference
Tizen Wearable Web App Programming > API References > Device API Reference > Application .

 AutoLaunch Your App

Update : it appears you can only autolaunch the host side app from your gear app but NOT the gear app from the host app.

You can configure  your host side app to be launched whenever a connection request is made. If you recall in the first tutorial, we use the SAP protocol in the HelloAccessory Tutorial to establish a connection between the gear and host apps. SAP runs on the host app, and “pipes” connection requests from the gear to the host app. I guess this is why it is able to autolaunch the host app in the event that it is closed. Given that there isnt an equivalent of SAP running on gear (for now), it is unclear if this autolaunch function works vice versa.

To autolaunch the Host app from your gear, you should modify the accessoryservices.xml file found in the Gear Project folder (res/xml/accessoryservices.xml) and include the autolaunchid (its full package name found in androidmanifest.xml) for the host app.

<serviceProfile
            id="/system/helloaccessory"
            name="helloaccessory"
            role="consumer"
            autoLaunchAppId = com.samsung.android.example.helloaccessoryprovider"
            version="2.0" >

See Also : Create rad touch gestures for your gear 2 apps using Hammer.js

About Vykthur

Mobile and Web App Developer and Researcher. Passionate about learning, teaching, and recently - writing.
This entry was posted in Tutorials, Wearables and tagged . Bookmark the permalink.
  • Tung Mai Le

    Hi, Thanks for your tute. There’s one point I’m not clear:

    “You can configure either your wearable side widget or your host side app to be launched whenever a connection request is made”

    My host app is running and the gear app is not, how do I make connection request to the gear app?
    I already added the part “autoLaunchAppId =…” to the xml file.

    Thanks

    • Vykthur

      Hi Tung Mai,

      Hi

      I havent done a whole load of integrated app design (Iv only focused on standalone app for now) . I’ll experiment more and update this post with sample code in the future.

      In the mean time, here are my thoughts

      In the HelloAccessoryProvider project, the host app (a service) listens for connections and responds. Particularly, in the OnRecieve method, it uses a handler to send a message to the gear app. In this section, a connection is made with the gear app to send some data …
      Snippet below.

      ===========================================================

      final HelloAccessoryProviderConnection uHandler = mConnectionsMap.get(Integer

      .parseInt(String.valueOf(mConnectionId)));

      if(uHandler == null){

      Log.e(TAG,”Error, can not get HelloAccessoryProviderConnection handler”);

      return;

      }

      new Thread(new Runnable() {

      public void run() {

      try {

      uHandler.send(HELLOACCESSORY_CHANNEL_ID, message.getBytes());

      } catch (IOException e) {

      e.printStackTrace();

      }

      }
      ==============================================================
      }).start();

      • Tung Mai Le

        Thanks for trying to answer, but my question is that if the Gear doesn’t initiate connection at all. The use case is: the user restart his Gear, the Gear app is not running after restarting, so can the phone app does something to either start connection to the Gear-app or wake the Gear-app up so that Gear-app start connection?

        • Bhadresh Devani

          Hi Tung Mai,

          How we can initiate connection request from host side. It means that first connection request send from samsung android device to gear. If you know then please give me hint.

  • Ben

    I have a similar question with my integrated app. I am trying to send an alarm message from my Android device to Gear 2 device. I already created an alarm widget on the Gear side. How do I launch that widget from the Android device? I tried the steps in this article but its not working for me.

    • Vykthur

      HI Ben,
      Thanks for your comment.
      Even though the documentation indicates that two way autoluanch is possible, it appears it isnt in practice.
      On the host side, the SAP framework “pipes” connection requests to the different apps and is probably responsible for autolaunching these apps if they are “dead” . On the gear side, it appears there isnt an SAP equivalent (yet) that can handle this autolaunch for the gear side apps hence the lack of results.
      I have updated the article to indicate the above.
      As soon as I get any further insights on how to solve the issue, I’ll update the article.

      -V.

  • Vu Anh Truong

    Did you have any update for this. I can’t make gear app auto launch.
    Thanks

  • fx

    useful article