简体   繁体   中英

Is there any other way to discover Bluetooth apart from using BroadcastReceiver?

I have created a BroadcastReceiver for BluetoothDevice.ACTION_FOUND in a Service to scan and Log the Bluetooth Device available. Part of this Service is to keep on checking every 30 seconds if the any of the previously found bluetooth devices is still available or not. Currently it throws me an error for Leaked IntendReceiver, I can fix that error, however I am just not convinced this is the right way to do this. I am creating a new thread to handle bluetooth scanning, creating a while loop which runs every 30 seconds and inside that loop registering the BroadcastReceiver, putting thread on sleep, by the time sleep time is over the onReceive gives me results of the current scan, then I unregister BroadcastReceiver and repeat the loop.

I am unregistering the BroadcastReceiver after every single completion of while loop because the next scan gives me list of currently available devices and then I compare that with previous scan's data.

It is fulfilling my requirement however I have a strong feeling that its not the correct design. Could you please advice me on an alternate approach? Thanks..

Below is the relevant code from the Service-

 class ScanBT extends Thread 
{
     static final long DELAYBT = 30000;

        @Override       
        public void run()
          {

            isBTRunning = true;
            Looper.prepare(); 
            BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();

            try { 
                Log.d(TAG, "BT Scanning started");

                while(isBTRunning)
                { 

                if (!mBluetoothAdapter.isEnabled())
                {
                        mBluetoothAdapter.enable();                     
                        Thread.sleep(15000);                    
                }

                IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                registerReceiver(mReceiver, filter);

                mBluetoothAdapter.startDiscovery();
                Log.d(TAG,"Inside while loop for BT");
                Thread.sleep(DELAYBT);
                unregisterReceiver(mReceiver);
             }
                Looper.loop();
            }
            catch (InterruptedException e) {
                Log.d(TAG, "BT Scanning stopped");
                    Looper.myLooper().quit();
            }

          }     
}

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                        BTDevice =  device.getName(); 
                BTAddress = device.getAddress();
                Log.d(TAG,"BT Found name: " + BTDevice);
                Log.d(TAG,"BT Found address: " + BTAddress);
                        //Code to compare with previous scan results
            }
        }
    };

Figured it out. There is no other way to achieve this. To keep the performance under control, I am now registering the receiver only once and then starting the discovery inside a loop with a sleep interval of 60 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM