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

时间:2016-05-26 18:43:40

标签: android wear-os

我正在尝试从我的[模拟器]发送一条简单的消息到我的Android手机,该消息应该根据我的日志发送,但它不会触发我的手机上的“showToast”方法[它应该在收到消息时触发]。任何人都知道我可能做错了什么?

这是我的Wear Manifest

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

这是我的磨损逻辑[我有一个发送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;
}
}

这是我在Mobile中的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();
}
}

这是我在移动设备中的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");
}

1 个答案:

答案 0 :(得分:3)

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

loop(ul);
loop(ul1);
loop(ul3);
// ... etc

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

修改

另请注意,MessageApi无法保证即使按照Google document所述的方式返回成功的结果代码也会发送消息:

  

注意:成功的结果代码不保证传递消息。如果您的应用需要数据可靠性,请使用DataItem对象或ChannelApi类在设备之间发送数据。