在Android上,我可以注册一个回调,告诉我如果蓝牙开启或关闭? [英] On Android, can I register for a callback that tells me if Bluetooth is turned on or off?
问题描述
我需要知道我的应用程序内的蓝牙是否开启或关闭。或者如果蓝牙被接通或关断例如从操作系统设置下拉菜单。我以为我可以在活动的做到这一点onResume()
。但事实证明,Android操作系统的设置下拉菜单(即拉下从屏幕顶部边缘的手指访问的一种)打开时该活动不会暂停。
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.
我需要在蓝牙变为可用或不可更新我的UI。
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?
推荐答案
您可以接收与意图过滤器进行注册:
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;
}
}
}
};
}
这篇关于在Android上,我可以注册一个回调,告诉我如果蓝牙开启或关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!