This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

start stop scanning in android app

Hi

How to start and stop scanning continuously in android devices ?.I found that In Nrf master control panel android app there is an option of performing scanning such that it can display the dynamic data that nrf51822 is advertising (settings->scanner->continuous scanning disabled). I couldnt find this function in the source code of nrftoolbox that nordic had provided . Am developing an app for nrf51822 such that ,it advertises dynamic data which should be display live in the app( for ex.displaying rssi value). So when will they provide the source code for master control panel ? Do anybody have the function for this ?

private void scanLeDevice(final boolean enable) {

    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
                System.out.println("hello");
                invalidateOptionsMenu();
            }
        }, SCAN_PERIOD);
               mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
    invalidateOptionsMenu();
}

Thankyou

Parents
  • Hi Hrg,

    Actually you can find the code that we limit the period of scanning in nRFToolbox at function startScan() in ScannerFragment.java file.

    We used a timer and defined a period SCAN_DURATION to stop scanning.

    The reason we do this is to avoid continuous scanning (that can draw lots of power of the phone) when the app is in background or when the customer forgot to stop scanning.

Reply
  • Hi Hrg,

    Actually you can find the code that we limit the period of scanning in nRFToolbox at function startScan() in ScannerFragment.java file.

    We used a timer and defined a period SCAN_DURATION to stop scanning.

    The reason we do this is to avoid continuous scanning (that can draw lots of power of the phone) when the app is in background or when the customer forgot to stop scanning.

Children
  • Thankyou Hung Bui !. My intention is i need to start scan for a second ,then stop & do this continuously ,so that LeScan callback is called everytime and then it updates with new broadcasting value.I have seen such an optin in nrfmaster control panel , but not in nrftoolbox . so it would be great if you provide the scanner.java file of nrfMastercontrol panel .

  • Hi hrg,

    As mentioned, that option is only to remove the timer to stop scanning. If you remove that timer, you will scan forever.

  • Actually my intention is to start scan stop it then again startscan ,stop it -to perform this continuously so that the LEScancallback is called everytimme and it displays the immediate broadcasted data in the app ! When i use the start and stop scan again and again it is neglecting the startscan calls ! so i couldnt refresh the new broadcasted data ,i have to manually stop scanning and then start it again which is a tiresome task . But in NRF master control panel , i have seen that it automatically starts and stops it for one second and again it starts ,stops continuously.I need to perform the same task in my app. So could you please explain the scanningactivity.java of nrfmastercontrol panel ! Thankyou

  • Hi hrg,

    LEScancallback() is called on every single advertising packet received when you are scanning. It's not called once on very startLeScan() you call. What you see in nRFMCP app that the data is updated every one second is that we use a 1 second timer to update the List on the screen, instead of update continuously whenever we receive a advertising packet.

    Could you explain a little bit more on "When i use the start and stop scan again and again it is neglecting the startscan calls ! so i couldnt refresh the new broadcasted data ,i have to manually stop scanning and then start it again which is a tiresome task " What is the difference between "start and stop scan again and again" and "manually stop scanning and then start it again" ?

  • while the ble is broadcasting (eg incremented data like 1,2,3 etc) ,once scan button is pressed it displays once ,again to refresh the display i need to stop it and again press scan button .Yea i used the runnable delay to stop it for one second and called again ,but it dint work . It would be great if you provide the codebase of it !

Related