Sensor isn't Operation again after reconnect.

edited March 6 in Android

Hi
Now I'm Using metawear C Pro and development android app in Java
I'm using BMI160 Step detector for my Traker App.

I Service Connected like this.

@Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        //serviceBinder = (BtleService.LocalBinder) service;
        board = ((BtleService.LocalBinder) service).getMetaWearBoard(btDevice);
        Log.e(TAG, "onServiceConnected: " + "Service Conn");
        board.onUnexpectedDisconnect(status -> attemptReconnect());
        retrieveBoard();
    }

**retrieveBoard() Method **

public void retrieveBoard() {
        final BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        final BluetoothDevice remoteDevice = btManager.getAdapter().getRemoteDevice(MW_ADDRESS);

        // Create a MetaWear board object for the Bluetooth Device
        //board = serviceBinder.getMetaWearBoard(remoteDevice);
        //board = serviceBinder.getMetaWearBoard(remoteDevice);

        board.connectAsync().onSuccessTask(new Continuation<Void, Task<Route>>() {
            @Override
            public Task<Route> then(Task<Void> task) throws Exception {

                Log.e("MainActivity", "Connected");

                board.readDeviceInformationAsync()
                        .continueWith((Continuation<DeviceInformation, Void>) task1 -> {
                            Log.e("MainActivity", "Device Information: " + task1.getResult());

                            manufacturerValue.setText(task1.getResult().manufacturer);
                            modelNumberValue.setText(task1.getResult().modelNumber);
                            serialNumberValue.setText(task1.getResult().serialNumber);
                            firmwareRevisionValue.setText(task1.getResult().firmwareRevision);
                            hardwareRevisionValue.setText(task1.getResult().hardwareRevision);
                            deviceMacAddressValue.setText(board.getMacAddress());

                            return null;
                        }, Task.UI_THREAD_EXECUTOR);

                accBmi160 = board.getModule(AccelerometerBmi160.class);
                accBmi160.stepDetector().configure()
                        .mode(AccelerometerBmi160.StepDetectorMode.NORMAL)
                        .commit();
                return accBmi160.stepDetector().addRouteAsync(new RouteBuilder() {
                    @Override
                    public void configure(RouteComponent source) {
                        source.stream(new Subscriber() {
                            @Override
                            public void apply(Data data, Object... env) {
                                step += 1;

                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        stepText.setText(String.valueOf(step));
                                    }
                                });

                                Log.e("MainActivity", "Took a step");
                            }
                        });
                    }
                });
            }
        }).continueWith(new Continuation<Route, Void>() {
            @Override
            public Void then(Task<Route> task) throws Exception {

                if (task.isFaulted()) {
                    Log.e("MainActivity", "Task Get failed " + task.getError());
                } else {
                    Log.e("MainActivity", "Task Get Successsss " + task.getResult());
                    accBmi160.stepDetector().start();
                    accBmi160.start();
                }
                return null;
            }
        });
}

initial Connect is very good. (change Step Text)**

Then, I tried Metawear Batt to fook off

Then. I insert to Batt in Metawear Again. BMI160 Step detector not working ,couldn't changed step text & not working metawwear

Comments

  • Reconnect code

    <br /> private Continuation<Void, Void> reconnectResult= task -> {
            ((DialogFragment) getSupportFragmentManager().findFragmentByTag(RECONNECT_DIALOG_TAG)).dismiss();
            Log.e(TAG, "Continuation:  in");
            if (task.isCancelled()) {
                Log.e(TAG, "Continuation:  task.isCancelled()");
                finish();
            } else {
    
                Log.e(TAG, "Continuation:  else");
                setConnInterval(board.getModule(Settings.class));
                setup();
                ((ModuleFragmentBase) currentFragment).reconnected();
    
            }
    
            return null;
        };
    
        private void attemptReconnect() {
            Log.e(TAG, "attemptReconnect:  -  호출 ");
    
    //        accBmi160.stop();
    //        accBmi160.stepDetector().stop();
    //        accBmi160 = null;
            handler.post(new Runnable() {
                @Override
                public void run() {
                    manufacturerValue.setText(" ");
                    modelNumberValue.setText(" ");
                    serialNumberValue.setText(" ");
                    firmwareRevisionValue.setText(" ");
                    hardwareRevisionValue.setText(" ");
                    deviceMacAddressValue.setText(" ");
                    board_battery_level_value.setText(" ");
                    board_rssi_value.setText(" ");
                }
            });
    
            attemptReconnect(0);
        }
    
        private void attemptReconnect(long delay) {
            ReconnectDialogFragment dialogFragment = ReconnectDialogFragment.newInstance(btDevice);
            dialogFragment.show(getSupportFragmentManager(), RECONNECT_DIALOG_TAG);
    
            if (delay != 0) {
                taskScheduler.postDelayed(() -> MWScanActivity.reconnect(board).continueWith(reconnectResult), delay);
            } else {
                MWScanActivity.reconnect(board).continueWith(reconnectResult);
                Log.e(TAG, "attemptReconnect:  -  재연결 다이얼로그 ");
            }
        }
    
    

    setup Metod

    <br /> protected void setup() {
    
            board.connectAsync().onSuccessTask(new Continuation<Void, Task<Route>>() {
                @Override
                public Task<Route> then(Task<Void> task) throws Exception {
    
                    Log.e("MainActivity", "Connected");
    
                    accBmi160 = board.getModule(AccelerometerBmi160.class);
                    accBmi160.stepDetector().configure()
                            .mode(AccelerometerBmi160.StepDetectorMode.NORMAL)
                            .commit();
    
                    board.readDeviceInformationAsync()
                            .continueWith((Continuation<DeviceInformation, Void>) task1 -> {
                                Log.e("MainActivity", "Device Information: " + task1.getResult());
    
                                manufacturerValue.setText(task1.getResult().manufacturer);
                                modelNumberValue.setText(task1.getResult().modelNumber);
                                serialNumberValue.setText(task1.getResult().serialNumber);
                                firmwareRevisionValue.setText(task1.getResult().firmwareRevision);
                                hardwareRevisionValue.setText(task1.getResult().hardwareRevision);
                                deviceMacAddressValue.setText(board.getMacAddress());
    
                                return null;
                            }, Task.UI_THREAD_EXECUTOR);
    
    
                    return accBmi160.stepDetector().addRouteAsync(new RouteBuilder() {
                        @Override
                        public void configure(RouteComponent source) {
                            source.stream(new Subscriber() {
                                @Override
                                public void apply(Data data, Object... env) {
                                    step += 1;
    
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            stepText.setText(String.valueOf(step));
                                        }
                                    });
    
                                    Log.e("MainActivity", "Took a step");
                                }
                            });
                        }
                    });
                }
            }).continueWith(new Continuation<Route, Void>() {
                @Override
                public Void then(Task<Route> task) throws Exception {
    
                    if (task.isFaulted()) {
                        Log.e("MainActivity", "Task Get failed " + task.getError());
                    } else {
                        Log.e("MainActivity", "Task Get Successsss " + task.getResult());
                        accBmi160.stepDetector().start();
                        accBmi160.start();
                    }
                    return null;
                }
            });
        }
    

    initial log like this (blow) Step Detector was work

    After Reconnect, the Step Detector is not work Log Like this,

  • I'd like to operate step detector again after reconnect. How to do that? and fix?

  • Since you are basing your code on the sample app, follow how that code base handles a reconnect:
    https://github.com/mbientlab/MetaWear-SampleApp-Android/blob/master/app/src/main/java/com/mbientlab/metawear/app/HomeFragment.java#L147

Sign In or Register to comment.