|
|
@ -30,6 +30,7 @@ import android.widget.Toast; |
|
|
|
|
|
|
|
import androidx.core.content.ContextCompat; |
|
|
|
|
|
|
|
import com.iflytop.profilometer.api.ws.BleWebsocketManager; |
|
|
|
import com.iflytop.profilometer.common.constant.LogTag; |
|
|
|
import com.iflytop.profilometer.core.migration.channel.BleDeviceUartChannel; |
|
|
|
import com.iflytop.profilometer.core.system.DeviceState; |
|
|
@ -53,7 +54,6 @@ import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
* - 低于 Android 12 的设备,BLUETOOTH_SCAN 和 BLUETOOTH_CONNECT 权限直接被视为已授予。 |
|
|
|
*/ |
|
|
|
public class BleManager { |
|
|
|
private static final String TAG = "BLEManager"; |
|
|
|
|
|
|
|
// 与设备通信所用的 UUID(请根据你的设备替换下列 UUID) |
|
|
|
public static final UUID SERVICE_UUID = UUID.fromString("6E8F0001-B5A3-F393-E0A9-E50E24DCCA9E"); |
|
|
@ -102,7 +102,8 @@ public class BleManager { |
|
|
|
for (BluetoothDevice device : toRemove) { |
|
|
|
deviceLastSeenMap.remove(device); |
|
|
|
scannedDevices.remove(device); |
|
|
|
Log.d(TAG, "移除超时设备: " + device.getName() + " - " + device.getAddress()); |
|
|
|
BleWebsocketManager.getInstance().pushBleList(); |
|
|
|
Log.d(LogTag.PROFILOMENTER, "移除超时设备: " + device.getName() + " - " + device.getAddress()); |
|
|
|
} |
|
|
|
} |
|
|
|
cleanupHandler.postDelayed(this, CLEANUP_INTERVAL); |
|
|
@ -136,6 +137,7 @@ public class BleManager { |
|
|
|
if (bluetoothManager != null) { |
|
|
|
bluetoothAdapter = bluetoothManager.getAdapter(); |
|
|
|
} |
|
|
|
cleanupHandler.postDelayed(cleanupRunnable, CLEANUP_INTERVAL); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -244,8 +246,8 @@ public class BleManager { |
|
|
|
return; |
|
|
|
} |
|
|
|
// 清空之前的数据 |
|
|
|
scannedDevices.clear(); |
|
|
|
deviceLastSeenMap.clear(); |
|
|
|
// scannedDevices.clear(); |
|
|
|
// deviceLastSeenMap.clear(); |
|
|
|
|
|
|
|
// 定义扫描回调 |
|
|
|
scanCallback = new ScanCallback() { |
|
|
@ -263,15 +265,14 @@ public class BleManager { |
|
|
|
String storedMac = prefs.getString("mac_address", null); |
|
|
|
if (storedMac != null && storedMac.equals(address)) { |
|
|
|
if (!isConnected()) { |
|
|
|
stopScan(); |
|
|
|
connectToDeviceSync(address); |
|
|
|
BlePingManager.getInstance().startPing(); |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
if (!scannedDevices.contains(device)) { |
|
|
|
scannedDevices.add(device); |
|
|
|
Log.d(TAG, "设备 " + device.getName() + "," + device.getAddress() + " ,已添加至列表"); |
|
|
|
BleWebsocketManager.getInstance().pushBleList(); |
|
|
|
Log.d(LogTag.PROFILOMENTER, "设备 " + device.getName() + "," + device.getAddress() + " ,已添加至列表"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -296,7 +297,8 @@ public class BleManager { |
|
|
|
} |
|
|
|
if (!scannedDevices.contains(device)) { |
|
|
|
scannedDevices.add(device); |
|
|
|
Log.d(TAG, "设备 " + device.getName() + "," + device.getAddress() + " ,已添加至列表 (batch)"); |
|
|
|
BleWebsocketManager.getInstance().pushBleList(); |
|
|
|
Log.d(LogTag.PROFILOMENTER, "设备 " + device.getName() + "," + device.getAddress() + " ,已添加至列表 (batch)"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -315,7 +317,7 @@ public class BleManager { |
|
|
|
.build(); |
|
|
|
|
|
|
|
bluetoothLeScanner.startScan(null, settings, scanCallback); |
|
|
|
cleanupHandler.postDelayed(cleanupRunnable, CLEANUP_INTERVAL); |
|
|
|
|
|
|
|
Log.i(LogTag.PROFILOMENTER, "开始扫描蓝牙列表"); |
|
|
|
} |
|
|
|
|
|
|
@ -336,7 +338,6 @@ public class BleManager { |
|
|
|
bluetoothLeScanner.stopScan(scanCallback); |
|
|
|
scanCallback = null; |
|
|
|
} |
|
|
|
cleanupHandler.removeCallbacks(cleanupRunnable); |
|
|
|
Log.i(LogTag.PROFILOMENTER, "结束扫描蓝牙列表"); |
|
|
|
} |
|
|
|
|
|
|
@ -375,7 +376,7 @@ public class BleManager { |
|
|
|
if (bluetoothGatt != null) { |
|
|
|
BluetoothDevice connectedDevice = bluetoothGatt.getDevice(); |
|
|
|
if (connectedDevice != null && connectedDevice.getAddress().equals(macAddress)) { |
|
|
|
Log.d(TAG, "设备已连接,无需重复连接: " + connectedDevice.getName()); |
|
|
|
Log.d(LogTag.PROFILOMENTER, "设备已连接,无需重复连接: " + connectedDevice.getName()); |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
disconnect(); |
|
|
@ -392,11 +393,11 @@ public class BleManager { |
|
|
|
@Override |
|
|
|
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { |
|
|
|
if (newState == BluetoothProfile.STATE_CONNECTED) { |
|
|
|
Log.d(TAG, "设备已连接,开始发现服务"); |
|
|
|
Log.d(LogTag.PROFILOMENTER, "设备已连接,开始发现服务"); |
|
|
|
// 等待服务发现回调决定成功与否 |
|
|
|
gatt.discoverServices(); |
|
|
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) { |
|
|
|
Log.d(TAG, "设备已断开连接"); |
|
|
|
Log.d(LogTag.PROFILOMENTER, "设备已断开连接"); |
|
|
|
disconnect(); |
|
|
|
if (status != BluetoothGatt.GATT_SUCCESS) { |
|
|
|
Log.e(LogTag.PROFILOMENTER, "连接失败,错误码:" + status); |
|
|
@ -413,7 +414,7 @@ public class BleManager { |
|
|
|
@Override |
|
|
|
public void onServicesDiscovered(BluetoothGatt gatt, int status) { |
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) { |
|
|
|
Log.d(TAG, "服务发现成功,目标服务存在"); |
|
|
|
Log.d(LogTag.PROFILOMENTER, "服务发现成功,目标服务存在"); |
|
|
|
// 获取目标服务 |
|
|
|
BluetoothGattService service = gatt.getService(SERVICE_UUID); |
|
|
|
if (service != null) { |
|
|
@ -467,6 +468,7 @@ public class BleManager { |
|
|
|
try { |
|
|
|
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress); |
|
|
|
bluetoothGatt = device.connectGatt(context, USE_AUTO_CONNECT, syncGattCallback); |
|
|
|
BleWebsocketManager.getInstance().pushBleList(); |
|
|
|
} catch (IllegalArgumentException e) { |
|
|
|
Log.e(LogTag.PROFILOMENTER, "无效的 MAC 地址: " + macAddress, e); |
|
|
|
return false; |
|
|
@ -498,9 +500,6 @@ public class BleManager { |
|
|
|
SharedPreferences.Editor editor = prefs.edit(); |
|
|
|
editor.putString("mac_address", macAddress); |
|
|
|
editor.apply(); |
|
|
|
|
|
|
|
// 连接成功后,移除定时清理任务 |
|
|
|
cleanupHandler.removeCallbacks(cleanupRunnable); |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
return false; |
|
|
@ -559,10 +558,8 @@ public class BleManager { |
|
|
|
SharedPreferences.Editor editor = prefs.edit(); |
|
|
|
editor.remove("mac_address"); |
|
|
|
editor.apply(); |
|
|
|
|
|
|
|
// 断开时取消定时清理任务,防止任务悬挂 |
|
|
|
cleanupHandler.removeCallbacks(cleanupRunnable); |
|
|
|
Log.d(TAG, "当前蓝牙设备已断开连接"); |
|
|
|
BleWebsocketManager.getInstance().pushBleList(); |
|
|
|
Log.d(LogTag.PROFILOMENTER, "当前蓝牙设备已断开连接"); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|