wjc
2026-03-20 905f88c976c27c8d30e14871e916be62b1c46efe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.hdl.photovoltaic.other;
 
import android.Manifest;
import android.bluetooth.BluetoothDevice;
 
import androidx.annotation.RequiresPermission;
 
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.utils.AppManagerUtils;
import com.hdl.photovoltaic.utils.BleWifiConfiguratorUtils;
import com.hdl.photovoltaic.utils.PermissionUtils;
 
import java.util.List;
 
/**
 * 蓝牙逻辑
 */
public class HdlBluetoothLogic {
    private static volatile HdlBluetoothLogic sHdlBluetoothLogic;
    private BleWifiConfiguratorUtils configurator;
 
    /**
     * 获取当前对象
     *
     * @return HdlAccountLogic
     */
    public static synchronized HdlBluetoothLogic getInstance() {
        if (sHdlBluetoothLogic == null) {
            synchronized (HdlBluetoothLogic.class) {
                if (sHdlBluetoothLogic == null) {
                    sHdlBluetoothLogic = new HdlBluetoothLogic();
                    // 在构造方法中初始化configurator
                    sHdlBluetoothLogic.initConfigurator();
                }
            }
 
        }
        return sHdlBluetoothLogic;
    }
 
    /**
     * 实例方法,初始化configurator
     */
    private void initConfigurator() {
        this.configurator = new BleWifiConfiguratorUtils(HDLApp.getInstance());
    }
 
    /**
     * 扫描设备
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
    public void scanDevices(BleWifiConfiguratorUtils.ScanListener listener) {
        if (this.configurator == null) {
            return;
        }
        this.configurator.startScan(listener);
    }
 
    /**
     * 连接设备
     *
     * @param deviceAddress 目标设备BluetoothDevice
     * @param listener      回调监听
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
    public void connect(String deviceAddress, BleWifiConfiguratorUtils.ConnectListener listener) {
        if (this.configurator == null) {
            return;
        }
        this.configurator.connect(deviceAddress, listener);
    }
 
    /**
     * 发送配置信息
     *
     * @param str      发送数据
     * @param listener 回调监听
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
    public void sendConfig(String str, BleWifiConfiguratorUtils.WriteListener listener) {
        if (this.configurator == null) {
            return;
        }
        this.configurator.writeCredentials(str, listener);
    }
 
    /**
     * 停止扫描
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
    public void stopScan() {
        if (this.configurator == null) {
            return;
        }
        this.configurator.stopScan();
    }
 
    /**
     * 断开设备连接
     * * @param deviceAddress 目标设备BluetoothDevice
     */
    public void disconnect(String deviceAddress) {
        if (this.configurator == null) {
            return;
        }
        this.configurator.disconnect();
    }
 
    /**
     * 检查蓝牙连接状态
     *
     * @param deviceAddress 目标设备BluetoothDevice
     * @return true表示连接成功,false表示连接失败
     */
    public boolean bluetoothStatusCheck(String deviceAddress) {
        if (this.configurator == null) {
            return false;
        }
        return this.configurator.getBluetoothStatus();
    }
 
    /**
     * 检查蓝牙是否开启
     *
     * @return true表示开启,false表示关闭
     */
    public boolean checkBluetoothEnabled() {
        if (this.configurator == null) {
            return false;
        }
        return this.configurator.checkBluetoothEnabled();
    }
 
    /**
     * 释放资源(在 Activity/Fragment 销毁时调用)
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
    public void release() {
        if (this.configurator == null) {
            return;
        }
        this.configurator.release();
    }
 
    /**
     * 获取权限状态( 权限检查结果对比)
     * 检查是否拥有必要的运行时权限(Android 12+ 需动态申请新权限)
     *
     * @return true表示拥有所有必要的权限,false表示缺少权限,也不会弹窗,必须引导用户去设置页面手动开启
     */
    public boolean getMissingPermissions() {
        if (this.configurator == null) {
            return false;
        }
        List<String> missingPermissions = this.configurator.getMissingPermissions();
        if (missingPermissions.isEmpty()) {
            return true;
        }
        for (int i = 0; i < missingPermissions.size(); i++) {
            int permissionState = PermissionUtils.getPermissionStateV2(AppManagerUtils.getAppManager().getLastActivity(), missingPermissions.get(i));
            if (permissionState == 2) {
                return false;
            }
        }
        if (AppManagerUtils.getAppManager().getLastActivity() != null) {
            AppManagerUtils.getAppManager().getLastActivity().requestPermissions(missingPermissions.toArray(new String[0]), 200);
        }
        return true;
    }
}