我花了几天时间尝试在佩戴设备和手机之间共享dataItem,但我没有成功。无论我做什么,可穿戴设备只能监听自己的dataItem。我的意思是,如果我在Wear活动中创建一个请求StringArray的dataResquest,手机会收到它,我可以在手机服务上更改它,但是当它的onDataChanged被调用时,wearable仍将获得原始的StringArray。
我甚至尝试从可穿戴设备向手机发送消息,从手机发出请求,并让可穿戴设备只是听取它,但如果我这样做,它就永远不会收到任何信息。
这就是WearListenerService目前在手机上的样子:
public class WearableService extends WearableListenerService {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
super.onDataChanged(dataEvents);
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
DataItem item = event.getDataItem();
item.freeze();
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
dataMap.putStringArray(Constants.SET_LIST, new String[]{"table 14", "table 27", "table 12323"});
PutDataRequest putDataReq = PutDataRequest.createFromDataItem(item).setUrgent();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
return;
}
}
}
}
这就是我的可穿戴活动的一部分:
public class MainActivity extends Activity implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private String[] set_list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty_database);
// Listen for data item events
// http://developer.android.com/training/wearables/data-layer/data-items.html
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Wearable.DataApi.addListener(mGoogleApiClient, this);
// Send request to phone asking for set list
PutDataMapRequest putDataMapReq = PutDataMapRequest.create(Constants.SET_LIST);
putDataMapReq.getDataMap().putLong("time", new Date().getTime());
putDataMapReq.getDataMap().putStringArray(Constants.SET_LIST, set_list);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest().setUrgent();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
set_list = dataMap.getStringArray("/new_string");
if (set_list != null) {
createList();
return;
}
}
}
}
有谁知道造成这种情况的原因是什么? 谢谢。
答案 0 :(得分:0)
通过使用MessageAPI向手机发送消息询问数据并让手机使用DataAPI将请求的数据发送给可穿戴设备来解决问题。
在从DataAPI添加侦听器之后,我在onConnected中调用sendMessage:
@Override
public void onConnected(Bundle bundle) {
Wearable.DataApi.addListener(mGoogleApiClient, this);
sendMessage(Constants.SET_LIST_PATH, Constants.BLANK_MESSAGE);
}
private void sendMessage(final String path, final String message) {
new Thread(new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for (Node node : nodes.getNodes()) {
Wearable.MessageApi.sendMessage(
mGoogleApiClient, node.getId(), path, message.getBytes()).await();
}
}
}).start();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
DataItem item = event.getDataItem();
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
set_list = dataMap.getStringArray(Constants.SET_LIST_PATH);
if (set_list != null) {
createList();
}
}
}
}
我使用此answer来修复我的可穿戴服务。当手机收到来自手表的消息时,需要创建一个新的GoogleApiClient并使用它通过DataAPI发送数据:
@Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
// Do whatever you need to do with your message here
sendData();
}
private void sendData() {
GoogleApiClient mGoogleApiClient = wearConnect();
final PutDataMapRequest putRequest = PutDataMapRequest.create(Constants.SET_LIST);
final DataMap map = putRequest.getDataMap();
// Add the time to the data map request if you want onDataChanged
// to be called every time you receive a message even if the data
// you are sending to the wearable is not new
map.putLong("time", new Date().getTime());
// Add your stuff here
Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}
private GoogleApiClient wearConnect() {
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
}
@Override
public void onConnectionSuspended(int cause) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
}
})
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
return mGoogleApiClient;
}