从 Wear 向 Phone [MessageAPI] 发送消息时未收到消息

Message not received when sending a message from Wear to Phone [MessageAPI]

我正在尝试从我的 wear [Emulator] 向我的 android phone 发送一条简单消息,根据我在 wear 上的日志,该消息应该已发送,但它不会触发我的 "showToast" 方法在我的 phone 上 [它应该在收到消息时触发]。有人知道我可能做错了什么吗?

这是我的穿着清单

<manifest package="georgikoemdzhiev.weartesttwo"
      xmlns:android="http://schemas.android.com/apk/res/android">

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

<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">
    <uses-library
        android:name="com.google.android.wearable"
        android:required="false"/>

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

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

这是我的移动清单

<manifest package="georgikoemdzhiev.weartesttwo"
      xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

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

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

这是我的 Wear 逻辑[我有一个发送 showToast 消息的按钮]

public class MainActivity extends WearableActivity {
private static final long CONNECTION_TIME_OUT_MS = 2500;
private static final String TAG = MainActivity.class.getSimpleName();
private CircularButton mSendButton;
private List<Node> myNodes = new ArrayList<>();


private static final SimpleDateFormat AMBIENT_DATE_FORMAT =
        new SimpleDateFormat("HH:mm", Locale.UK);

private BoxInsetLayout mContainerView;
private TextView mTextView;
private TextView mClockView;
private GoogleApiClient mClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setAmbientEnabled();
    mSendButton = (CircularButton)findViewById(R.id.sendToast);
    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendToastMessage();
        }
    });
    mClient = new GoogleApiClient.Builder(this)
            .addApiIfAvailable(Wearable.API)
            .build();

    getNodes();

    mContainerView = (BoxInsetLayout) findViewById(R.id.container);
    mTextView = (TextView) findViewById(R.id.text);
    mClockView = (TextView) findViewById(R.id.clock);
}

private void sendToastMessage() {
    Log.d(TAG,"Sending message... Nodes List size:" + myNodes.size());
    // send toast message logic...
    new Thread(new Runnable() {
        @Override
        public void run() {
            for(Node n:myNodes) {
                Log.d(TAG,"Sending message to node:"+n.getDisplayName());
                Wearable.MessageApi.sendMessage(mClient,n.getId(),"/showToast",null);
            }
        }
    });
}

private List<Node> getNodes(){
    new Thread(new Runnable() {
        //
        @Override
        public void run() {
            Log.d(TAG,"Getting nodes...");

            mClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);

            NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(mClient).await();
            List<Node> nodes = result.getNodes();

            for(Node n:nodes){
                Log.d(TAG,"Adding Node: "+n.getDisplayName());
                myNodes.add(n);

            }

            Log.d(TAG,"Getting nodes DONE!");
        }
    }).start();


    return null;
}
}

这是我在移动端的 ReceiveMessageService

public class ReceiveMessageService extends WearableListenerService {


@Override
public void onMessageReceived(MessageEvent messageEvent) {
    Log.d("ReceiveMessageService","onMessageReceived");
    //if(messageEvent.getPath().equals("/showToast")) {
        showToast(messageEvent.getPath());
    //}
}


private void showToast(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}

这是我在 Mobile 中的 MainActivity

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private static final String TAG = MainActivity.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

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

}



@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.d(TAG,"onConnected");
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG,"onConnectionSuspended");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG,"onConnectionFailed");
}

您似乎在移动端 AndroidManifest 中使用了错误的 pathPrefix。尝试替换

<data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />

<data android:scheme="wear" android:host="*" android:pathPrefix="/showToast" />

编辑

另请记住,MessageApi 不能保证传递消息,即使 returns 成功的结果代码如 Google 的 document 所述:

Note: A successful result code does not guarantee delivery of the message. If your app requires data reliability, use DataItem objects or the ChannelApi class to send data between devices.