how to wait till bluetooth turn on in android

落花浮王杯 提交于 2019-12-05 20:47:58

You can register a BroadcastReceiver to listen for state changes on the BluetoothAdapter.

First create a BroadcastReceiver that will listen for state changes

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 bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                                BluetoothAdapter.ERROR);
            switch (bluetoothState) {
            case BluetoothAdapter.STATE_ON:
                //Bluethooth is on, now you can perform your tasks
                break;
            }
        }
    }
};

Then register the BroadcastReceiver with your Activity when it is created so that it can start receiving events.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Set a filter to only receive bluetooth state changed events.
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, filter);
}

Remember to unregister the listener when the Activity is destroyed.

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mReceiver);
}

Use this code Permissions on your menifest file

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

and code

if (!mBluetoothAdapter.isEnabled()) {
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
     startActivityForResult(enableBtIntent , 0);
  } else
  {
     Toast.makeText(getApplicationContext(),"Already on", Toast.LENGTH_LONG).show();
  }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
    Toast.makeText(getApplicationContext(),"Turned on",Toast.LENGTH_LONG).show();
}
if(resultCode == RESULT_CANCELED){

}

You can use startActivityForResult() and check for whether resultCode is RESULT_OK in onActivityResult() with bluetooth permission in your Manifest file like..

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableIntent, 0);
}

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        // bluetooth enabled                      
    }else{
        // show error
    }       
}

ACTION_REQUEST_ENABLE

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //do your things
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!