我遇到了 Google android wear 数据层 API 的问题...
我已经用它从可穿戴活动中调用手持活动,而且效果很好。但现在我想从表盘服务调用手持活动......
我创建了一个 Google API 客户端并添加了 wearableApi:
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFace.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_VARIABLE)
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.setAcceptsTapEvents(true)
.build());
mGoogleApiClient = new GoogleApiClient.Builder(MyWatchFace.this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
然后我使用这些方法发送数据:
public void startActivity()
{
HashSet<String> results = new HashSet<String>();
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
String handheld = getConnectedNodesResult.getNodes().get(0).getId();
Toast.makeText(MyWatchFace.this, getConnectedNodesResult.getNodes().get(0).getDisplayName()+": "+ handheld, Toast.LENGTH_SHORT).show();
sendStartActivityMessage(handheld, giveValueXYZ());
}
});
private void sendStartActivityMessage(String nodeId, String data) {
byte[] dataArray = data.getBytes();
Toast.makeText(MyWatchFace.this, "ActivityPath: "+ START_ACTIVITY_PATH, Toast.LENGTH_SHORT).show();
Wearable.MessageApi.sendMessage(
mGoogleApiClient, nodeId, START_ACTIVITY_PATH, dataArray).setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG, "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
}
}
});
}
在移动网站上,我使用此代码来收听消息:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_host_main);
context = getApplicationContext();
...
//Google Api
final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API).build();
googleApiClient.connect();
Wearable.MessageApi.addListener(googleApiClient, this);
...
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals(START_ACTIVITY_PATH_WEARDATA)) {
Log.d(TAG, "getPath: " + messageEvent.getPath() + "|||getData: " + messageEvent.getData());
someActions();
}
如果我从可穿戴活动发送消息,但如果我从表盘服务发送消息甚至不会触发 onMessageReceived(...) 方法,那么所有这些都可以正常工作......
我的包名可能有问题吗?
手机:com.example.janik.xxx.xxx
表盘:de.WatchSmart.watch_smart_watch_face_service
客户端如何知道向他发送了哪个 Data Layer Api 消息?