Question

I'm new with smartwatch development. I want to try to send string from hostapp to smartextension. Based on tutorial given by android, I have made simple application that can take the string and send with intent to another activity.

Right now, I want to extends it So it can communicates with smartwatch. In application intentHostapp. I have mainactivity.java :

   package com.example.myfirstapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
    public final static String buzzIntent = "com.example.myfirstapp.buzzintent";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
    Context mContext;


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {

        Intent intentBuzz = new Intent();
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String buzzIntent = editText.getText().toString();
        intentBuzz.setAction(buzzIntent);
        startService(intentBuzz); 
    }

}

I took those codes from this question.

In this part Do I have to make some Service class for my Application?

In another application, I have classes that I have made 5 classes:

  1. HelloWatchExtension : My idea is to make the String in here
  2. HelloWatchExtension Receiver: extends from Broadcast Receiver
  3. HelloWatchExtension Service: for smartextension Broadcastservice
  4. SRegistrationInformation: for registration them smartextension to smart connect(I think) I have declare HelloWatchExtension for received the intent

    package com.example.hellowatch;

    public class HelloWatchExtension extends ControlExtension{
    
    int width;
    int height;
    
    
    
    RelativeLayout layout;
    Canvas canvas;
    Bitmap bitmap;
    TextView textView;
    
    public void onCreate()
    {
        Intent intent = new Intent("com.example.myfirstapp.buzzintent");
        String message = intent.getAction();
        textView.setText(message);
    
    }
    public HelloWatchExtension(Context context, String hostAppPackageName) {
        super(context, hostAppPackageName);
        width = getSupportedControlWidth(context);
        height = getSupportedControlHeight(context);
        layout = new RelativeLayout(context);
        textView = new TextView(context);
    
    
    
        textView.setTextSize(9);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Color.WHITE);
        textView.layout(0, 0, width, height);
    
    }
    
    
    @Override
    public void onResume() {
        updateVisual();
    }
    private void updateVisual() {
    
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        layout.draw(canvas);
    
        showBitmap(bitmap);
    }
    
    public static int getSupportedControlWidth(Context context) {
        return context.getResources().getDimensionPixelSize(
                R.dimen.smart_watch_control_width);
    }
    
    public static int getSupportedControlHeight(Context context) {
        return context.getResources().getDimensionPixelSize(
                R.dimen.smart_watch_control_height);
    }
    

    }

Those are classes I saw in the documentation, but I don't know to put receiver for intent from the HostApp?

Edit:

I still couldn't solve my problem. I don't know what mistake that I have made, As Marlin said, I determine 3 points: 1. make a Service to register your extension in the host app

public class HelloWatchRegistrationInformation extends RegistrationInformation {
    final Context context;

    protected HelloWatchRegistrationInformation(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }
        this.context = context;
    }

    @Override
    public ContentValues getExtensionRegistrationConfiguration() {
        String extensionIcon = ExtensionUtils.getUriString(context,
                R.drawable.ic_extension);
        String iconHostapp = ExtensionUtils.getUriString(context,
                R.drawable.ic_launcher);

        String configurationText = context.getString(R.string.configuration_text);
        String extensionName = context.getString(R.string.extension_name);

        ContentValues values = new ContentValues();

        values.put(Registration.ExtensionColumns.CONFIGURATION_TEXT, configurationText);
        values.put(Registration.ExtensionColumns.EXTENSION_ICON_URI, extensionIcon);
        values.put(Registration.ExtensionColumns.EXTENSION_KEY,
                HelloWatchExtensionService.EXTENSION_KEY);
        values.put(Registration.ExtensionColumns.HOST_APP_ICON_URI, iconHostapp);
        values.put(Registration.ExtensionColumns.NAME, extensionName);
        values.put(Registration.ExtensionColumns.NOTIFICATION_API_VERSION,
                getRequiredNotificationApiVersion());
        values.put(Registration.ExtensionColumns.PACKAGE_NAME, context.getPackageName());

        return values;
    }

    @Override
    public int getRequiredNotificationApiVersion() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getRequiredWidgetApiVersion() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getRequiredControlApiVersion() {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public int getRequiredSensorApiVersion() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isDisplaySizeSupported(int width, int height) {
        return ((width == HelloWatchExtension.getSupportedControlWidth(context) && height == HelloWatchExtension
                .getSupportedControlHeight(context)));
    }
}

2.. Declare a BroadcastReceiver in your AndroidManifest.xml *

...

<service android:name=".HelloWatchExtensionService" />
        <receiver android:name=".HelloWatchExtensionReceiver" >
            <intent-filter>

                <!-- Generic extension intents. -->
                <action android:name="com.sonyericsson.extras.liveware.aef.registration.EXTENSION_REGISTER_REQUEST" />
                <action android:name="com.sonyericsson.extras.liveware.aef.registration.ACCESSORY_CONNECTION" />
                <action android:name="android.intent.action.LOCALE_CHANGED" />

                <!-- Notification intents -->
                <action android:name="com.sonyericsson.extras.liveware.aef.notification.VIEW_EVENT_DETAIL" />
                <action android:name="com.sonyericsson.extras.liveware.aef.notification.REFRESH_REQUEST" />

                <!-- Widget intents -->
                <action android:name="com.sonyericsson.extras.aef.widget.START_REFRESH_IMAGE_REQUEST" />
                <action android:name="com.sonyericsson.extras.aef.widget.STOP_REFRESH_IMAGE_REQUEST" />
                <action android:name="com.sonyericsson.extras.aef.widget.ONTOUCH" />
                <action android:name="com.sonyericsson.extras.liveware.extension.util.widget.scheduled.refresh" />

                <!-- Control intents -->
                <action android:name="com.sonyericsson.extras.aef.control.START" />
                <action android:name="com.sonyericsson.extras.aef.control.STOP" />
                <action android:name="com.sonyericsson.extras.aef.control.PAUSE" />
                <action android:name="com.sonyericsson.extras.aef.control.RESUME" />
                <action android:name="com.sonyericsson.extras.aef.control.ERROR" />
                <action android:name="com.sonyericsson.extras.aef.control.KEY_EVENT" />
                <action android:name="com.sonyericsson.extras.aef.control.TOUCH_EVENT" />
                <action android:name="com.sonyericsson.extras.aef.control.SWIPE_EVENT" />

                <!-- From SmartPhone  -->
                <action android:name="com.example.myfirstapp.buzzintent"/>

            </intent-filter>
        </receiver>
    </application>

... 3. in the onReceive() method start your service.

@Override
    public void onReceive(Context context, final Intent intent) {
        //Log.d(HelloWatchExtensionReceiver.TAG,"onReceive:  " + intent.getAction());
        intent.setClass(context, HelloWatchExtensionService.class);
        context.startService(intent);

    }
Was it helpful?

Solution

Have you looked at the sample code within the Sony Add-on SDK, specifically the SampleControlExtension? That would be a good example to start with.

Regarding your questions:

Sending a string from your app to the extension - it depends what you want to do with that string. If you wanted to show it on the screen of the watch then look for the showBitmap() method in the sample code I mentioned above for an example.

Sending data from extension to your app - this is the same as how you would do it in a standard Android app. Basically in your extension you would broadcast the Intent, something like this:

Intent intentBuzz = new Intent();
intentBuzz.setAction(buzzIntent);
mContext.sendBroadcast(intentBuzz); 

Then in your app Activity register a broadcast receiver:

buzzReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
    buzzAction();
}
};
registerReceiver(buzzReceiver, new IntentFilter(buzzIntent));

Edit:

I realized that I may have misunderstood your original question. It sounds like you are just trying to register your extension with the host app so that it shows up on the watch, is that correct? Since the host app is a Sony application, there is no way to modify the code for it, so you can't send a string from the host app to your extension or vice versa. If you are talking about sending a string from your application running on the phone/tablet to your extension running on the watch itself, then my answer above is correct.

To answer the original question then, you do need to make a Service to register your extension in the host app so that it shows up on the watch. See the SampleExtensionService.java in the example code.

As far as receiving the broadcast to register from the host app, you will need to declare a BroadcastReceiver in your AndroidManifest.xml and then in the onReceive() method start your service. Again, best way to see this is to look at the ExtensionReceiver.java class in the sample.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top