On Android, can I register for a callback that tel

2020-07-13 09:38发布

I need to know inside my app whether bluetooth is on or off. Or if bluetooth was turned on or off e.g. from the OS settings pulldown menu. I thought I could do this in the Activity's onResume(). But it turns out that the activity is not paused when the Android OS's settings "pulldown menu" (the one that is accessed by pulling down with a finger from the top edge of the screen) is opened.

I need to update my UI when bluetooth becomes available or unavailable.

Can I register for a callback (e.g. a BroadcastReceiver) or any other callback to let the system tell me when Bluetooth availability changes?

2条回答
2楼-- · 2020-07-13 10:02

You can register the receiver with intent filter:

<receiver
        android:name="com.example.BluetoothReceiver"
        android:enabled="true">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
    </intent-filter>
</receiver>

And this is the BroadcastReceiver:

public class BluetoothReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

            switch (state){
                case BluetoothAdapter.STATE_OFF:
                    //Indicates the local Bluetooth adapter is off.
                break;

                case BluetoothAdapter.STATE_TURNING_ON:
                    //Indicates the local Bluetooth adapter is turning on. However local clients should wait for STATE_ON before attempting to use the adapter.
                break;

                case BluetoothAdapter.STATE_ON:
                    //Indicates the local Bluetooth adapter is on, and ready for use.
                break;

                case BluetoothAdapter.STATE_TURNING_OFF:
                    //Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
                break;
            }
        }
    }
};

Or if you want to add directly in the activity:

public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        //Indicates the local Bluetooth adapter is off.
                        break;

                    case BluetoothAdapter.STATE_TURNING_ON:
                        //Indicates the local Bluetooth adapter is turning on. However local clients should wait for STATE_ON before attempting to use the adapter.
                        break;

                    case BluetoothAdapter.STATE_ON:
                        //Indicates the local Bluetooth adapter is on, and ready for use.
                        break;

                    case BluetoothAdapter.STATE_TURNING_OFF:
                        //Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
                        break;
                }
            }
        }
    };
}
查看更多
我想做一个坏孩纸
3楼-- · 2020-07-13 10:10

This is the intent-filter you need:

<intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
查看更多
登录 后发表回答