gpt4 book ai didi

android - 检测启用/禁用蓝牙

转载 作者:行者123 更新时间:2023-11-30 00:15:56 25 4
gpt4 key购买 nike

我做了一个应用程序来打开/关闭蓝牙。要打开/关闭,我使用了一个开关按钮。这是代码:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private final static int REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter = null;

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(mBluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);

switch (state){
case BluetoothAdapter.STATE_OFF:

break;
case BluetoothAdapter.STATE_ON:
break;
}
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch bluetoothSwitch = (Switch) findViewById(R.id.bluetoothSwitch);

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null){
//Device does not support bluetooth
}
else{
bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(bluetoothSwitch.isChecked() && !mBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, BTIntent);
}
if(!bluetoothSwitch.isChecked() && mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, BTIntent);
}
}
});
}
}

问题是,如果我从设置而不是从我的应用程序打开/关闭蓝牙,开关不会改变。我已经实现了一个广播接收器,但我无法从它访问开关。我试过:

bluetoothSwich.setChecked(true) 

在广播接收器中,但它不起作用。

编辑:我部分解决了全局开关的问题,但要首先从设置中捕捉开/关操作,我必须至少从我的应用程序中单击一次开/关按钮。有什么建议吗?

最佳答案

要检测蓝牙的状态变化,您需要在 AndroidManifest.xml 中添加以下权限。

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

最好使用本地广播。您不需要在 Manifest 中注册它。在运行时在您需要的地方注册它。(如果整个应用程序都需要,则在 list 中注册)

private final BroadcastReceiver bStateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
// Bluetooth is disconnected, do handling here
}
}
}

};

运行时寄存器:

LocalBroadcastManager.getInstance(this).registerReceiver(bStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

运行时取消注册:不要忘记取消注册广播。

LocalBroadcastManager.getInstance(this).unregisterReceiver(bStateReceiver);

静态寄存器:

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

关于android - 检测启用/禁用蓝牙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47280616/

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