2

I am having a progress bar which keeps running until I am connected to BLE.Here is the code -

@Override
    public void Progress() {
        if (activity != null)
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (progressDialog == null) {
                        progressDialog = ProgressDialog.show(activity, getString(R.string.wait), getResources().getString(R.string.connecting), true, false);
                    }
                    progress.setVisibility(View.VISIBLE);
                    container.setVisibility(View.GONE);
                }
            });
    }

When the progress is running I mean when the progress dialog is being shown, if Bluetooth in Settings is disabled, I need to detect it and stop the progress and close the dialog. How do I do this in run().

5 Answers5

1

Create one broad cast receiver for bluetooth . So this will execute when the bluetooth enabled and disabled.

You have to register with below action.

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

In your broad cast receiver you have to right your code like below.

@Override
public void onReceive(Context context, Intent intent) {
    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
         boolean bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
                == BluetoothAdapter.STATE_ON;
        //when bluetooth enabled
        if(bluetoothState) {
            //hide your progress bar here
        }
    }

}

if bluetoothState is true means, bluetooth enabled and false means disabled.

Navas pk
  • 331
  • 3
  • 17
  • In order to work this logic, we need to add the permission. – Navas pk Jan 16 '17 at 10:41
  • What if I don't want to listen for **enabled/disabled**? I just want to know at any point in time the current status of bluetooth? – IgorGanapolsky Mar 15 '17 at 20:43
  • 1
    Then you try below code for checking the current status of bluetooth. `BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();` `boolean staus = bluetoothAdapter.isEnabled()`, if the returned values is true means bluetooth is enabled ..... – Navas pk Mar 17 '17 at 06:04
1

if you want to dismiss dialog if it is already appeared and then if you disable bluetooth and want to hide dialog without doing anything in your already opened activity then you can use broadcast-receiver and for this please refer this link: How to detect Bluetooth state change using a broadcast receiver?

or if you want to check on button click then you can try this code:

public static boolean isBluetoothAvailable() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return (bluetoothAdapter != null && bluetoothAdapter.isEnabled());
}
Community
  • 1
  • 1
android
  • 2,942
  • 1
  • 15
  • 20
0

Try like this

/**
 * Check for Bluetooth.

 * @return True if Bluetooth is available.
 */


public static boolean isBluetoothAvailable() {

    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    return (bluetoothAdapter != null && bluetoothAdapter.isEnabled());
}
perissf
  • 15,979
  • 14
  • 80
  • 117
maulik
  • 150
  • 9
0
/* Register BroadcastReceiver with intent action BluetoothAdapter.ACTION_STATE_CHANGED and move your notifiyng code into onReceive method. Don't forget to check if new state is OFF */

if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {

` if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
        == BluetoothAdapter.STATE_OFF)`}
Jayanth
  • 5,954
  • 3
  • 21
  • 38
Vicky Mahale
  • 1,209
  • 11
  • 18
0

You can check if the bluetooth is disabled with something like:

    boolean isBluetoothDiasabled(){
        BluetoothAdapter myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(!myBluetoothAdapter.isEnabled()){
          return true;
        }
         return false;     
  }
cru3lgenius
  • 106
  • 1
  • 7