我正在尝试使用可穿戴消息api在手机和可穿戴设备之间发送消息。手机(物理一加一)和智能手表(物理索尼智能手表3)将相互连接。发送消息也会报告为已发送。但是移动设备和可穿戴应用程序的可穿戴服务侦听器都不会响应这些消息。
我检查过:
应用程序ID
包名称
显示
我还能查什么来找出问题?
谢谢你
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