我有一个应用程序上的可穿戴,应该发送一个数据到手持按钮点击。我做了几乎相同的设置从手持到电话,唯一的区别是,我发送了一条消息,这是完美的,现在我想发送一个DataMap的另一种方式。
我非常肯定,我有正确的设置,但仍然onDataChanged()上的wearableListenerService上的电话从来没有打过电话。我是不是忘了什么重要的事情,或者还有什么不对劲?
请看一看,拯救我的一天!
下面是从可穿戴设备发送数据的线程(从可穿戴设备的mainActivity调用,googleClient存在,我已经启动了线程)。调试成功发送数据that的返回。
public class SendDataMapToHandheldDataLayer_Thread extends Thread {
private String path;
private DataMap dataMap;
private GoogleApiClient googleClient;
public SendDataMapToHandheldDataLayer_Thread(String cPath, DataMap cDataMap, GoogleApiClient cGoogleClient){
path = cPath;
dataMap = cDataMap;
googleClient = cGoogleClient;
}
public void run(){
PutDataMapRequest putDMR = PutDataMapRequest.create(path);
putDMR.getDataMap().putAll(dataMap);
PutDataRequest request = putDMR.asPutDataRequest();
DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient, request).await();
if(result.getStatus().isSuccess()){
Log.v("dataMapSender_Wear", "DataMap successfully sent!");
}else{
Log.v("dataMapSender_Wear", "ERROR: Failed to send DataMap to data layer");
}
}
}这是电话里的听众,从来没有人打过电话。为什么?从本质上说,它只是从安卓开发人员教程中复制而来:http://developer.android.com/training/wearables/data-layer/events.html#Listen。我也尝试过自己的版本,但我认为问题在其他地方。
public class ListenerServiceMobile extends WearableListenerService{
private static final String TAG = "DataLayerSample";
private static final String START_ACTIVITY_PATH = "/start-activity";
private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
Toast.makeText(getApplicationContext(), "Data changed!", Toast.LENGTH_LONG).show();
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "onDataChanged: " + dataEvents);
}
final List<DataEvent> events = FreezableUtils
.freezeIterable(dataEvents);
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
ConnectionResult connectionResult =
googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
if (!connectionResult.isSuccess()) {
Log.e(TAG, "Failed to connect to GoogleApiClient.");
return;
}
// Loop through the events and send a message
// to the node that created the data item.
for (DataEvent event : events) {
Uri uri = event.getDataItem().getUri();
// Get the node id from the host value of the URI
String nodeId = uri.getHost();
// Set the data of the message to be the bytes of the URI
byte[] payload = uri.toString().getBytes();
// Send the RPC
Wearable.MessageApi.sendMessage(googleApiClient, nodeId,
DATA_ITEM_RECEIVED_PATH, payload);
}
}移动的清单文件包含以下内容:
<service android:name=".ListenerServiceMobile">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED"></action>
<data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />
</intent-filter>
</service>(谢谢你的答覆!)
发布于 2016-04-13 23:41:01
我首先要删除android:pathPrefix,因为它正在过滤您的服务正在侦听的数据。您现在应该看到onDataChange()正在被调用。
在看到数据之后,可以返回并将pathPrefix设置为在可穿戴设备中设置路径的任何内容。(我猜/数据-项目收到)。pathPrefix匹配主机之后指定的任何内容的开头。
这是你想要的,如果是这样的话:
<service android:name=".ListenerServiceMobile">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<data android:scheme="wear" android:host="*"
android:pathPrefix="/data-item-received"" />
</intent-filter>
</service>https://stackoverflow.com/questions/36432795
复制相似问题