| | |
| | | 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; |
| | | |
| | | /** |
| | | * 蓝牙逻辑 |
| | |
| | | * 扫描设备 |
| | | */ |
| | | @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN) |
| | | public void scanDevices() { |
| | | public void scanDevices(BleWifiConfiguratorUtils.ScanListener listener) { |
| | | 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) { |
| | | |
| | | } |
| | | }); |
| | | this.configurator.startScan(listener); |
| | | } |
| | | |
| | | /** |
| | | * 连接设备 |
| | | * |
| | | * @param deviceAddress 目标设备BluetoothDevice |
| | | * @param listener 回调监听 |
| | | */ |
| | | @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) |
| | | public void connect() { |
| | | public void connect(String deviceAddress, BleWifiConfiguratorUtils.ConnectListener listener) { |
| | | 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) { |
| | | |
| | | } |
| | | }); |
| | | this.configurator.connect(deviceAddress, listener); |
| | | } |
| | | |
| | | /** |
| | | * 发送配置信息 |
| | | * |
| | | * @param str 发送数据 |
| | | * @param listener 回调监听 |
| | | */ |
| | | @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) |
| | | public void sendConfig() { |
| | | public void sendConfig(String str, BleWifiConfiguratorUtils.WriteListener listener) { |
| | | 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) { |
| | | |
| | | } |
| | | }); |
| | | this.configurator.writeCredentials(str, listener); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * 断开设备连接 |
| | | * * @param deviceAddress 目标设备BluetoothDevice |
| | | */ |
| | | public void disconnect() { |
| | | 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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | 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; |
| | | } |
| | | } |