文章目录
Your application can listen for the
ACTION_STATE_CHANGED
broadcast intent, which the system broadcasts whenever the Bluetooth state changes. This broadcast contains the extra fields
EXTRA_STATE
and
EXTRA_PREVIOUS_STATE
, containing the new and old Bluetooth states, respectively. Possible values for these extra fields are
STATE_TURNING_ON
,
STATE_ON
,
STATE_TURNING_OFF
, and
STATE_OFF
. Listening for this broadcast can be useful if your app needs to detect runtime changes made to the Bluetooth state.
准备
IDE:
Android Studio 4.1.1
Build #AI-201.8743.12.41.6953283, built on November 5, 2020
Runtime version: 1.8.0_242-release-1644-b01 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
接收蓝牙状态改变的广播
新建项目,选择 Empty Activity,在配置项目时,Minimum SDK
选择 API 16: Android 4.1 (Jelly Bean)
。
编辑 src\main\AndroidManifest.xml
应用清单文件,声明使用 android.permission.BLUETOOTH
权限(第 4 行):
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<!-- Allows applications to connect to paired bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH" />
</manifest>
编辑 MainActivity
文件:
package com.mk;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(broadcastReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
String currentState = null;
switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)) {
case BluetoothAdapter.STATE_TURNING_ON:
currentState = "BluetoothAdapter.STATE_TURNING_ON";
break;
case BluetoothAdapter.STATE_ON:
currentState = "BluetoothAdapter.STATE_ON";
break;
case BluetoothAdapter.STATE_TURNING_OFF:
currentState = "BluetoothAdapter.STATE_TURNING_OFF";
break;
case BluetoothAdapter.STATE_OFF:
currentState = "BluetoothAdapter.STATE_OFF";
break;
default:
currentState = "UNKNOWN";
break;
}
Toast.makeText(context, currentState, Toast.LENGTH_SHORT).show();
// String previousState = null;
// switch (intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, -1)) {
// case BluetoothAdapter.STATE_TURNING_ON:
// previousState = "BluetoothAdapter.STATE_TURNING_ON";
// break;
// case BluetoothAdapter.STATE_ON:
// previousState = "BluetoothAdapter.STATE_ON";
// break;
// case BluetoothAdapter.STATE_TURNING_OFF:
// previousState = "BluetoothAdapter.STATE_TURNING_OFF";
// break;
// case BluetoothAdapter.STATE_OFF:
// previousState = "BluetoothAdapter.STATE_OFF";
// break;
// default:
// previousState = "UNKNOWN";
// break;
// }
//
// Toast.makeText(context, previousState, Toast.LENGTH_SHORT).show();
}
}
};
}
参考
Broadcasts - Overview - Receiving broadcasts
Bluetooth - Overview - Set up bluetooth
BluetoothAdapter.ACTION_STATE_CHANGED