gpt4 book ai didi

android - 在 Android 上,我可以注册一个回调来告诉我蓝牙是打开还是关闭吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:40:49 24 4
gpt4 key购买 nike

我需要在我的应用程序内部知道蓝牙是打开还是关闭。或者,如果蓝牙已打开或关闭,例如从操作系统设置下拉菜单。我认为我可以在 Activity 的 onResume() 中执行此操作。但事实证明,当打开 Android 操作系统的设置“下拉菜单”(用手指从屏幕顶部边缘下拉访问的菜单)时, Activity 不会暂停。

当蓝牙可用或不可用时,我需要更新我的用户界面。

我可以注册回调(例如 BroadcastReceiver)或任何其他回调,让系统在蓝牙可用性发生变化时通知我吗?

最佳答案

您可以使用 Intent 过滤器注册接收器:

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

这是广播接收器:

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

或者如果你想直接在 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 - 在 Android 上,我可以注册一个回调来告诉我蓝牙是打开还是关闭吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33260292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com