Android 6.1 和智能 watch 无法通信 WearableListenerService

标签 android wear-os

我正在尝试使用可穿戴消息 API 在手机和可穿戴设备之间发送消息。手机(实体一加一)和智能 watch (实体索尼智能 watch 3)将相互连接。发送消息也会报告已发送。但移动应用程序和可穿戴应用程序的 WearableServiceListener 都不会响应这些消息。

我已经检查过:

  1. 应用程序ID

  2. 包名称

  3. list

我还可以检查什么来发现问题? 谢谢

public class MainActivityPhone extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Node connectedPhone;
private static String WEAR_PATH = "/from_phone";
private final String TAG = MainActivityPhone.class.getSimpleName();
GoogleApiClient googleApiClient;

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

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Wearable.API)
            .build();
    googleApiClient.connect();

}

public void phoneButtonHandler(View view){
    sendMessage();
}

@Override
public void onConnectionSuspended(int i) {

    Log.e(TAG,"connection suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.e(TAG,"connection failed");
}
@Override
public void onConnected(Bundle bundle) {

    Wearable.NodeApi.getConnectedNodes(googleApiClient)
            .setResultCallback(new ResultCallbacks<NodeApi.GetConnectedNodesResult>() {
                @Override
                public void onSuccess(@NonNull NodeApi.GetConnectedNodesResult nodes) {
                    for (Node node : nodes.getNodes()) {
                        if (node != null && node.isNearby()) {
                            connectedPhone = node;
                            Log.i(TAG, "Connected to " + connectedPhone.getDisplayName());
                        }
                    }
                }

                @Override
                public void onFailure(@NonNull Status status) {
                    if (connectedPhone == null) {
                        Log.i(TAG, "not connected!");
                    }
                }
            });
}

@Override
protected void onStart() {
    super.onStart();
    googleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    googleApiClient.disconnect();
}

private void sendMessage(){

    if(connectedPhone != null && googleApiClient != null) {

        Wearable.MessageApi.sendMessage(googleApiClient, connectedPhone.getId(), WEAR_PATH, "alarm".getBytes()).setResultCallback(new ResultCallbacks<MessageApi.SendMessageResult>() {
            @Override
            public void onSuccess(@NonNull MessageApi.SendMessageResult sendMessageResult) {

                Log.e(TAG, "message sent");
            }

            @Override
            public void onFailure(@NonNull Status status) {

                Log.e(TAG, "connection failed!" + status.getStatusMessage());
            }
        });
    }
    else{
        Toast.makeText(this,"failed to send, no connection",Toast.LENGTH_SHORT).show();
    }
}

.

<service android:name=".ListenerFromWear">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
        </intent-filter>
    </service>

对于两个模块:

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
    applicationId "com.leon_wolf.bigpresenter.connectphoneandwatch"
    minSdkVersion 23
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"

最佳答案

将您的服务定义更改为:

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

您需要指定 wear 方案,详细信息如下:

https://developers.google.com/android/reference/com/google/android/gms/wearable/WearableListenerService

还要确保您的Service扩展了WearableListenerService

关于Android 6.1 和智能 watch 无法通信 WearableListenerService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39707680/

相关文章:

Android:获取 Activity 窗口的尺寸

android - 在 Android SQlite 中查询 BIGINTEGER 数据类型

android - 如何在操作栏中添加后退箭头?

android - 是否有可以从开发的应用程序直接连接的心率追踪器?

android - scrollview(findviewby id)返回null

google-api - 导致 getGoogleAppId 失败并显示状态错误的 Android Wear 应用

android - 如何从另一个 Activity 调用 ListView 适配器进行刷新 - notifyDataSetChanged()

Android 虚拟设备安装 Google API Intel Atom

java - 为什么?状态 : App not accepted into Wear OS on Google Play

Android Wear Preview 未连接到 Wear Emulator