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