Android监听蓝牙广播

简介: Android监听蓝牙广播
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
public class BluetoothMonitorReceiver extends BroadcastReceiver {
    private static final String TAG = "BluetoothMonitorReceiver";
 
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(TAG,"action = " + action);
        if(action != null){
            switch (action) {
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    switch (blueState) {
                        case BluetoothAdapter.STATE_TURNING_ON:
                            Log.d(TAG,"蓝牙正在打开");
                            break;
                        case BluetoothAdapter.STATE_ON:
                            Log.d(TAG,"蓝牙已经打开");
                            break;
                        case BluetoothAdapter.STATE_TURNING_OFF:
                            Log.d(TAG,"蓝牙正在关闭");
                            break;
                        case BluetoothAdapter.STATE_OFF:
                            Log.d(TAG,"蓝牙已经关闭");
                            break;
                    }
                    break;
 
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    Log.d(TAG,"蓝牙设备已连接");
                    //.getInstance(context).updateBluetoothlist();
                    break;
 
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    Log.d(TAG,"蓝牙设备已断开");
                   // CacheArrayManager.getInstance(HwContext.getContext()).disConnectGatt("val");
                    //CacheArrayManager.getInstance(context).updateBluetoothlist();
                    break;
                case BluetoothDevice.ACTION_ALIAS_CHANGED:
                    Log.d(TAG,"蓝牙设备名称改变");
                    //CacheArrayManager.getInstance(context).updateBluetoothlist();
                    break;
            }
 
        }
    }
}


目录
相关文章
|
4天前
|
JavaScript Java Android开发
uniapp通过蓝牙传输数据 (安卓)
uniapp通过蓝牙传输数据 (安卓)
37 1
|
4天前
|
XML 物联网 API
Android Ble蓝牙App(五)数据操作
Android Ble蓝牙App(五)数据操作
130 0
|
4天前
|
Android开发
【Android App】蓝牙的设备配对、音频传输、点对点通信的讲解及实战(附源码和演示 超详细)
【Android App】蓝牙的设备配对、音频传输、点对点通信的讲解及实战(附源码和演示 超详细)
135 0
|
2天前
|
Android开发
Android获取蓝牙设备列表的方法
Android获取蓝牙设备列表的方法
14 5
|
2天前
|
Android开发
Android监听USB设备插拔
Android监听USB设备插拔
17 7
|
3天前
|
定位技术 Android开发
Android 12蓝牙报java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission
Android 12蓝牙报java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission
7 1
|
4天前
|
Android开发
Android获取横竖屏状态及监听
Android获取横竖屏状态及监听
11 0
|
4天前
|
Android开发
Android 对鼠标事件的监听实现
Android 对鼠标事件的监听实现
14 1
|
4天前
|
物联网 Android开发
Android Ble蓝牙App(七)扫描过滤
Android Ble蓝牙App(七)扫描过滤
|
4天前
|
物联网 Android开发
Android Ble蓝牙App(六)请求MTU与显示设备信息
Android Ble蓝牙App(六)请求MTU与显示设备信息
116 0