连接蓝牙,配网
This commit is contained in:
517
app/src/main/java/com/ckkj/water/espBle/EspProvisionManager.java
Normal file
517
app/src/main/java/com/ckkj/water/espBle/EspProvisionManager.java
Normal file
@@ -0,0 +1,517 @@
|
||||
package com.ckkj.water.espBle;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.ckkj.water.AppConfig;
|
||||
import com.espressif.provisioning.DeviceConnectionEvent;
|
||||
import com.espressif.provisioning.ESPConstants;
|
||||
import com.espressif.provisioning.ESPDevice;
|
||||
import com.espressif.provisioning.WiFiAccessPoint;
|
||||
import com.espressif.provisioning.listeners.ProvisionListener;
|
||||
import com.espressif.provisioning.listeners.WiFiScanListener;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
|
||||
|
||||
/**
|
||||
* @author fengxiaoyang
|
||||
* @date 2026/6/4
|
||||
* @description 鎿嶄綔钃濈墮绠$悊绫?
|
||||
*/
|
||||
public class EspProvisionManager {
|
||||
private static volatile EspProvisionManager instance;
|
||||
|
||||
private final Context context;
|
||||
private final Handler mainHandler;
|
||||
private final com.espressif.provisioning.ESPProvisionManager provisionManager;
|
||||
|
||||
private ESPDevice espDevice;
|
||||
private ProvisionCallback callback;
|
||||
private boolean connecting;
|
||||
private boolean connected;
|
||||
private boolean provisioning;
|
||||
private boolean eventBusRegistered;
|
||||
private boolean ignoreNextDisconnectEvent;
|
||||
private String currentDeviceName;
|
||||
private String currentDeviceType = ProvisionConfig.TRANSPORT_BLE;
|
||||
|
||||
private final Runnable connectionTimeoutTask = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (connecting && !connected) {
|
||||
connecting = false;
|
||||
notifyFailed(ProvisionConfig.ERROR_CONNECT_TIMEOUT, "蓝牙连接超时");
|
||||
disconnectDeviceOnly();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private final Runnable provisionTimeoutTask = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (provisioning) {
|
||||
finishProvisionFailed(ProvisionConfig.ERROR_PROVISION_TIMEOUT, "配网超时");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private EspProvisionManager(Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
this.mainHandler = new Handler(Looper.getMainLooper());
|
||||
this.provisionManager = com.espressif.provisioning.ESPProvisionManager.getInstance(this.context);
|
||||
ensureEventBusRegistered();
|
||||
}
|
||||
|
||||
public static EspProvisionManager init(Context context) {
|
||||
if (instance == null) {
|
||||
synchronized (EspProvisionManager.class) {
|
||||
if (instance == null) {
|
||||
instance = new EspProvisionManager(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static EspProvisionManager get() {
|
||||
if (instance == null) {
|
||||
throw new RuntimeException("Please call init()");
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public EspProvisionManager setCallback(ProvisionCallback callback) {
|
||||
this.callback = callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ESPDevice getEspDevice() {
|
||||
return espDevice;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
public boolean isProvisioning() {
|
||||
return provisioning;
|
||||
}
|
||||
|
||||
public void connect(String deviceAddress, String deviceName) {
|
||||
connect(deviceAddress, deviceName, AppConfig.BLUETOOTH_DEVICE_UUID);
|
||||
}
|
||||
|
||||
public void connect(String deviceAddress, String deviceName, String serviceUuid) {
|
||||
connect(deviceAddress, deviceName, serviceUuid, "", ProvisionConfig.DEFAULT_SECURITY);
|
||||
}
|
||||
|
||||
public void connect(String deviceAddress, String deviceName, String serviceUuid,
|
||||
String proofOfPossession, int securityType) {
|
||||
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if (bluetoothAdapter == null || TextUtils.isEmpty(deviceAddress)) {
|
||||
notifyFailed(ProvisionConfig.ERROR_INVALID_DEVICE, "蓝牙设备无效");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(deviceAddress);
|
||||
connect(bluetoothDevice, deviceName, serviceUuid, proofOfPossession, securityType);
|
||||
} catch (IllegalArgumentException e) {
|
||||
notifyFailed(ProvisionConfig.ERROR_INVALID_DEVICE, "蓝牙设备地址无效");
|
||||
}
|
||||
}
|
||||
|
||||
public void connect(BluetoothDevice bluetoothDevice, String serviceUuid) {
|
||||
connect(bluetoothDevice, null, serviceUuid, "", ProvisionConfig.DEFAULT_SECURITY);
|
||||
}
|
||||
|
||||
public void connect(BluetoothDevice bluetoothDevice, String deviceName, String serviceUuid,
|
||||
String proofOfPossession, int securityType) {
|
||||
connect(bluetoothDevice, deviceName, serviceUuid, proofOfPossession, "", securityType);
|
||||
}
|
||||
|
||||
public void connect(BluetoothDevice bluetoothDevice, String deviceName, String serviceUuid,
|
||||
String proofOfPossession, String userName, int securityType) {
|
||||
if (bluetoothDevice == null) {
|
||||
notifyFailed(ProvisionConfig.ERROR_INVALID_DEVICE, "蓝牙设备无效");
|
||||
return;
|
||||
}
|
||||
|
||||
String primaryServiceUuid = TextUtils.isEmpty(serviceUuid) ? ProvisionConfig.SERVICE_UUID : serviceUuid;
|
||||
String displayName = !TextUtils.isEmpty(deviceName) ? deviceName : bluetoothDevice.getName();
|
||||
currentDeviceName = displayName;
|
||||
currentDeviceType = ProvisionConfig.TRANSPORT_BLE;
|
||||
|
||||
ignoreNextDisconnectEvent = espDevice != null;
|
||||
disconnectDeviceOnly();
|
||||
ensureEventBusRegistered();
|
||||
|
||||
espDevice = provisionManager.createESPDevice(
|
||||
ESPConstants.TransportType.TRANSPORT_BLE,
|
||||
parseSecurityType(securityType)
|
||||
);
|
||||
espDevice.setBluetoothDevice(bluetoothDevice);
|
||||
espDevice.setPrimaryServiceUuid(primaryServiceUuid);
|
||||
espDevice.setDeviceName(displayName);
|
||||
espDevice.setProofOfPossession(proofOfPossession == null ? "" : proofOfPossession);
|
||||
espDevice.setUserName(userName == null ? "" : userName);
|
||||
|
||||
connecting = true;
|
||||
connected = false;
|
||||
provisioning = false;
|
||||
mainHandler.removeCallbacks(connectionTimeoutTask);
|
||||
mainHandler.postDelayed(connectionTimeoutTask, ProvisionConfig.CONNECTION_TIMEOUT);
|
||||
notifyConnecting();
|
||||
espDevice.connectBLEDevice(bluetoothDevice, primaryServiceUuid);
|
||||
}
|
||||
|
||||
public void scanDeviceWifiList() {
|
||||
if (!checkConnected()) {
|
||||
notifyWifiListFailed("设备未连接");
|
||||
return;
|
||||
}
|
||||
|
||||
espDevice.scanNetworks(new WiFiScanListener() {
|
||||
@Override
|
||||
public void onWifiListReceived(ArrayList<WiFiAccessPoint> wifiList) {
|
||||
Set<String> wifiNames = new LinkedHashSet<>();
|
||||
if (wifiList != null) {
|
||||
for (WiFiAccessPoint accessPoint : wifiList) {
|
||||
if (accessPoint != null && !TextUtils.isEmpty(accessPoint.getWifiName())) {
|
||||
wifiNames.add(accessPoint.getWifiName());
|
||||
}
|
||||
}
|
||||
}
|
||||
notifyWifiListReceived(wifiNames.toArray(new String[0]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWiFiScanFailed(Exception e) {
|
||||
notifyWifiListFailed(getErrorMessage(e, "获取设备 WiFi 列表失败"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void provision(String ssid, String password) {
|
||||
provisionWifi(ssid, password);
|
||||
}
|
||||
|
||||
public void provisionWifi(String ssid, String password) {
|
||||
if (!checkConnected()) {
|
||||
notifyFailed(ProvisionConfig.ERROR_NOT_CONNECTED, "设备未连接");
|
||||
return;
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(ssid)) {
|
||||
notifyFailed(ProvisionConfig.ERROR_WIFI_CONFIG_FAILED, "WiFi 名称不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
provisioning = true;
|
||||
mainHandler.removeCallbacks(provisionTimeoutTask);
|
||||
mainHandler.postDelayed(provisionTimeoutTask, ProvisionConfig.PROVISION_TIMEOUT);
|
||||
notifyProvisioning(0);
|
||||
|
||||
espDevice.provision(ssid, password == null ? "" : password, new ProvisionListener() {
|
||||
@Override
|
||||
public void createSessionFailed(Exception e) {
|
||||
finishProvisionFailed(ProvisionConfig.ERROR_SESSION_FAILED, getErrorMessage(e, "创建配网会话失败"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wifiConfigSent() {
|
||||
notifyProvisioning(30);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wifiConfigFailed(Exception e) {
|
||||
notifyProvisioningRejected();
|
||||
finishProvisionFailed(ProvisionConfig.ERROR_WIFI_CONFIG_FAILED, getErrorMessage(e, "发送 WiFi 信息失败"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wifiConfigApplied() {
|
||||
notifyProvisioning(60);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wifiConfigApplyFailed(Exception e) {
|
||||
finishProvisionFailed(ProvisionConfig.ERROR_WIFI_APPLY_FAILED, getErrorMessage(e, "应用 WiFi 信息失败"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provisioningFailedFromDevice(ESPConstants.ProvisionFailureReason failureReason) {
|
||||
finishProvisionFailed(ProvisionConfig.ERROR_PROVISION_FAILED, getProvisionFailureMessage(failureReason));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deviceProvisioningSuccess() {
|
||||
finishProvisionSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProvisioningFailed(Exception e) {
|
||||
finishProvisionFailed(ProvisionConfig.ERROR_PROVISION_FAILED, getErrorMessage(e, "配网失败"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
boolean shouldNotify = connected || connecting || provisioning;
|
||||
disconnectDeviceOnly();
|
||||
resetState();
|
||||
if (shouldNotify) {
|
||||
notifyDisconnected();
|
||||
}
|
||||
}
|
||||
|
||||
public void release() {
|
||||
disconnect();
|
||||
callback = null;
|
||||
mainHandler.removeCallbacksAndMessages(null);
|
||||
if (eventBusRegistered) {
|
||||
EventBus.getDefault().unregister(this);
|
||||
eventBusRegistered = false;
|
||||
}
|
||||
if (instance == this) {
|
||||
instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onDeviceConnectionEvent(DeviceConnectionEvent event) {
|
||||
if (event == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (event.getEventType()) {
|
||||
case ESPConstants.EVENT_DEVICE_CONNECTED:
|
||||
mainHandler.removeCallbacks(connectionTimeoutTask);
|
||||
connecting = false;
|
||||
connected = true;
|
||||
notifyConnected();
|
||||
break;
|
||||
|
||||
case ESPConstants.EVENT_DEVICE_CONNECTION_FAILED:
|
||||
mainHandler.removeCallbacks(connectionTimeoutTask);
|
||||
mainHandler.removeCallbacks(provisionTimeoutTask);
|
||||
boolean wasConnecting = connecting;
|
||||
connecting = false;
|
||||
connected = false;
|
||||
provisioning = false;
|
||||
if (wasConnecting) {
|
||||
notifyFailed(ProvisionConfig.ERROR_CONNECT_FAILED, "蓝牙连接失败");
|
||||
} else {
|
||||
notifyFailed(ProvisionConfig.ERROR_PROVISION_FAILED, "设备通信失败");
|
||||
}
|
||||
break;
|
||||
|
||||
case ESPConstants.EVENT_DEVICE_DISCONNECTED:
|
||||
if (ignoreNextDisconnectEvent) {
|
||||
ignoreNextDisconnectEvent = false;
|
||||
return;
|
||||
}
|
||||
mainHandler.removeCallbacks(connectionTimeoutTask);
|
||||
mainHandler.removeCallbacks(provisionTimeoutTask);
|
||||
boolean shouldNotify = connected || connecting || provisioning;
|
||||
resetState();
|
||||
if (shouldNotify) {
|
||||
notifyDisconnected();
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkConnected() {
|
||||
return espDevice != null && connected;
|
||||
}
|
||||
|
||||
private void ensureEventBusRegistered() {
|
||||
if (!eventBusRegistered) {
|
||||
EventBus.getDefault().register(this);
|
||||
eventBusRegistered = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void disconnectDeviceOnly() {
|
||||
if (espDevice != null) {
|
||||
try {
|
||||
espDevice.disconnectDevice();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void resetState() {
|
||||
mainHandler.removeCallbacks(connectionTimeoutTask);
|
||||
mainHandler.removeCallbacks(provisionTimeoutTask);
|
||||
connecting = false;
|
||||
connected = false;
|
||||
provisioning = false;
|
||||
ignoreNextDisconnectEvent = false;
|
||||
}
|
||||
|
||||
private ESPConstants.SecurityType parseSecurityType(int securityType) {
|
||||
if (securityType == ProvisionConfig.SECURITY_0) {
|
||||
return ESPConstants.SecurityType.SECURITY_0;
|
||||
} else if (securityType == ProvisionConfig.SECURITY_1) {
|
||||
return ESPConstants.SecurityType.SECURITY_1;
|
||||
} else {
|
||||
return ESPConstants.SecurityType.SECURITY_2;
|
||||
}
|
||||
}
|
||||
|
||||
private void finishProvisionSuccess() {
|
||||
mainHandler.removeCallbacks(provisionTimeoutTask);
|
||||
provisioning = false;
|
||||
notifyProvisioning(100);
|
||||
notifySuccess();
|
||||
}
|
||||
|
||||
private void finishProvisionFailed(int errorCode, String message) {
|
||||
mainHandler.removeCallbacks(provisionTimeoutTask);
|
||||
provisioning = false;
|
||||
notifyFailed(errorCode, message);
|
||||
}
|
||||
|
||||
private String getProvisionFailureMessage(ESPConstants.ProvisionFailureReason failureReason) {
|
||||
if (failureReason == ESPConstants.ProvisionFailureReason.AUTH_FAILED) {
|
||||
return "WiFi 密码错误";
|
||||
} else if (failureReason == ESPConstants.ProvisionFailureReason.NETWORK_NOT_FOUND) {
|
||||
return "未找到指定 WiFi";
|
||||
} else if (failureReason == ESPConstants.ProvisionFailureReason.DEVICE_DISCONNECTED) {
|
||||
return "设备已断开连接";
|
||||
} else {
|
||||
return "设备配网失败";
|
||||
}
|
||||
}
|
||||
|
||||
private String getErrorMessage(Exception e, String defaultMessage) {
|
||||
if (e != null && !TextUtils.isEmpty(e.getMessage())) {
|
||||
return e.getMessage();
|
||||
}
|
||||
return defaultMessage;
|
||||
}
|
||||
|
||||
private void runOnMainThread(Runnable runnable) {
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
runnable.run();
|
||||
} else {
|
||||
mainHandler.post(runnable);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyConnecting() {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onConnecting();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifyConnected() {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onConnected();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifyProvisioning(final int progress) {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onProvisioning(progress);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifySuccess() {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onSuccess(currentDeviceName, currentDeviceType);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifyFailed(final int errorCode, final String message) {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onFailed(errorCode, message);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifyDisconnected() {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onDisconnected();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifyWifiListReceived(final String[] wifiList) {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onWifiListReceived(wifiList);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifyWifiListFailed(final String message) {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onWifiListFailed(message);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifyProvisioningRejected() {
|
||||
runOnMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (callback != null) {
|
||||
callback.onProvisioningRejected();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.ckkj.water.espBle;
|
||||
|
||||
/**
|
||||
* @author fengxiaoyang
|
||||
* @date 2026/6/4
|
||||
* @description 蓝牙设备配网回调接口
|
||||
*/
|
||||
public interface ProvisionCallback {
|
||||
|
||||
/**
|
||||
* 正在连接设备
|
||||
*/
|
||||
void onConnecting();
|
||||
|
||||
/**
|
||||
* 设备连接成功
|
||||
*/
|
||||
void onConnected();
|
||||
|
||||
/**
|
||||
* 正在配网中
|
||||
* @param progress 配网进度 (0-100)
|
||||
*/
|
||||
void onProvisioning(int progress);
|
||||
|
||||
/**
|
||||
* 配网成功
|
||||
* @param deviceName 设备名称
|
||||
* @param deviceType 设备类型
|
||||
*/
|
||||
void onSuccess(String deviceName, String deviceType);
|
||||
|
||||
/**
|
||||
* 配网失败
|
||||
* @param errorCode 错误码
|
||||
* @param message 错误信息
|
||||
*/
|
||||
void onFailed(int errorCode, String message);
|
||||
|
||||
/**
|
||||
* 设备断开连接
|
||||
*/
|
||||
void onDisconnected();
|
||||
|
||||
/**
|
||||
* WiFi列表获取成功
|
||||
* @param wifiList WiFi列表
|
||||
*/
|
||||
void onWifiListReceived(String[] wifiList);
|
||||
|
||||
/**
|
||||
* 获取WiFi列表失败
|
||||
* @param message 错误信息
|
||||
*/
|
||||
void onWifiListFailed(String message);
|
||||
|
||||
/**
|
||||
* 配网被拒绝(设备端拒绝配网请求)
|
||||
*/
|
||||
void onProvisioningRejected();
|
||||
}
|
||||
125
app/src/main/java/com/ckkj/water/espBle/ProvisionConfig.java
Normal file
125
app/src/main/java/com/ckkj/water/espBle/ProvisionConfig.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package com.ckkj.water.espBle;
|
||||
|
||||
import android.os.Build;
|
||||
|
||||
import com.espressif.provisioning.ESPConstants;
|
||||
|
||||
/**
|
||||
* @author fengxiaoyang
|
||||
* @date 2026/6/4
|
||||
* @description 蓝牙配网配置管理类
|
||||
*/
|
||||
public class ProvisionConfig {
|
||||
|
||||
/**
|
||||
* ESP BLE Service UUID
|
||||
*/
|
||||
public static final String SERVICE_UUID =
|
||||
"0000ffff-0000-1000-8000-00805f9b34fb";
|
||||
|
||||
/**
|
||||
* ESP BLE Characteristic UUID for writes
|
||||
*/
|
||||
public static final String CHARACTERISTIC_UUID =
|
||||
"0000ff01-0000-1000-8000-00805f9b34fb";
|
||||
|
||||
/**
|
||||
* ESP BLE Characteristic UUID for notifications
|
||||
*/
|
||||
public static final String NOTIFY_UUID =
|
||||
"0000ff02-0000-1000-8000-00805f9b34fb";
|
||||
|
||||
/**
|
||||
* 设备传输的协议类型
|
||||
*/
|
||||
public static final String TRANSPORT_BLE = "ble";
|
||||
|
||||
/**
|
||||
* 设备前缀 (用于过滤设备)
|
||||
*/
|
||||
public static final String DEVICE_PREFIX = "PROV_";
|
||||
|
||||
/**
|
||||
* 安全类型 - SECURITY_0 (无安全)
|
||||
*/
|
||||
public static final int SECURITY_0 = ESPConstants.SecurityType.SECURITY_0.ordinal();
|
||||
|
||||
/**
|
||||
* 安全类型 - SECURITY_1 (推荐)
|
||||
*/
|
||||
public static final int SECURITY_1 = ESPConstants.SecurityType.SECURITY_1.ordinal();
|
||||
|
||||
/**
|
||||
* 默认安全类型
|
||||
*/
|
||||
public static final int DEFAULT_SECURITY = SECURITY_1;
|
||||
|
||||
/**
|
||||
* 连接超时时间 (毫秒)
|
||||
*/
|
||||
public static final int CONNECTION_TIMEOUT = 30000;
|
||||
|
||||
/**
|
||||
* 配网超时时间 (毫秒)
|
||||
*/
|
||||
public static final int PROVISION_TIMEOUT = 120000;
|
||||
|
||||
public static final int ERROR_NOT_INITIALIZED = 1000;
|
||||
|
||||
public static final int ERROR_INVALID_DEVICE = 1001;
|
||||
|
||||
public static final int ERROR_CONNECT_TIMEOUT = 1002;
|
||||
|
||||
public static final int ERROR_CONNECT_FAILED = 1003;
|
||||
|
||||
public static final int ERROR_NOT_CONNECTED = 1004;
|
||||
|
||||
public static final int ERROR_SESSION_FAILED = 1005;
|
||||
|
||||
public static final int ERROR_WIFI_CONFIG_FAILED = 1006;
|
||||
|
||||
public static final int ERROR_WIFI_APPLY_FAILED = 1007;
|
||||
|
||||
public static final int ERROR_PROVISION_FAILED = 1008;
|
||||
|
||||
public static final int ERROR_PROVISION_TIMEOUT = 1009;
|
||||
|
||||
public static final int ERROR_WIFI_SCAN_FAILED = 1010;
|
||||
|
||||
/**
|
||||
* 配网版本 (可根据实际固件调整)
|
||||
*/
|
||||
public static final String VERSION = "v1.1";
|
||||
|
||||
/**
|
||||
* 生成蓝牙扫描过滤器
|
||||
* @return 服务UUID数组
|
||||
*/
|
||||
public static String[] getScanServiceUuids() {
|
||||
return new String[]{SERVICE_UUID};
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成设备前缀过滤器 (用于过滤非ESP设备)
|
||||
* @return 设备前缀
|
||||
*/
|
||||
public static String getDevicePrefix() {
|
||||
return DEVICE_PREFIX;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前Android版本号
|
||||
* @return Android版本号
|
||||
*/
|
||||
public static int getAndroidVersion() {
|
||||
return Build.VERSION.SDK_INT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为Android 10及以上版本
|
||||
* @return true 表示支持
|
||||
*/
|
||||
public static boolean isAndroid10OrAbove() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user