(NOOB) Log Save

´´´package com.example.mmripre;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.mbientlab.metawear.Data;
import com.mbientlab.metawear.MetaWearBoard;
import com.mbientlab.metawear.Route;
import com.mbientlab.metawear.Subscriber;
import com.mbientlab.metawear.android.BtleService;
import com.mbientlab.metawear.builder.RouteBuilder;
import com.mbientlab.metawear.builder.RouteComponent;
import com.mbientlab.metawear.data.EulerAngles;
import com.mbientlab.metawear.module.Logging;
import com.mbientlab.metawear.module.SensorFusionBosch;

import bolts.Continuation;
import bolts.Task;

public class MainActivity extends AppCompatActivity implements ServiceConnection {

private BtleService.LocalBinder serviceBinder;
private MetaWearBoard board;
private SensorFusionBosch sensorFusion;
DatabaseReference reff;
private Logging eulerLogging;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(MainActivity.this,"Firebase conectado",Toast.LENGTH_LONG).show();
    reff= FirebaseDatabase.getInstance().getReference().child("Datos");


    // Bind the service when the activity is created
    getApplicationContext().bindService(new Intent(this, BtleService.class),
            this, Context.BIND_AUTO_CREATE);
    findViewById(R.id.Inicio).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            eulerLogging.start(true);
            sensorFusion.eulerAngles().start();
            sensorFusion.start();

        }
    });
    findViewById(R.id.Apagado).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sensorFusion.stop();
            // download log data and send 100 progress updates during the download
            eulerLogging.downloadAsync(100, new Logging.LogDownloadUpdateHandler() {
                @Override
                public void receivedUpdate(long nEntriesLeft, long totalEntries) {
                    Log.i("MainActivity", "Progress Update = " + nEntriesLeft + "/" + totalEntries);
                }
            }).continueWithTask(new Continuation<Void, Task<Void>>() {
                @Override
                public Task<Void> then(Task<Void> task) throws Exception {
                    Log.i("MainActivity", "Download completed");
                    return null;
                }
            });
            sensorFusion.eulerAngles().stop();
        }
    });
}

@Override
public void onDestroy() {
    super.onDestroy();

    // Unbind the service when the activity is destroyed
    getApplicationContext().unbindService(this);
}


@Override
public void onServiceConnected(ComponentName name, IBinder service) {

    // Typecast the binder to the service's LocalBinder class
    serviceBinder = (BtleService.LocalBinder) service;

    Log.i("euler","Servicio iniciado");
    retrieveBoard("EA:51:34:53:9B:1A");

}

@Override
public void onServiceDisconnected(ComponentName name) {

}



private void retrieveBoard(final String MW_MAC_ADDRESS) {
    final BluetoothManager btManager=
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    final BluetoothDevice remoteDevice=
            btManager.getAdapter().getRemoteDevice(MW_MAC_ADDRESS);

    // Create a MetaWear board object for the Bluetooth Device
    board= serviceBinder.getMetaWearBoard(remoteDevice);
    board.connectAsync().onSuccessTask(new Continuation<Void, Task<Route>>() {
        @Override public  Task<Route> then(Task<Void> task) throws Exception {

            Log.i("euler", "Conectado a "+ MW_MAC_ADDRESS);

            sensorFusion = board.getModule(SensorFusionBosch.class);
            eulerLogging = board.getModule(Logging.class);
            sensorFusion.configure()
                    .mode(SensorFusionBosch.Mode.IMU_PLUS)
                    .accRange(SensorFusionBosch.AccRange.AR_16G)
                    .gyroRange(SensorFusionBosch.GyroRange.GR_2000DPS)
                    .commit();


            return sensorFusion.eulerAngles().addRouteAsync(new RouteBuilder() {
                @Override
                public void configure(RouteComponent source) {
                    source.stream(new Subscriber() {
                        @Override
                        public void apply(Data data, Object... env) {
                            Log.i("euler", data.value(EulerAngles.class).toString());


                        }

                    });
                }
            });
        }



    }).continueWith(new Continuation<Route, Void>() {
        @Override
        public Void then(Task<Route> task) throws Exception {
            if (task.isFaulted()) {
                Log.w("euler","fallo la configuración", task.getError());
            } else {
                Log.i("euler", "Configuración exitosa");
            }


            return null;
        }
    });
}

}´´´

Comments

  • Hello again, well I have advanced slowly but surely hahaha, but I have not been able to download the data, so I understood the documents are done as I have in the code, but it is not downloaded .... I do not doubt that it can be a silly error but I really can't continue without this

  • edited October 2019

    And (I dont want to spam but I just thought about this) In what folder will the file be and with what name?

  • edited October 2019

    Maybe I can help a little bit :)

    I think the first thing you need to know is that no files are automatically created. If you want to save data into a file, you will have to write the code for that yourself. All the API is doing, is giving you raw data from the board. What happens to it, is up to you.

    The second thing is that you need to know is that no data is saved to the boards memory unless you actively tell it to. Depending on you usecase you may not need to save anything to the board. You can maybe just save the data you receive in your RouteComponent.stream()-subscriber.

    As far as I can see, you are using two different approaches. On the one hand, you are streaming the data (using RouteComponent.stream()), meaning that you are sending all data directly to the phone.
    On the other hand, you are trying to download the data from the boards internal memory (using downloadAsync()) - but you have not told the board which data it should log (all you are doing is starting the logger by calling Logger.start(). But since you have not told the board which data should be logged, this call basically does nothing).

    So, you have one of two choices depending on you usecase:

    1.) Do you want to download the data directly while using the board (with the phone still connected)?
    Then you can remove downloadAsync() and Logging.start() and just extend the subscriber in your stream-function. This subscriber receives all data. If you want them stored into a file, you can do that here. Right now you are only sending your data to the console with

    Log.i("euler", data.value(EulerAngles.class).toString());
    

    and then forgetting about it.

    2.) If you cant keep the phone connected to the board while producing data, you can, instead of streaming, save all data to the internal memory and download all data at a later point in time.
    To do that, replace RouteComponent.stream() with RouteComponent.log(). Like RouteComponent.stream(), RouteComponent.log() expects a subscriber which will receive all data as soon as you call downloadAsync() - this would be the place where you, for example, save data into a file (unfortunately the documentation is a bit fuzzy about that).
    You will also need to serialize the board-state before you disconnect from the board or the download-subscriber will be lost when you reconnect (meaning that the app will not just ignore and delete all data when you try to download them from the board)

  • edited October 2019

    first thank you very much, what I wanted to do was that stream, but that it would be saved somewhere, that's why I thought log was the solution, I don't know if you know how to do it ....
    Because I get to receive the data, but I can't keep it and I don't have the problem of staying connected
    Then I must make a code that writes a file at the same time as: Log.i ("euler", data.value (EulerAngles.class) .toString ());

Sign In or Register to comment.