Streaming from multiple Metawear devices

Hello dear friends,
Is there any explicit example on streaming from for example 2 Metawear devices at the same time?
I have already checked the "multimw" sample app but it doesn't work for me actually...
And as far as I know, there is nothing available on this topic in the Java API.
So, can anyone please enlighten me with some sort of basic code on this topic?

Comments

  • I have provided my code below... I am trying to have windows of length 250 over acc/gyro data from 2 boards. The problem is that when streaming, I get data only from one of the devices although the other one is connected and the Led module proves that.

  • ` public class MainActivity extends AppCompatActivity implements ServiceConnection {
    private static final int REQUEST_ENABLE_BT = 1;
    private BtleService.LocalBinder serviceBinder;
    // Sensor No.1 parameters. Sensor No.2 parameters are similar.
    private final String MAC_ADDRESS_1 = "XX:XX:XX:XX:XX:XX";
    private MetaWearBoard mwBoard1;
    private Accelerometer accelerometer1;
    private GyroBmi160 gyroscope1;
    private Led led1;
    private Settings settings1;
    /* Signals Storage ========================================================================== */
    private static final int DataLimit = 250;
    public int counter = 0;
    // Sensor No.1 linkedLists used as windows over data streams. Sensor No.2 windows are similarly defined.
    public LinkedList accDataX1 = new LinkedList<>();
    public LinkedList accDataY1 = new LinkedList<>();
    public LinkedList accDataZ1 = new LinkedList<>();
    public LinkedList gyroDataX1 = new LinkedList<>();
    public LinkedList gyroDataY1 = new LinkedList<>();
    public LinkedList gyroDataZ1 = new LinkedList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Bind the service when the activity is created
        getApplicationContext().bindService(new Intent(this, BtleService.class),
                this, Context.BIND_AUTO_CREATE);
        Button startButton = findViewById(R.id.startButtonID);
        Button stopButton = findViewById(R.id.stopButtonID);
        startButton.setOnClickListener(v -> {
            Log.i(TAG0, "Clicked Connect");
            accDataX1.clear();
            // repeated for all linkedLists.
            counter = 0;
            accelerometer1.packedAcceleration().start();
            gyroscope1.packedAngularVelocity().start();
            accelerometer2.packedAcceleration().start();
            gyroscope2.packedAngularVelocity().start();
            accelerometer1.start();
            gyroscope1.start();
            accelerometer2.start();
            gyroscope2.start();
        });
    
        stopButton.setOnClickListener(v -> {
            Log.i(TAG0, "Clicked Disconnect");
            accelerometer1.stop();
            gyroscope1.stop();
            accelerometer2.stop();
            gyroscope2.stop();
            accelerometer1.packedAcceleration().stop();
            gyroscope1.packedAngularVelocity().stop();
            accelerometer2.packedAcceleration().stop();
            gyroscope2.packedAngularVelocity().stop();
        });
    }
    
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        disconnectBoard1();
        disconnectBoard2();
        getApplicationContext().unbindService(this);
    }
    
    
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        serviceBinder = (BtleService.LocalBinder) service;
        retrieveBoards();
        connect2Board1();
        connect2Board2();
    }
    
    
    @Override
    public void onServiceDisconnected(ComponentName name) {
    }
    
    private void retrieveBoards() {
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
        if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
        final BluetoothDevice remoteDevice1 =
                bluetoothManager.getAdapter().getRemoteDevice(MAC_ADDRESS_1);
        final BluetoothDevice remoteDevice2 =
                bluetoothManager.getAdapter().getRemoteDevice(MAC_ADDRESS_2);
        mwBoard1 = serviceBinder.getMetaWearBoard(remoteDevice1);
        mwBoard2 = serviceBinder.getMetaWearBoard(remoteDevice2);
    }
    
    private void connect2Board1() {
        mwBoard1.connectAsync().onSuccessTask((Continuation<Void, Task<Route>>) task -> {
            settings1 = mwBoard1.getModule(Settings.class);
            settings1.editBleConnParams()
                    .maxConnectionInterval(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? 11.25f : 7.5f)
                    .commit();
            settings1.editBleAdConfig()
                    .txPower((byte) 4)
                    .commit();
            accelerometer1 = mwBoard1.getModule(Accelerometer.class);
            gyroscope1 = mwBoard1.getModule(GyroBmi160.class);
            led1 = mwBoard1.getModule(Led.class);
            accelerometer1.configure()
                    .odr(25f)
                    .commit();
            gyroscope1.configure()
                    .odr(GyroBmi160.OutputDataRate.ODR_25_HZ)
                    .range(GyroBmi160.Range.FSR_250)
                    .commit();
            accelerometer1.packedAcceleration().addRouteAsync(source -> source.stream((Subscriber) (data, env) -> {
                Log.i(TAG1, String.valueOf(data.value(Acceleration.class).x()));
                if (accDataX1.size() >= DataLimit) accDataX1.removeFirst();
                accDataX1.addLast(data.value(Acceleration.class).x());
                // Same for Y and Z component windows.
            }));
            gyroscope1.packedAngularVelocity().addRouteAsync(source -> source.stream((Subscriber) (data, env) -> {
                Log.i(TAG1, data.value(AngularVelocity.class).toString());
                // Similar to the windows in acc section above.
            }));
            return null;
        }).continueWith((Continuation<Route, Void>) task -> {
            if(task.isFaulted()){
                connect2Board1();
            }else{
                Log.i(TAG1,"Sensor No.1 is configured and streaming");
                led1.editPattern(Led.Color.GREEN)
                        .riseTime((short) 0)
                        .pulseDuration((short) 1000)
                        .repeatCount((byte) 3)
                        .highTime((short) 500)
                        .highIntensity((byte) 16)
                        .lowIntensity((byte) 0)
                        .commit();
                led1.play();
            }
            return null;
        });
    }
    
    private void connect2Board2()
    {
       // Similar to connect2Board1() but for mwBoard2
    }
    
    private void disconnectBoard1()
    {
        led1.editPattern(Led.Color.RED)
                .riseTime((short) 0)
                .pulseDuration((short) 1000)
                .repeatCount((byte) 3)
                .highTime((short) 500)
                .highIntensity((byte) 16)
                .lowIntensity((byte) 0)
                .commit();
        led1.play();
        mwBoard1.tearDown();
        mwBoard1.disconnectAsync().continueWith((Continuation<Void, Void>) task -> null);
    }
    
    private void disconnectBoard2()
    {
        // Similar to disconnectBoard1() but for mwBoard2
    }
    

    } `

  • This is what the multimw example app is for. Start with that first and tell us why it's not working for you.

  • Thanks for your prompt response, Laura.
    Actually, I could never build the app using the multimw source project and there was something wrong with the gradle file. I tried different gradle builds and different versions of mbient sdks with and also tried changing the min Android sdk required, for the later mbient sdks but no luck.
    I have also noticed that the project has been removed from the following github repositories:

    https://github.com/mbientlab/MetaWear-Tutorial-Android/tree/master/multimw
    https://github.com/mbientlab/Metawear-SampleAndroidApp

    is there a new working version of the multimw project available?

  • Actually you are using the outdated Apps which is the issue. The new App are here:
    https://github.com/mbientlab/MetaWear-SDK-Android/blob/master/metawear.zip
    https://github.com/mbientlab/MetaWear-SDK-Android/blob/master/examples.zip
    Those should build just fine.

  • Thanks a lot Laura.

Sign In or Register to comment.