|
@@ -0,0 +1,70 @@
|
|
1
|
+package com.xhly.manageapp.broadcastreceiver;
|
|
2
|
+
|
|
3
|
+import android.annotation.SuppressLint;
|
|
4
|
+import android.bluetooth.BluetoothAdapter;
|
|
5
|
+import android.content.BroadcastReceiver;
|
|
6
|
+import android.content.Context;
|
|
7
|
+import android.content.Intent;
|
|
8
|
+import android.content.IntentFilter;
|
|
9
|
+import android.util.Log;
|
|
10
|
+import android.widget.Toast;
|
|
11
|
+
|
|
12
|
+public class BLEStateReceiver extends BroadcastReceiver {
|
|
13
|
+
|
|
14
|
+ private static final String BLE_STATE_OFF = "android.bluetooth.BluetoothAdapter.STATE_OFF";
|
|
15
|
+ private static final String BLE_STATE_ON = "android.bluetooth.BluetoothAdapter.STATE_ON";
|
|
16
|
+ private static BLEStateReceiver receiver = new BLEStateReceiver();
|
|
17
|
+
|
|
18
|
+ /**
|
|
19
|
+ * 注册
|
|
20
|
+ *
|
|
21
|
+ * @param context
|
|
22
|
+ */
|
|
23
|
+
|
|
24
|
+ public static void register(Context context) {
|
|
25
|
+ /* IntentFilter filter = new IntentFilter();
|
|
26
|
+ filter.setPriority(Integer.MAX_VALUE);
|
|
27
|
+ // 监视蓝牙关闭和打开的状态
|
|
28
|
+ filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
|
|
29
|
+ filter.addAction(BLE_STATE_OFF);
|
|
30
|
+ filter.addAction(BLE_STATE_ON);
|
|
31
|
+ filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
|
32
|
+ context.registerReceiver(receiver, filter);*/
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ /**
|
|
36
|
+ * 注销
|
|
37
|
+ *
|
|
38
|
+ * @param context
|
|
39
|
+ */
|
|
40
|
+ public static void unregister(Context context) {
|
|
41
|
+ // context.unregisterReceiver(receiver);
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ @Override
|
|
45
|
+ public void onReceive(final Context context, Intent intent) {
|
|
46
|
+ // 检测蓝牙状态的逻辑
|
|
47
|
+ checkBLEState(context, intent);
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ /**
|
|
51
|
+ * 检测蓝牙状态
|
|
52
|
+ *
|
|
53
|
+ * @param context
|
|
54
|
+ * @param intent
|
|
55
|
+ */
|
|
56
|
+ private synchronized void checkBLEState(Context context, Intent intent) {
|
|
57
|
+ int BLEState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
|
|
58
|
+ Log.i("执行监听", "--------蓝牙监听" +BLEState);
|
|
59
|
+ switch (BLEState) {
|
|
60
|
+ case BluetoothAdapter.STATE_ON:
|
|
61
|
+ // 蓝牙已经打开
|
|
62
|
+ Toast.makeText(context, "蓝牙打开", Toast.LENGTH_LONG).show();
|
|
63
|
+ break;
|
|
64
|
+ case BluetoothAdapter.STATE_TURNING_OFF:
|
|
65
|
+ // 蓝牙正在关闭
|
|
66
|
+ Toast.makeText(context, "蓝牙关闭" , Toast.LENGTH_LONG).show();
|
|
67
|
+ break;
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+}
|