1

Im trying to send some info from android Wear to the smartphone, but im having two troubles, first of all I am using PutDataRequest but it doesnt work and I dont know what am i doing wrong:

public class MainActivity extends WearableActivity implements SensorEventListener, DataApi.DataListener, MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setAmbientEnabled();

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    apiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)//nos notifica cuando estamos conectados
            .addOnConnectionFailedListener(this)// ofrece el resultado del error
            .build();
    if(!apiClient.isConnected())
        apiClient.connect();

    x = (TextView) findViewById(R.id.x);
    y = (TextView) findViewById(R.id.y);
    z = (TextView) findViewById(R.id.z);



}

I am sending the values to the phone this way, i know this is wrong so how should I send the three values in one message instead of sending three messages?

 public void onSensorChanged(SensorEvent event) {

    accelerometer[0]=event.values[0];
    accelerometer[1]=event.values[1];
    accelerometer[2]=event.values[2];

    x.setText(Float.toString(event.values[0]));
    y.setText(Float.toString(event.values[1]));
    z.setText(Float.toString(event.values[2]));

    //Send X acceleration
    putDataMapReq = PutDataMapRequest.create(ITEM_0);
    putDataMapReq.getDataMap().putFloat(KEY,  accelerometer[0]);
    putDataReq = putDataMapReq.asPutDataRequest();
    resultado = Wearable.DataApi.putDataItem(apiClient, putDataReq);
    Wearable.DataApi.putDataItem(apiClient,putDataReq);

On the other way on the phone i am receiving the messesages this way:

public class MainActivity extends AppCompatActivity implements DataApi.DataListener, MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks,  GoogleApiClient.OnConnectionFailedListener{


//On create

apiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this) 
            .addOnConnectionFailedListener(this)
            .build();

    if(!apiClient.isConnected())
        apiClient.connect();

Receiving messages here:

@Override
public void onDataChanged(DataEventBuffer eventos) {

        for (DataEvent event : eventos) {
            if (event.getType() == DataEvent.TYPE_CHANGED){
                DataItem item =event.getDataItem();
                if (item.getUri().getPath().equals(ITEM_0)) {
                    DataMapItem dataMapItem = DataMapItem.fromDataItem(item);

                    acel_x = dataMapItem.getDataMap().getFloat(KEY);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((TextView) findViewById(R.id.x)).setText(Float.toString(acel_x));

                        }
                    });
                }

So I want to ask you guys how should I send/recieve the messages and how should I send the messages to avoid memory overflow?

  • why can't you store the data in the watch and retrieve it later? If you really want real time data available in phone, then you can use message api to send the data. – NewOne Oct 10 '16 at 10:26
  • Yes, i want real time data available. But how should i use it? Sending a message everytime that values are changed? That will cause a memory overflow, wont it? – approdriguez Oct 10 '16 at 10:47

1 Answers1

0

DataMap has putFloatArray/getFloatArray.

wear:

putDataMapReq.getDataMap().putFloatArray(KEY,  accelerometer);

phone:

acel_xyz = dataMapItem.getDataMap().getFloatArray(KEY);
kazhik
  • 556
  • 1
  • 5
  • 7