a noob want to use kitkat to build a project. (helping..!!!)

Hi everyone,
I am doing a simple project with metawear, just want to get the data from accelerometer and do some calculation. So the procedure should be started from:

1: ask user to allow to open the Bluetooth of their smartphone, 
2: scan the available devices surrounding.(I want it scanning automatically after allowed and list all devices in a "small window")
3: feedback to user "connected" after tapping the correct one.
4: start to read the data from accelerometer of metawear.
5: convert to be a real acceleration.
7: do some calculation.

Is it correct?

It is my first time to use android studio and I don't have any experience with Java or other computer language. I will appreciate if you can teach me how to write the android code to access it step by step, I am just a newbie so I may need more your patience.

Thanks you so much and have a good day :)


  • Yes, your steps outlined are correct.  

    I'd first start with a Hello World app.  In other words, start a new project in Android Studio, walk through the wizard to get simple app running on your phone.  Once you feel confident, please read our Android Getting Started guide (http://docs.mbientlab.com/?page_id=40) to get up to speed on the MetaWear library.  The section on the Accelerometer module (http://docs.mbientlab.com/?page_id=40#post-95) explain how to interact with the accelerometer.

    After that, check out some of our code, specifically the Accelerometer panel from the sample app and the SOSTrigger project for full code samples to study.
  • edited July 2015
    Hi Eric,

    Thank you for your reply. So now I need to finish the Bluetooth part(open scan and connectd) for my project. and what I have known so far just is
     1. Layout is for user interface(so coding is for display or show to user)
     2. java.MainActivity is for implementation, do some calculations like how the app run and what the function is.

    and my plan is inside the MainActivity, I want to create an infinite while loop for data collecting and calculating and showing the result in real time to my phone. 

    So now what confusing me is that do I need to create another new activity for BLEservice only? or just mix up inside the MainActivity? because I saw their app has something called  BLEServiceFragment. 
    and if yes, how can I connect these two activities? 
    Forgive my stupid question.

    and there are some coding I have done so far, and can you help me to check? thanks again:)

    //Enable the Bluetooth
    public class MainActivity extends Activity implements ServiceConnection{

    private BluetoothAdapter mBluetoothAdapter;
    private static final int REQUEST_ENABLE_BT= 1;

    protected void onCreate(Bundle savedInstanceState) {

    // Initializes Bluetooth adapter.
    final BluetoothManager bluetoothManager =
    (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    if (!bluetoothManager.getAdapter().isEnabled()) {
    final Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    //bind with metawear
    getApplicationContext().bindService(new Intent(this,MetaWearBleService.class),
    this, Context.BIND_AUTO_CREATE);
  • The MetaWear library has a background service that manages the Bluetooth LE connection, you do not need to create your own service for that task.  Your code looks good so what I would do next is add the MetaWear broadcast receiver to your activity and setup the Bluetooth LE scanner (or hardcode your board's MAC address) to get your app to find your board.
  • edited July 2015
    Hi Eric

    Thank you so much!
    I have completed the code which is for board connecting so far, meaning I can access the board now, using your SOS trigger project as reference :) and now my facing problem is 
    I can not display the data from accelerometer on my phone as 

    public static class PlaceholderFragment extends Fragment implements ServiceConnection can't use the findViewByid function.
    TextView activeCaloriesBurned = (TextView) findViewById(R.id.Condition);
    activeCaloriesBurned.setText("" + X);
    and where and how can I create a loop for calculating? thanks

    public void setBtDevice(BluetoothDevice device) {

    mwController= mwService.getMetaWearController(device);
    mwController.addDeviceCallback(new MetaWearController.DeviceCallbacks() {
    public void connected() {

    accelCtrllr= ((Accelerometer) mwController.getModuleController(Module.ACCELEROMETER));

    Toast.makeText(getActivity(), R.string.toast_connected, Toast.LENGTH_SHORT).show();

    public void disconnected() {
    Toast.makeText(getActivity(), R.string.toast_disconnected, Toast.LENGTH_SHORT).show();
    }).addModuleCallback(new Accelerometer.Callbacks() {
    public void receivedDataValue(short x, short y, short z) {
    Log.i("MainActivity", String.format(Locale.US, "(%.3f, %.3f, %.3f)",
    x / 1000.0, y / 1000.0, z / 1000.0));


  • edited July 2015
    Sorry, may my questions are not so clear.

    what I am trying to say is 

    public void receivedDataValue(short x, short y, short z) {
    Log.i("MainActivity", String.format(Locale.US, "(%.3f, %.3f, %.3f)",
    x / 1000.0, y / 1000.0, z / 1000.0));
    this code is for data acquisition right? meaning the acceleration value is stored in the x, y, z, and I want to use them for calculating, so how can I achieve it? do I need to create a new class for it? 

    thanks again!
  • The call to startComponents must be done in the connected callback function, after you've configured the acclerometer sampling.  You have the startComponents call inside the receivedDataValue callback function.
    Also, 200Hz is too high for streaming, you should lower it to 100Hz or 50Hz.

    There are no loops needed to do your computation.  The code inside receivedDataValue will be executed every time acclerometer sensor data has been received.
  • Hi Eric:

    What your meaning is I can do my calculation after the 
                Log.i("MainActivity", String.format(Locale.US, "(%.3f, %.3f, %.3f)",
    x /
    1000.0, y / 1000.0, z / 1000.0));
    //start my calculation here?
    inside the receivedDataValue function? 
    and what code can I use for display on the smartphone? because when I use 

    TextView activeCaloriesBurned = (TextView) findViewById(R.id.Condition);
    activeCaloriesBurned.setText("" + X);
    there is nothing to show even I assign a value short X=1;

    thanks Eric
  • Yes, you will do your calculation where your comment is.

    Before we get to that though, is the receivedDataValue callback function being called at all?  It doesn't seem like it is, otherwise you should be getting messages in Logcat.  What does your code look like now?  Also, I suggest posting your code in Pastebin (http://pastebin.com/) instead of a Facebook text wall.
  • edited July 2015
    Hi Eric,

    thanks, you help me a lot, and I have posted my code here http://pastebin.com/NEaDbfz3
    could you just give me some direction?

    next thing I need to do should calculating and display the result. but I don't know how to move on as it alway said the non-static can not be referenced from a static context. 

    thank you
  • What line is this error occurring on?
  • Hi Eric,

    Can you access to the link? http://pastebin.com/NEaDbfz3 the is error is at line 187.

  • Yes I can access the link.  However, I don't see any issue with the code at line 187.  What is the full error message?  Also, if you comment out the TextView code, do log messages get printed to Logcat?
  • Hi Eric, 

    the full error message said Non-static method 'findViewById(int)' cannot be referenced from a static context.
  • edited July 2015
    1. Change "activeCaloriesBurned" to be a member variable of the PlaceholderFragment class
    2. Assign it in onViewCreated
    3. Remove the assignment on line 187

    public class PlaceholderFragment {
        private TextView activeCaloriesBurned;

        public void onViewCreated(View view, Bundle savedInstanceState) {
            activeCaloriesBurned= (TextView) view.findViewById(R.id.Condition);
            super.onViewCreated(view, savedInstanceState);
  • Thanks Eric, I can get the data and display it now.

    next what I need to do is when the GPIO pin read the the signal as high, the timer and accelerometer will be start until become low for stopping. and display the how long the time it use and acceleration during the time. 

    can you guild me how to achieve it, thank you!!

  • First, you should try to setup notifications for pin state changes using setPinChangeType and enablePinChangeNotification.  This feature will let you receive notifications for when the input voltage changes.
  • Hi Eric, 

    I have finished this part for notification. and now I have a question that :
    how can I store all the accelerometer value inside a variable array like acc[] using a while loop when it is working? so that I can use it in another place.

    I think should be inside the callback function.

    addModuleCallback(new Accelerometer.Callbacks() {
    public void receivedDataValue(short x, short y, short z) {
    //Here create a while loop for storing till it stop


  • edited August 2015
    double accX[],accY[],accZ[];
    int i=0;
    .addModuleCallback(new Accelerometer.Callbacks() {
    public void receivedDataValue(short x, short y, short z) {

    do {
    accX[i] = x/1000.00;
    accY[i] = y/1000.00;
    accZ[i] = z/1000.00;

    i = i + 1;
    } while(//Condition start-logging and stop-exit);

    That is what I am trying to do it, but seem can't work, how can I fix it? and how to write the condition for logging when the accl start and exit when it stop. thank you.
  • You should use a List to store the data, not an array.  Arrays should only be used when you know what the array size will be.  On the other hand, the List class is for storing an indeterminate amount of data.

    Also, you do not need a while loop in the function.  The only thing the function should do is add data to your list.
  • edited August 2015
    Hi Eric, 

    Can I get more detail, as I never learn about it and don't know where to start, is it called the arraylist or linkedlist?
    List listA = new ArrayList(); //like this?
    and how about this tutorial?: http://tutorials.jenkov.com/java-collections/list.html something like this?
    and do you have other some relative tutorials about the List function which may help me t understand? Thank you so much.
  • Yes, that tutorial covers all the functionality you need for your task and I would use an ArrayList as the List of choice.

    The Java website also provides good information about Lists.
  • edited August 2015
    Hi Eric, 

    My app will be exited automatically.

    List AcceleLength;
    .addModuleCallback(new Accelerometer.Callbacks() {
    public void receivedDataValue(short x, short y, short z) {AcceleLength.add(z);// add data}
    .addModuleCallback(new GPIO.Callbacks() {
    public void pinChangeDetected ( byte pin, byte state) {
    if (state == 0) {
    } else {
    Iterator iterator = AcceleLength.iterator();//get data
    length = (double) iterator.next();
    lengthshow.setText(new DecimalFormat("##.##").format(resultLength));
    there are no any problem indicating, just don't know why it will stop suddenly when I start the accelerometer.  
  • oh, I know why, because I forget the AcceleLength=new arrayList;  thanks, 
    and now I am doing the last one which is multiple connection(connect two mtwear) 

    how can I do it? I know there is an sample app, but which part I should focus on? thanks
This discussion has been closed.