how to wait till bluetooth turn on in android

北慕城南 提交于 2019-12-07 16:25:09

问题


I need to turn on Bluetooth in an android device programmatically and wait till it on to proceed to next line of code.

My code is as below

if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                ctx.startActivity(enableBtIntent);
}

When doing like this, the code continue to execute from nect line without waiting for bluetooth completely on. Is there any way to solve this? Can I add a look to check if bluetooth is on?


回答1:


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);
}



回答2:


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){

}



回答3:


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




回答4:


startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //do your things
}


来源:https://stackoverflow.com/questions/36735067/how-to-wait-till-bluetooth-turn-on-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!