Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7529387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:38:56+00:00 2026-05-30T04:38:56+00:00

I have created a BroadcastReceiver for BluetoothDevice.ACTION_FOUND in a Service to scan and Log

  • 0

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
            }
        }
    };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:38:57+00:00Added an answer on May 30, 2026 at 4:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a webservice in .net 2.0, C#. I need to log some
I have created a BroadcastReceiver to detect SDCard mount and unmount event, however, I
I have created a service running in background which saves gps points on the
I have created a BroadcastReceiver . Now how can I launch my custom Intent
I have a scheduled Service (for scheduling I use a bundle of BroadcastReceiver and
I have created a Alarm using AlarmManager. Intent intent = new Intent(MyApp.this,NotificationMessage.class); PendingIntent sender
I have created a battery widget that uses a Service to listen for Battery
I have created a service which displays a status bar notification after certain time
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a template for Visual Studio 2008 and it currently shows up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.