• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps About the data transmission between the android phone and wear

carsonsu

Lurker
Apr 13, 2015
2
0
Hi everyone,
I am working on sending the data from the wear(LG watch R) to the android phone. However, I can not find any mistakes on my code and I don't know how to test it.

Could anyone help me to fix it?
Thank you very much.

Here is the sending code of the wear:
@override
public void onSensorChanged(SensorEvent event) {


if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
value[0] = event.values[0];
value[1] = event.values[1];
value[2] = event.values[2];
xViewO.setText("Gyro X: " + value[0]);
yViewO.setText("Gyro Y: " + value[1]);
zViewO.setText("Gyro Z: " + value[2]);
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
value[0] = event.values[0];
value[1] = event.values[1];
value[2] = event.values[2];
xViewA.setText("Accel X: " + value[0]);
yViewA.setText("Accel Y: " + value[1]);
zViewA.setText("Accel Z: " + value[2]);
}
if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
value[0] = event.values[0];
pressure.setText("press: " +value[0]);
}
// TODO Auto-generated method stub
Log.d(TAG, "onsensorChanged" +value[0]+value[1]+value[2]);
sendToPhone(value);
}


private void sendToPhone(float[] value) {

Log.d("sendToPhone","sendToPhone");
PutDataMapRequest putDataMapReq = PutDataMapRequest.create(SENSOR_DATA_PATH);
putDataMapReq.getDataMap().putFloat(SENSOR_DATA_KEY,value[0] );

PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();

Wearable.DataApi
.putDataItem(mGoogleApiClient, putDataReq).setResultCallback(new ResultCallback<DataApi.DataItemResult>(){

@override
public void onResult(DataItemResult result) {
if(result.getStatus().isSuccess()){
Log.e("phone", "success");
}
}

});


if (!mGoogleApiClient.isConnected()) {

Log.d(tag, "unconnected");
return;
}

}

Here is the receiving Code of the phone:

public class MainActivity extends Activity implements DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
final String TAG = "sensors";
private GoogleApiClient mGoogleApiClient;
TextView wear=null;
private static final String SENSOR_DATA_PATH="/sensor-data";
private static final String SENSOR_DATA_KEY="sensor-data";


protected void onCreate(Bundle savedInstanceState) {


setContentView(R.layout.activity_main);

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)

.addOnConnectionFailedListener(this)
.build();
Wearable.DataApi.addListener(mGoogleApiClient, this);
mGoogleApiClient.connect();
super.onCreate(savedInstanceState);

wear=(TextView) findViewById(R.id.wear);

}

protected void onStart() {
mGoogleApiClient.connect();
super.onStart();

Log.d("start", "start.........");

}

@override
public void onConnected(Bundle connectionHint) {


wear.setText("test");
Log.d(TAG, "onConnected: " + connectionHint);
Log.d(TAG, "Connected to Google Api Service");
Wearable.DataApi.addListener(mGoogleApiClient, this);


}

@override
protected void onResume() {
super.onStart();
// mGoogleApiClient.connect();
Log.d("Resume", "Resume..........");

}
public void onDataChanged(DataEventBuffer dataEvents) {
final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
dataEvents.close();
Log.d(TAG, "test");
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_DELETED) {

} else
if (event.getType() == DataEvent.TYPE_CHANGED) {
DataMap dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
if (event.getDataItem().getUri().getPath().equals(SENSOR_DATA_PATH)) {
String data = dataMap.get(SENSOR_DATA_KEY);
wear.setText(data);

}
}
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones