Darren
Darren

Reputation: 74

Programmatically restarting Bluetooth

How would i restart android Bluetooth programmatically and wait till the Bluetooth is on before executing another method.

I would like to do the following but stop the execution of other code til the bluetooth adaptar has fully restarted: How to enable/disable bluetooth programmatically in android

Upvotes: 1

Views: 3551

Answers (3)

GrindingOgre
GrindingOgre

Reputation: 36

Sadly, method enable() and disable() are not usable anymore since API 33. If you try to call them, they'll return false and nothing happens on the device.

https://developer.android.com/reference/android/bluetooth/BluetoothAdapter?hl=en#disable()

https://developer.android.com/reference/android/bluetooth/BluetoothAdapter?hl=en#enable()

Now every time you need to enable or disable the bluetooth on a device, you must ask to the users if they want by using this action:

https://developer.android.com/reference/android/bluetooth/BluetoothAdapter?hl=en#ACTION_REQUEST_ENABLE

Upvotes: 1

Rutvik Solanki
Rutvik Solanki

Reputation: 11

Using Handler you will easily restart bluetooth.

            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialogConConnect.dismiss();
                }
            });
            tvOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String sec = edtConC.getText().toString();
                    int second = Integer.parseInt(sec);
                    int minus = second - 1;
                    int mSecond = minus*1000;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if(bluetoothAdapter.isEnabled()){
                                bluetoothAdapter.disable();
                            }
                            new Handler().postDelayed(new Runnable() {
                                @Override
                               public void run() {
                                    if(!bluetoothAdapter.isEnabled()){
                                        bluetoothAdapter.enable();
                                    }
                                }
                            },1000);
                        }
                    },mSecond);
                    dialogConConnect.dismiss();

                }
            });
            dialogConConnect.show();
        }
    });

Upvotes: 0

Sujith Niraikulathan
Sujith Niraikulathan

Reputation: 2141

You need to listen to Bluetooth states and work accordingly

public class BluetoothRestarter {

private Context mContext;
private RestartListener mListener;
private BroadcastReceiver mReceiver;

public BluetoothRestarter(Context context) {
    mContext = context;
}

public void restart(RestartListener listener) {
    mListener = listener;
    if (mReceiver == null) {
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                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:
                            BluetoothAdapter.getDefaultAdapter().enable();
                            break;
                        case BluetoothAdapter.STATE_ON:
                            mListener.onRestartComplete();
                            break;
                    }
                }
                context.unregisterReceiver(this);
            }
        };
        mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }
}

public interface RestartListener {
    void onRestartComplete();
    }
}

Now you just have to create instance of this class with a context and call restart method with a RestartListener. You will get the callback.

Upvotes: 2

Related Questions