我正在尝试从手表到手机和手机的文字传输。我能够将这两个设备的数据相互发送,我的手表也可以接收数据。
我的Receive_Data_Phone类似乎有些问题。它没有检测到我通过手表发送的任何数据
我已经共享了代码和Logcat输出
Recieve_Data.java
public class Recieve_Data extends WearableListenerService {
private static final String TAG = "@@##";
private GoogleApiClient mGoogleApiClient;
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(
Wearable.API).build();
mGoogleApiClient.connect();
System.out.println("Inside RecieveData onCreate()");
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
final List<DataEvent> events = FreezableUtils
.freezeIterable(dataEvents);
dataEvents.close();
System.out.println("Inside OnDataChanged()");
if (!mGoogleApiClient.isConnected()) {
ConnectionResult connectionResult = mGoogleApiClient
.blockingConnect(30, TimeUnit.SECONDS);
if (!connectionResult.isSuccess()) {
Log.e(TAG,"WEAR :: Service failed to connect to GoogleApiClient.");
return;
}
} else {
Log.e(TAG,"WEAR :: Service connected to GoogleApiClient.");
}
for (DataEvent event : events) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
String path = event.getDataItem().getUri().getPath();
Log.d(TAG, "DataEvent.TYPE_CHANGED, path = " + path);
/* if (Constants.PATH_SERVER_RESPONSE.equals(path)) {
// Get the data out of the event
DataMapItem dataMapItem = DataMapItem.fromDataItem(event
.getDataItem());
final String responseString = dataMapItem.getDataMap()
.getString(Constants.KEY_TITLE);
Log.d(TAG, "DataEvent notif responseString: "
+ responseString);
Intent intent = new Intent("my-event");
intent.putExtra("message", responseString);
LocalBroadcastManager.getInstance(this).sendBroadcast(
intent);
}
*/
if (Constant_Vars.PATH_OBJECTIVE.equals(path)) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(event
.getDataItem());
String msg = dataMapItem.getDataMap().getString(
Constant_Vars.KEY_OBJECTIVE);
int message = Integer.parseInt(msg);
Log.d(TAG, "WEAR:: String " + message);
} else {
Log.d(TAG, "Unrecognized path: " + path);
}
}
}
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d(TAG, "MOBILE:: onMessageReceived messageEvent path: "
+ messageEvent.getPath());
if (messageEvent.getPath().equals(Constant_Vars.PATH_OBJECTIVE)) {
Toast.makeText(getBaseContext(), "Service Working",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Unknown request",
Toast.LENGTH_SHORT).show();
}
}
}
Receive_Data_Phone.java
public class Recieve_Data_Phone extends WearableListenerService {
private static final String TAG = "@@##";
private GoogleApiClient mGoogleApiClient;
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(
Wearable.API).build();
mGoogleApiClient.connect();
System.out.println("Inside RecieveData onCreate()");
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
final List<DataEvent> events = FreezableUtils
.freezeIterable(dataEvents);
dataEvents.close();
System.out.println("Inside OnDataChanged()");
if (!mGoogleApiClient.isConnected()) {
ConnectionResult connectionResult = mGoogleApiClient
.blockingConnect(30, TimeUnit.SECONDS);
if (!connectionResult.isSuccess()) {
Log.e(TAG,"PHONE :: Service failed to connect to GoogleApiClient.");
return;
}
} else {
Log.e(TAG,"PHONE :: Service connected to GoogleApiClient.");
}
for (DataEvent event : events) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
String path = event.getDataItem().getUri().getPath();
Log.d(TAG, "DataEvent.TYPE_CHANGED, path = " + path);
/* if (Constants.PATH_SERVER_RESPONSE.equals(path)) {
// Get the data out of the event
DataMapItem dataMapItem = DataMapItem.fromDataItem(event
.getDataItem());
final String responseString = dataMapItem.getDataMap()
.getString(Constants.KEY_TITLE);
Log.d(TAG, "DataEvent notif responseString: "
+ responseString);
Intent intent = new Intent("my-event");
intent.putExtra("message", responseString);
LocalBroadcastManager.getInstance(this).sendBroadcast(
intent);
}
*/
if (Constant_Vars.PATH_FLAG.equals(path)) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(event
.getDataItem());
String msg = dataMapItem.getDataMap().getString(
Constant_Vars.KEY_FLAG);
//int message = Integer.parseInt(msg);
Log.d(TAG, "PHONE:: String " + msg);
} else {
Log.d(TAG, "Unrecognized path: " + path);
}
}
}
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d(TAG, "MOBILE:: onMessageReceived messageEvent path: "
+ messageEvent.getPath());
if (messageEvent.getPath().equals(Constant_Vars.PATH_FLAG)) {
Toast.makeText(getBaseContext(), "Service Working",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Unknown request",
Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:2)
我认为,您可以发送相同的数据项,尝试添加键值,例如 dataMap.putLong(&#34; timestamp&#34;,System.currentMillion()) 它可能会起作用
原因是:如果您发送相同的数据项,谷歌服务将无法检测到数据项是否已更改,因此它不会通知onDataChanged()方法