How to periodically scan for bluetooth devices on android

后端 未结 4 1270
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 16:35

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

4条回答
  •  情深已故
    2020-12-07 17:20

    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.

提交回复
热议问题