SmartWatch 2

Use both the Control & Notification APIs for customised UIs in your SmartWatch 2 extension [code]

A few of you SmartWatch developers out there have recently asked us if it’s possible to use both the Control API and Notification API together in a single extension. The good news is – yes, you can use both of these Smart Extension APIs in a single SmartWatch extension, for any device that supports the Smart Extension APIs with both control and notification, including the SmartWatch 2. Read on to get the full details from Marlin Liew, Developer Technical Services Engineer at Sony, who provides a code example showing how to use both of these APIs in a single project.

Marlin Liew

Marlin Liew, Sony Developer Technical Services Engineer.

The Control API and Notification API are parts of the Smart Extension APIs, available through the Sony Add-on SDK. The Smart Extension APIs enable apps to access the general controls of a number of Sony Smart Accessories, such as SmartWatch 2 and the original SmartWatch. Note that apps for Sony’s smart accessories are called Smart Extensions in our terminology. The Control API, as the name implies, lets your Smart Extension take full control over the accessory’s display. The Notification API is used by simple event driven data providers, such as SMS, MMS, and missed calls.

As a developer, you may want to have notifications in, for example, your SmartWatch 2 extension, but you may also want the flexibility to customise the UI differently than the default Notification view.

Default Notification view for SmartWatch 2.

Default Notification view for SmartWatch 2.

At the moment, customisation of the Notification UI is not supported in the Smart Extension UI. This is because the host application is in charge of creating the UI for the device that it’s connected to. However, you can still use the Notification UI as a starting point, and then open a customised UI. To do this, start your extension in Notification mode, then, once a notification is displayed from the menu, launch a Control extension, where you can design the UI any way you like.

The following diagram provides an example of how a SmartWatch 2 extension application would use both the Notification and Control APIs. On the SmartWatch 2, the user would touch on the menu button of the notification window, then press 1 to launch and view the first message.

Control & Notification API diagram

Example of a SmartWatch 2 extension application flow using both the Notification and Control APIs.

How to integrate the Notification and Control APIs

Note: This tutorial assumes that you have a SmartWatch 2 extension already under development. For help on developing a new SmartWatch 2 extension, please refer to our How to create an app extension for SmartWatch 2 tutorial to get started. Also, you will need to compile the code example using Sony Add-on SDK v3.0 in order for it to run on the SmartWatch 2 with latest firmware and host app.

The easiest way to integrate the Notification and Control APIs in your existing extension is to first start with the Control extension template. This template is included as part of the Sony Add-on SDK package, and can be found in the folder /samples/SmartExtensions/SampleControlExtension. Once you have the template, follow the steps below:

1. Modify the SampleRegistrationInformation.java to tell the host application to support the Notification API. This is done in the method getRequiredNotificationApiVersion().

public int getRequiredNotificationApiVersion() {
return 1; //update this value to 1
}

2. Add the following line to getExtensionRegistrationConfiguration() to tell the extension to start in Notification mode:

values.put(Registration.ExtensionColumns.LAUNCH_MODE, Registration.LaunchMode.NOTIFICATION);</i>

Now you should be able to receive notifications in your Control extension.

Testing notifications in your Control extension
To send a sample event, we’ll copy the addData() method from the Notification sample and add it to SampleExtensionService.java:

private void addData() {
Log.d(LOG_TAG, "addData");
Random rand = new Random();
int index = rand.nextInt(5);
String name = NAMES[index];
String message = MESSAGE[index];
long time = System.currentTimeMillis();
long sourceId = NotificationUtil
.getSourceId(this, EXTENSION_SPECIFIC_ID);
if (sourceId == NotificationUtil.INVALID_ID) {
Log.e(LOG_TAG, "Failed to insert data");
return;
}

ContentValues eventValues = new ContentValues();
eventValues.put(Notification.EventColumns.EVENT_READ_STATUS, false);
eventValues.put(Notification.EventColumns.DISPLAY_NAME, name);
eventValues.put(Notification.EventColumns.MESSAGE, message);
eventValues.put(Notification.EventColumns.PERSONAL, 1);
eventValues.put(Notification.EventColumns.PUBLISHED_TIME, time);
eventValues.put(Notification.EventColumns.SOURCE_ID, sourceId);

try {
getContentResolver().insert(Notification.Event.URI, eventValues);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "Failed to insert event", e);
} catch (SecurityException e) {
Log.e(LOG_TAG, "Failed to insert event, is Live Ware Manager installed?", e);
} catch (SQLException e) {
Log.e(LOG_TAG, "Failed to insert event", e);
}
}

Now, just add a call to addData() in onCreate():

public void onCreate() {
super.onCreate();
addData();
Log.d(SampleExtensionService.LOG_TAG, "SampleControlService: onCreate");
}

Creating your menus
The final thing to do is create your menus and to open your Control extension from an item in the menu. To create your menu, add the following code to getSourceRegistrationConfiguration() in SampleRegistrationInformation.java:

sourceValues.put(Notification.SourceColumns.ACTION_ICON_1,
ExtensionUtils.getUriString(mContext, R.drawable.actions_1));

Here, R.drawable.actions_1 is the icon to be shown in the menu.

We also need to update the onViewEvent() method in SampleExtensionService.java to handle this action and send an Intent to start the Control:

if (Notification.SourceColumns.ACTION_1.equals(action)) {
Intent intent = new Intent(Control.Intents.CONTROL_START_REQUEST_INTENT);
intent.putExtra(Control.Intents.EXTRA_AEA_PACKAGE_NAME, "com.sony.samplecontrolnotification");
intent.setPackage("com.sonymobile.smartconnect.smartwatch2");
sendBroadcast(intent, Registration.HOSTAPP_PERMISSION);
}

Now, when you run your extension, it should start in Notification mode, as shown in the diagram above. The method addData() will be called to create a list of dummy notifications.  From here, select a notification to view its contents. Press the menu button to bring up the menu, then select “1”.  The Control part of the extension should now start and you can customize this to work the way you want.

***

We hope this has given you everything you need to add both the Control and Notification APIs into your extension. For further reference, check out our SmartWatch 2 Control and Notification API code example. If you have any questions about these Smart Extension APIs or about Smart Accessory extension development, please drop us a line below and we’ll get back to you as soon as we can.

More information

Comments 23

Sort by: Newest | Oldest | Most popular

  1. By kunal mehta

    #1

    HI Marlin,
    In my android application i need to develop a extension for Sony smart watch and i need to show a list. This list needs to be refresh when i got notification in my android device. How can i add data dynamically and generate a list in smart watch.I also need to have my list visible after lights goes off like custom smart watch face does.
    How can i achieve this??

  2. By Mirko Nell

    #4

    Hm, the interrupt listener gets events all the time without moving the watch at all. The Smartwach2 accelerometer is not working with that listenertype for me :(

    • By Marlin L

      #5

      Hi Mirko:

      I’m confused as to what you are referring to here. The above example does not have anything to do with the accelerometer. Can you link to what you are referring to?

      Also, have you looked at the sample code in the \samples\SmartExtensions\SampleSensorExtension of the Add-on SDK? That should show you how to register a listener for accelerometer data.

  3. By Kay Jean

    #6

    I try to implement this example. It works. thank you. But I have to set up LAUNCH_MODE to NOTIFICATION. I know that Being able to start in Control mode is a different use case.

    But I still need an entry point to my normal app when user click my app. ( it changes from control mode to notification mode )

    My question is …

    1. Do I have to write two apps? one works for notification , and another works for control? How to set two apps in one project ?

    2. I want to keep my control and just send message to normal sw2 notification. How to send my message to normal sw2 notification mechanism ?

    • By Marlin L

      #7

      Hi Kay:

      I believe you asked this question on StackOverflow as well? Please see my answer there.

  4. By A B

    #8

    How would the other way round work? e.g. i want to click on my smart extension icon and the Control part shall be opened. By action key i want to switch to the Notification part. is that possible?

    • By Marlin L

      #9

      Unfortunately at this time that is not possible due to a bug in the API which prevents the an extension using both Control and Notification APIs to start in Control mode. Will let you know if a fix becomes available.

      • By A B

        #10

        Thank You for your answer Marlin. I guess then the only option is to design an own notification layout as a Control and start it. or maybe two different extensions, one control and one notification that interact with each other.

  5. By Michael Diener

    #11

    if LaunchMode.CONTROL would work as expected there would be no need for this user-unfriendly workaround.

    • By Marlin L

      #12

      Not sure what you are talking about here by referring to this as a workaround? Being able to start in Control mode is a different use case than the code example given above.

    • By Michael Diener

      #13

      One more thing, this only would work if notifications are available at the moment.

  6. By Technosoft Degree

    #14

    long sourceId = NotificationUtil
    .getSourceId(this, EXTENSION_SPECIFIC_ID);
    get sourceId = -1 and it reurns with message “Failed to insert data”

    • By Technosoft Degree

      #15

      Hi,
      I solved that source Id issue but please help me to get rid out of this scenario which i explained below .
      i have successfully done what u have explained above and with example code.
      From that i found that notification occurred when data is added to content provide.
      Now i want to implement that thing in my main application from where i will get notification through gcm and want to show in smart watch extension.
      I also read Notification API documentation from developer world.
      but i am bit confuse what to register in main app or how to register for content provider to add my notification message.
      I googled all things but not got any relevant to this.
      You are the only hope for this.So Please reply me and help me into this.

      Thanks.

      • By Marlin L

        #16

        Hi Technosoft Degree:

        Did you look at the SampleNotificationExtension sample code in the /samples/SmartExtensions directory of the Sony Add-on SDK? That should show you how to add notifications to the SmartConnect content provider.

        • By Technosoft Degree

          #17

          Hello ,
          Thanks for the reply sir.
          I checked that SampleNotificationExtension sample code in the /samples/SmartExtensions directory of the Sony Add-on SDK.
          i run it successfully and also got the notification in my smart-watch 2.
          but as i mentioned in my previous question that i want notification in my smart watch when notification comes from gcm in my main app.
          so my question is that how do i connect my main application with notification extension.
          As per the sample code i found that notification occurred when data is added to content provider(in sample code addData() method).
          that i want to know sir how do i register that notification content provider to my main app to store my gcm notification and display into both main app and smart-watch extension.
          I m just confused in this thing how to add or pass any intent from main app.

          • By Marlin L

            #18

            Handling a GCM notification in your main activity displayed on the phone should be the same as if you weren’t developing a watch extension at all. In order to pass that notification to the notification extension part of your app, you can just use an Intent sent from your main activity and register a Broadcast Receiver in your NotificationExtension to pass the notification information between the two.

  7. By Kutbi Panki

    #19

    have errors in integrate the Notification and Control APIs.

    • By Marlin L

      #20

      What errors are you seeing? Were you able to compile and run the sample project?

  8. By Kutbi Panki

    #21

    I get a 404 File Not Found error when I try to download the example SmartWatch 2 project file.

    • By Anna Aleryd

      #22

      Hi,

      Sorry about this, the link is repaired now and you should be able to download the code example.

      Best regards,
      Anna from Developer World

  9. By Cindy Osmon

    #23

    I get a 404 File Not Found error when I try to download the example SmartWatch 2 project file.

1-23 of 23 comments.