【问题标题】:Android Wear sends message to android mobileAndroid Wear 向安卓手机发送消息
【发布时间】:2017-11-11 21:48:44
【问题描述】:

我是 Android Wear 开发的新手。我正在尝试让智能手表向移动应用程序发送一个字符串,但我不知道它是如何工作的。我按照一些教程尝试过这样做,但仍然没有任何效果。

Android 移动清单

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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


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

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

        <service
            android:name=".ListenerService"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
                <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
                <data android:scheme="wear" android:host="*" />
            </intent-filter>

        </service>
    </application>

</manifest>

Android Wear 清单

<?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>

ListenerService for mobile

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();
    }
}

MainActivity for Wear

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();
    }
}

【问题讨论】:

  • 你遇到了什么错误?
  • 其实什么都没有发生,wear和移动模拟器都启动了,我按下wear侧的按钮但是移动端没有出现toast。

标签: android wear-os


【解决方案1】:

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

  • 任意负载(可选)
  • 唯一标识消息操作的路径

这是Sending String from watch to phone 的另一个参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多