How to periodically scan for bluetooth devices on android

Ameya Phadke picture Ameya Phadke · Jul 20, 2010 · Viewed 42k times · Source

Hi this may sound as a stupid question.But I was unable to find any answers for this, thus posting here.

I am building an indoor application which continuously scans the bluetooth dongles located at different locations in a place like a mall or library.As I move in the mall with android phone in my hand I should be able to get the nearest dongle which I can connect to(Stupid Idea but I want to do something else with this).For this I should be able to continuously scan for the bluetooth devices.

Please can someone tell me how do I make android scan the available Bluetooth devices periodically.

Answer

Ameya Phadke picture Ameya Phadke · Jul 20, 2010

I guess this was so simple but didnt strike me before. Here is the answer,

private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();

private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();

            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action))
            {
            //do something
            }

            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
            {
                Log.v(TAG,"Entered the Finished ");
                mBtAdapter.startDiscovery();
            }

Thus we should start discovery again on ACTION_DISCOVERY_FINISHED which will continuously scan for devices every 12 seconds.