今天遇到个问题,需要对蓝牙状态进行监听。这个功能之前做过,一直没有总结记录过,今天又遇到了,就记录一下蓝牙状态的监听过程。
首先写一个广播接收器,主要实现蓝牙状态变化的广播接收。
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BluetoothStateBroadcastReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (action){
case BluetoothDevice.ACTION_ACL_CONNECTED:
Toast.makeText(context , "蓝牙设备:" + device.getName() + "已链接", Toast.LENGTH_SHORT).show();
break;