我在可穿戴设备上有一个应用程序,它应该通过点击按钮将数据图发送到掌上电脑。我从手持设备到手机的设置几乎相同,唯一不同的是我发送了一条完美无缺的消息,现在我想以另一种方式发送DataMap
。
我很确定我的设置正确,但手机onDataChanged()
上的wearableListenerService
仍未被调用。我忘记了重要的事情或者其他什么是错的吗?
请看看并保存我的一天! :)
这是从可穿戴设备发送数据图的线程(从可穿戴设备的mainActivity调用,googleClient存在,我已经启动了线程)。调试返回数据映射已成功发送。
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");
}
}
}
这是手机上的聆听者,从未被召唤过。为什么?它基本上只是从这里的android开发人员教程复制而来: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>
感谢您的回答! :)
答案 0 :(得分:-1)
我首先删除has_many
,因为它会过滤您的服务正在侦听的数据。你应该看到现在正在调用android:pathPrefix
。
当您看到数据遇到后,您可以返回并将 pathPrefix 设置为您在Wearable中设置路径的任何内容。 (我猜/收到数据项)。 pathPrefix匹配您在主机之后指定的任何内容的开头。
如果是这样的话,这就是你想要的:
onDataChange()