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;
|
}
|
}
|