Android Wear向Android手机发送消息

时间:2017-11-08 17:22:44

标签: android wear-os

我是Android Wear开发的新手。我正在尝试让智能手表将一个字符串发送到移动应用程序,但我不知道它是如何工作的。我试图在一些教程之后做到这一点,但仍然无效。

Android移动清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="manuela.com.messagewearableandroid">

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">

        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android Wear清单

public class ListenerService extends WearableListenerService {
    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        showToast(messageEvent.getPath());
        System.out.println("Arrivato");
    }
    private void showToast(String message) {

        System.out.println("Arrivato");
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

适用于移动设备的ListenerService

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initApi();

    Button button = (Button) findViewById(R.id.btn_toast);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            /**
             * Sets up the button for handling click events.
             */
            sendToast();

        }
    });
}

/**
 * Initializes the GoogleApiClient and gets the Node ID of the connected device.
 */
private void initApi() {
    client = getGoogleApiClient(this);
    retrieveDeviceNode();
}


/**
 * Returns a GoogleApiClient that can access the Wear API.
 * @param context
 * @return A GoogleApiClient that can make calls to the Wear API
 */
private GoogleApiClient getGoogleApiClient(Context context) {
    return new GoogleApiClient.Builder(context)
            .addApi(Wearable.API)
            .build();
}

/**
 * Connects to the GoogleApiClient and retrieves the connected device's Node ID. If there are
 * multiple connected devices, the first Node ID is returned.
 */
private void retrieveDeviceNode() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
            NodeApi.GetConnectedNodesResult result =
                    Wearable.NodeApi.getConnectedNodes(client).await();
            List<Node> nodes = result.getNodes();
            if (nodes.size() > 0) {
                nodeId = nodes.get(0).getId();
            }
            client.disconnect();
        }
    }).start();
}

/**
 * Sends a message to the connected mobile device, telling it to show a Toast.
 */
private void sendToast() {
    if (nodeId != null) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
                Wearable.MessageApi.sendMessage(client, nodeId, MESSAGE, null);
                System.out.println("Mandato");
                client.disconnect();
            }
        }).start();
    }
}

穿着的主要活动

ID <- c("X","X","Y","Z")

A <- c(1,0,2,0)

B <- c(1,1,0,0) 

C <- c(1,0,0,0)

df <- data.frame(ID,A,B,C)    

1 个答案:

答案 0 :(得分:0)

您可以关注此<{3}} 发送和接收消息。您使用documentation发送消息并将以下项目附加到消息中:

  • 任意有效负载(可选)
  • 唯一标识邮件操作的路径

这是MessageApi的另一个参考。