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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:11:27+00:00 2026-05-31T10:11:27+00:00

I have an app with a button on it that I use to turn

  • 0

I have an app with a button on it that I use to turn BT on and off. I have the following code in there;

public void buttonFlip(View view) {
    flipBT();
    buttonText(view);
}

public void buttonText(View view) {  
    Button buttonText = (Button) findViewById(R.id.button1);
    if (mBluetoothAdapter.isEnabled() || (mBluetoothAdapter.a)) {
        buttonText.setText(R.string.bluetooth_on);  
    } else {
        buttonText.setText(R.string.bluetooth_off);
    }
}

private void flipBT() {
    if (mBluetoothAdapter.isEnabled()) {
        mBluetoothAdapter.disable();    
    } else {
        mBluetoothAdapter.enable();
    }
}

I’m calling button Flip, which flips the BT state, and then calls ButtonText, which should update the UI. However, the issue I’m having is, it takes a few seconds for BT to turn on – and during these seconds, the BT status is not enabled, making my button say Bluetooth off, even if it will be on in 2 seconds.

I found the STATE_CONNECTING constant in the BluetoothAdapter android documentation, but… I simply don’t know how to use it, being a newbie and all.

So, I’ve got two questions:

  1. Is there a way to dynamically tie a UI element (such as a button or image) to a BT state, so that when the BT state changes, the button will change as well?
  2. Otherwise, I would want to press the button and get the correct state (I would like for it to say BT on, even if it’s only connecting, since it will be on in 2 seconds). How do I do this?
  • 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-31T10:11:28+00:00Added an answer on May 31, 2026 at 10:11 am

    You will want to register a BroadcastReceiver to listen for any changes in the state of the BluetoothAdapter:

    As a private instance variable in your Activity (or in a separate class file… whichever one you prefer):

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
    
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                                     BluetoothAdapter.ERROR);
                switch (state) {
                case BluetoothAdapter.STATE_OFF:
                    setButtonText("Bluetooth off");
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    setButtonText("Turning Bluetooth off...");
                    break;
                case BluetoothAdapter.STATE_ON:
                    setButtonText("Bluetooth on");
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    setButtonText("Turning Bluetooth on...");
                    break;
                }
            }
        }
    };
    

    Note that this assumes that your Activity implements a method setButtonText(String text) that will change the Button‘s text accordingly.

    And then in your Activity, register and unregister the BroadcastReceiver as follows,

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        /* ... */
    
        // Register for broadcasts on BluetoothAdapter state change
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mReceiver, filter);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
    
        /* ... */
    
        // Unregister broadcast listeners
        unregisterReceiver(mReceiver);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button that launches the google maps app on my device via
I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
Hello StackOverflow'ers, I have a (flex) app that, on the click of a button,
I'm now testing GKPeerPickerController. I have a simple app that just have a button
In my app there is requirement that..I have 6 buttons in a nib, when
I have an app that is making use of photos stored on either the
I have this new app I am creating that will use consumable in-app purchases.
I have a flex app under construction that makes use of a lot of
I have an app with a login screen with a button that invites users
I have done one app in iphone sdk 3.0 in that i use some

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.