wjc
2026-03-19 b2340ac4ce1c9ea2070c0e7fb72a03fb2b2500ad
app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -2,6 +2,8 @@
import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanResult;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -9,6 +11,7 @@
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.IBinder;
import android.os.SystemClock;
import android.provider.Settings;
@@ -49,6 +52,7 @@
import com.hdl.photovoltaic.ui.bean.CUserInfo;
import com.hdl.photovoltaic.ui.bean.ChatMessageBean;
import com.hdl.photovoltaic.ui.bean.CloudInverterDeviceBean;
import com.hdl.photovoltaic.ui.bean.CustomBluetoothBean;
import com.hdl.photovoltaic.ui.bean.DeviceInfoBean;
import com.hdl.photovoltaic.ui.bean.DeviceTimeBean;
import com.hdl.photovoltaic.ui.bean.Geolocation;
@@ -71,6 +75,7 @@
import com.hdl.photovoltaic.ui.newC.PowerStationsListActivity;
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
import com.hdl.photovoltaic.utils.BleWifiConfiguratorUtils;
import com.hdl.photovoltaic.utils.NetworkUtils;
import com.hdl.photovoltaic.utils.PermissionUtils;
import com.hdl.photovoltaic.utils.SharedPreUtils;
@@ -853,7 +858,6 @@
                    //设备定位
                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_GET_DEVICE_LOCATION: {
                        this.uniGetDeviceFind(mode_type, data, TopicApi.DELETING_GATEWAY_FIND, callback);
                        ;
                    }
                    break;
                }
@@ -953,6 +957,157 @@
                    }
                    break;
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_MODEL.equals(topic)) {
                //蓝牙模块
                switch (type) {
                    case HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_PERMISSION: {
                        //请求蓝牙权限
                        boolean isGranted = HdlBluetoothLogic.getInstance().getMissingPermissions();
                        uniSuccessCallback(mode_type, isGranted, callback);
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_OPEN_SETTING: {
                        //前往蓝牙设置页
                        Intent intent = new Intent();
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", HDLApp.getInstance().getPackageName(), null);
                        intent.setData(uri);
                        HDLApp.getInstance().startActivity(intent);
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_LIST: {
                        //搜索/停止蓝牙(startSearch,stopSearch)
                        String startSearch = getKeyValue("operateType", getKeyValue("data", data));
                        String mac = getKeyValue("mac", getKeyValue("data", data));
                        if (!HdlBluetoothLogic.getInstance().checkBluetoothEnabled()) {
                            uniCallbackData(mode_type, 1, -2, HDLApp.getInstance().getString(R.string.bluetooth_not_enabled), callback);
                        }
                        if (startSearch.equals("startSearch")) {
                            HdlBluetoothLogic.getInstance().scanDevices(new BleWifiConfiguratorUtils.ScanListener() {
                                @Override
                                public void onDeviceFound(BluetoothDevice device, int rssi, byte[] scanRecord, ScanResult result) {
                                    try {
//                                    Log.d("===", "onDeviceFound: " + Objects.requireNonNull(result.getScanRecord()).getDeviceName());
                                        if (result.getScanRecord().getDeviceName() == null || !result.getScanRecord().getDeviceName().startsWith("BMS")) {
                                            return;
                                        }
                                        // 检查 MAC 地址是否匹配
                                        String deviceAddress = device.getAddress();
                                        if (!TextUtils.isEmpty(mac) && !deviceAddress.equals(mac)) {
                                            // 如果指定了 MAC 地址且不匹配,直接返回
                                            return;
                                        }
                                        //创建并推送设备信息
                                        CustomBluetoothBean customBluetoothBean = new CustomBluetoothBean();
                                        customBluetoothBean.setBluetoothName(result.getScanRecord().getDeviceName());
                                        customBluetoothBean.setBluetoothMac(device.getAddress());
//                                      customBluetoothBean.setUuid(result.getScanRecord());
                                        HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
                                        uniCallBackBaseBean.setType(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_find_bluetooth_device);
                                        uniCallBackBaseBean.setData(customBluetoothBean);
                                        HdlUniLogic.getInstance().sendUni(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL, uniCallBackBaseBean);
                                    } catch (Exception ignored) {
                                    }
                                    if (ActivityCompat.checkSelfPermission(HDLApp.getInstance(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                                        // TODO: Consider calling
                                        //    ActivityCompat#requestPermissions
                                        // here to request the missing permissions, and then overriding
                                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                        //                                          int[] grantResults)
                                        // to handle the case where the user grants the permission. See the documentation
                                        // for ActivityCompat#requestPermissions for more details.
                                        return;
                                    }
                                    HdlBluetoothLogic.getInstance().stopScan();
                                }
                                @Override
                                public void onScanFailed(int errorCode) {
//                                    uniCallbackData(mode_type, errorCode, -2, "Bluetooth is not enabled.", callback);
                                }
                            });
                        } else {
                            HdlBluetoothLogic.getInstance().stopScan();
//                            uniSuccessCallback(mode_type, true, callback);
                        }
                        uniSuccessCallback(mode_type, true, callback);
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_Connect: {
                        //连接蓝牙
                        String mac = getKeyValue("mac", getKeyValue("data", data));
                        HdlBluetoothLogic.getInstance().connect(mac, new BleWifiConfiguratorUtils.ConnectListener() {
                            @Override
                            public void onConnected() {
                                uniSuccessCallback(mode_type, true, callback);
                            }
                            @Override
                            public void onDisconnected() {
                                uniCallbackData(type, null, -2, "disconnected", callback);
                            }
                            @Override
                            public void onConnectionFailed(String reason) {
                                uniCallbackData(type, null, -2, "connectionFailed " + reason, callback);
                            }
                        });
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_Disconnect: {
                        //断开蓝牙设备
                        String mac = getKeyValue("mac", getKeyValue("data", data));
                        HdlBluetoothLogic.getInstance().disconnect(mac);
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_Send: {
                        //发送数据给设备
                        String objString = getKeyValue("data", data);
                        HdlBluetoothLogic.getInstance().sendConfig(objString, new BleWifiConfiguratorUtils.WriteListener() {
                            @Override
                            public void onWriteSuccess() {
                            }
                            @Override
                            public void onWriteComplete(boolean success) {
                            }
                            @Override
                            public void onWriteFailed(int status) {
                                uniCallbackData(type, status, -2, "write failed", callback);
                            }
                            @Override
                            public void onDeviceResponse(String response) {
                                uniSuccessCallback(mode_type, response, callback);
                            }
                        });
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_status_check: {
                        //检查蓝牙连接状态
                        String mac = getKeyValue("mac", getKeyValue("data", data));
                        boolean status = HdlBluetoothLogic.getInstance().bluetoothStatusCheck(mac);
                        uniSuccessCallback(mode_type, status, callback);
                    }
                    break;
                    case HDLUniMP.UNI_EVENT_REPLY_BLUETOOTH_Release: {
                        //释放蓝牙设备资源
                        HdlBluetoothLogic.getInstance().release();
                    }
                    break;
                }
            }
        } catch (Exception e) {