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