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 8368555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:25:07+00:00 2026-06-09T13:25:07+00:00

I made a helper class that extends BroadcastReceiver to listen for BluetoothDevice found and

  • 0

I made a helper class that extends BroadcastReceiver to listen for BluetoothDevice found and discovery finish intents. I have two activities which use this class by passing a handler. The handler receives messages as per the intent. I instantiate the class and registerReceiver like this:

From mainActivity:

deviceHelper=new DevicesHelper(myHandler,DevicesHelper.REQUEST_DETECT_DEVICES_IN_RANGE);

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

filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(deviceHelper, filter);

if(myBluetoothAdapter.isDiscovering())myBluetoothAdapter.cancelDiscovery();
myBluetoothAdapter.startDiscovery(); 

From ListActivity:

deviceHelper=new DevicesHelper(deviceListHandler,DevicesHelper.REQUEST_DEVICE_LIST_ACTIVITY);

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

filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(deviceHelper, filter);

DeviceHelper class:

public class DevicesHelper extends BroadcastReceiver {

    public final static int REQUEST_DEVICE_LIST_ACTIVITY=1;
    public final static int REQUEST_DETECT_DEVICES_IN_RANGE=2;


    Handler myHandler;      
    int requestCode;

    public DevicesHelper(){

    }
    public DevicesHelper(Handler handler,int requestCode){
            this.requestCode=requestCode;
            myHandler=handler;

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();

        // When discovery finds a device
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            switch(requestCode){
            case REQUEST_DEVICE_LIST_ACTIVITY: 
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    //newDevicesCount++;
                    String[] deviceInfo={device.getName(),device.getAddress()};

                    myHandler.obtainMessage(DeviceListActivity.MESSAGE_NEW_DEVICE,deviceInfo);
                };


                break;

            case REQUEST_DETECT_DEVICES_IN_RANGE:

                String[] deviceInfo={device.getName(),device.getAddress()};

                myHandler.obtainMessage(StripChartRecorder.MESSAGE_NEW_DEVICE,deviceInfo);
                break;

            }


            // When discovery is finished, change the Activity title
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

            myHandler.obtainMessage(StripChartRecorder.MESSAGE_DISCOVERY_FINISHED);

        }
    }

Handler:

private final Handler myHandler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case MESSAGE_NEW_DEVICE:
                    doOtherStuff();

                case MESSAGE_DISCOVERY_FINISHED:
                    dostuff();
            }
            break;
}}

Am I missing something here? I appreciate any help.

  • 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-06-09T13:25:08+00:00Added an answer on June 9, 2026 at 1:25 pm

    The problem was how I was handling the message received from the handler. Turned out Set<> was initially set to null as per eclipse’s instruction, so it was never adding the devices received from the BroadcastReceiver helper class. I appreciate anyone who tried to help.

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

Sidebar

Related Questions

I have made a small helper that adds class=selected . Primarily it uses current_page?
I have made a helper class called Navigation that gets used on every page
I have a helper class that I need context so I can access the
I made a UITableViewCell and I have 20 rows in that table in which
I am trying to make Http connections made by C# HttpClient helper class reusable.
I'm trying to build a jQuery.live like function. Helper is a class that has
For collection of smaller helper utility classes, I have created a general class MyUtils
I have made extension for UIButton class. Now if I use it everything works
I have found a few threads on various sites that seem to touch on
I have this project that I'm working on for a class, and I'm not

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.