检测启用/禁用蓝牙

时间:2017-11-14 08:10:05

标签: android bluetooth uiswitch

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

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) 

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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>