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

  • 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 !

  • Hi Hrg,

    I am now reminded that we had the issue with Nexus 4 and Nexus 7 that it only notify with LEScancallback() only once per scan. I guess you has one of this phone ?

    You are correct that we (in the MCP) stop and start scanning every one second. I was wrong about this.

    But it's pretty strange what you reported that if you do it automatically using a timer, it won't work, but if you do it manually it worked. Could you please update the code snippet where you do that scanning? Please add that code in the question you post above.

    I will try to get the developer who made the nRF MCP to help you.

  • Mr Hung ,Yea You are right am using nexus 7 ! and now i tried with nexus 5 its working well :) !! but here is the problem -- i can see that app is scanning for new data packets ,but its not displaying or refresing the data in the app display ! i have posted the code above for scanning. So where i have to clear the devicelist and update with new values ? Thankyou

  • Hi hrg,

    I haven't seen your code to re-start the scanning after one second. What I find in the code is you stop scanning after a SCAN_PERIOD. In addition, you would need to make sure you update the list view every time you have new data, and also every time you stop scanning.

Related