wjc
2026-03-12 ddc4ee25f8e4fd5a13f010488b6a11a501cca96a
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
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.BleWifiConfiguratorUtils;
 
/**
 * 蓝牙逻辑
 */
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() {
        if (this.configurator == null) {
            return;
        }
        this.configurator.startScan(new BleWifiConfiguratorUtils.ScanListener() {
            @Override
            public void onDeviceFound(BluetoothDevice device, int rssi, byte[] scanRecord) {
 
            }
 
            @Override
            public void onScanFailed(int errorCode) {
 
            }
        });
    }
 
    /**
     * 连接设备
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
    public void connect() {
        if (this.configurator == null) {
            return;
        }
        this.configurator.connect("", new BleWifiConfiguratorUtils.ConnectListener() {
            @Override
            public void onConnected() {
 
            }
 
            @Override
            public void onDisconnected() {
 
            }
 
            @Override
            public void onConnectionFailed(String reason) {
 
            }
        });
    }
 
    /**
     * 发送配置信息
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
    public void sendConfig() {
        if (this.configurator == null) {
            return;
        }
        this.configurator.writeCredentials("", "", new BleWifiConfiguratorUtils.WriteListener() {
            @Override
            public void onSsidWriteSuccess() {
 
            }
 
            @Override
            public void onPasswordWriteSuccess() {
 
            }
 
            @Override
            public void onWriteComplete(boolean success) {
 
            }
 
            @Override
            public void onWriteFailed(int status) {
 
            }
 
            @Override
            public void onDeviceResponse(String response) {
 
            }
        });
    }
 
    /**
     * 停止扫描
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
    public void stopScan() {
        if (this.configurator == null) {
            return;
        }
        this.configurator.stopScan();
    }
 
    /**
     * 断开设备连接
     */
    public void disconnect() {
        if (this.configurator == null) {
            return;
        }
        this.configurator.disconnect();
    }
 
    /**
     * 释放资源(在 Activity/Fragment 销毁时调用)
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
    public void release() {
        if (this.configurator == null) {
            return;
        }
        this.configurator.release();
    }
}