我正在尝试使用DataApi,但我没有收到可穿戴方面的数据。这是我的代码:
移动方面:
public class Moto360Service implements GoogleApiClient.ConnectionCallbacks, MessageApi.MessageListener {
private static final String TAG = Moto360Service.class.getSimpleName();
private static Moto360Service moto360ServiceInstance = null;
private GoogleApiClient mApiClient;
public Moto360Service() {
Log.v(TAG, "Moto360Service instance Created");
//Favourites
mSelectionOrangeContent = new ArrayList<WatchData>();
mMyRadiosContent = new ArrayList<WatchData>();
mMyPodcastsContent = new ArrayList<WatchData>();
//
initGoogleApiClient();
}
public static Moto360Service getInstance() {
synchronized (TAG) {
if (moto360ServiceInstance == null) {
moto360ServiceInstance = new Moto360Service();
}
}
return moto360ServiceInstance;
}
private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder(OrangeradioApplication.getInstance().getApplicationContext())
.addApi( Wearable.API )
.build();
mApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "connect to watch");
String[] data = {"c","d","e" + + System.currentTimeMillis()};
new DataTask (data).execute();
}
class DataTask extends AsyncTask<Node, Void, Void> {
private final String[] content;
public DataTask (String [] content) {
this.content = content;
}
@Override
protected Void doInBackground(Node... nodes) {
PutDataMapRequest dataMap = PutDataMapRequest.create ("/watch/data");
dataMap.getDataMap().putStringArray("content", content);
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mApiClient, request);
pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult> () {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
// something
Log.d(TAG, "SOMETHING");
Log.d(TAG, "dataItemResult : " + dataItemResult.getDataItem());
if (dataItemResult.getStatus().isSuccess()) {
Log.d(TAG, "message successfully sent");
} else if (dataItemResult.getStatus().isInterrupted()) {
Log.e(TAG, "couldn't send data to watch (interrupted)");
} else if (dataItemResult.getStatus().isCanceled()) {
Log.e(TAG, "couldn't send data to watch (canceled)");
}
}
} );
Log.d(TAG, "sending data");
return null;
}
}
磨损面:
public class MainActivity extends Activity implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks,DataApi.DataListener, WearableListView.ClickListener {
private GoogleApiClient mApiClient;
private SimpleListAdapter mAdapter;
private static final String TAG = MainActivity.class.getSimpleName();
private ArrayList<String> list = new ArrayList<>();
private WearableListView mListView;
private ProgressDialog dialog;
private String showReceived;
private Float showSize;
private Context mContext;
private Handler myHandler = new Handler();
private int progressValue = 0;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
...
initGoogleApiClient();
}
private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.addConnectionCallbacks( this )
.build();
if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )
mApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "connected");
Wearable.DataApi.addListener(mApiClient, this);
Wearable.MessageApi.addListener(mApiClient, this);
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
Log.d(TAG, "data changed");
}
}
在可穿戴设备上未收到该消息,但该消息是从移动设备发送的。
private void sendMessage(final String path, final String text) {
new Thread( new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mApiClient ).await();
for (Node node : nodes.getNodes()) {
Log.d(TAG, "name : " + node.getDisplayName() + "id : " + node.getId());
Log.d(TAG, "mApiClient : " + mApiClient);
Log.d(TAG, "sending message");
Wearable.MessageApi.sendMessage(mApiClient, node.getId(), path, text.getBytes());
}
}
}).start();
}
在日志中,我看到找到了节点:
D/Moto360Service﹕ name : Moto 360 E109id : ca292f00
磨损清单:http://pastebin.com/C5BuXWx8
编辑:
我尝试了以下方法(在每次请求时进行更改):
String[] data = {"a","b","c" + System.currentTimeMillis()};
new DataTask (data).execute();
但它并没有改变任何事情。
答案 0 :(得分:2)
onDataChanged()
,因此如果添加相同的数据(即具有相同内容和路径的数据项),则不会触发该方法。查看是否是这种情况的一种方法是为数据添加时间戳,并查看是否会触发回调。