|
|
@ -37,10 +37,8 @@ import com.iflytop.profilometer.core.system.SystemService; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.CountDownLatch; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
@ -82,10 +80,8 @@ public class BleManager { |
|
|
|
|
|
|
|
// 记录设备最后出现时间,用于定时清理(线程安全) |
|
|
|
private final Map<BluetoothDevice, Long> deviceLastSeenMap = Collections.synchronizedMap(new HashMap<>()); |
|
|
|
// 记录已经进行服务查询的设备(以 MAC 地址为 key),避免重复连接(线程安全) |
|
|
|
private final Set<String> processedDevices = Collections.synchronizedSet(new HashSet<>()); |
|
|
|
|
|
|
|
// 定时清理 Handler 和 Runnable(移除超时设备及清除已处理记录) |
|
|
|
// 定时清理 Handler 和 Runnable(移除超时设备) |
|
|
|
private final Handler cleanupHandler = new Handler(Looper.getMainLooper()); |
|
|
|
private final Runnable cleanupRunnable = new Runnable() { |
|
|
|
@SuppressLint("MissingPermission") |
|
|
@ -105,7 +101,6 @@ public class BleManager { |
|
|
|
for (BluetoothDevice device : toRemove) { |
|
|
|
deviceLastSeenMap.remove(device); |
|
|
|
scannedDevices.remove(device); |
|
|
|
processedDevices.remove(device.getAddress()); |
|
|
|
Log.d(TAG, "移除超时设备: " + device.getName() + " - " + device.getAddress()); |
|
|
|
} |
|
|
|
} |
|
|
@ -129,48 +124,6 @@ public class BleManager { |
|
|
|
this.bleDataListener = listener; |
|
|
|
} |
|
|
|
|
|
|
|
private class ServiceDiscoveryGattCallback extends BluetoothGattCallback { |
|
|
|
private final BluetoothDevice device; |
|
|
|
|
|
|
|
public ServiceDiscoveryGattCallback(BluetoothDevice device) { |
|
|
|
this.device = device; |
|
|
|
} |
|
|
|
|
|
|
|
@SuppressLint("MissingPermission") |
|
|
|
@Override |
|
|
|
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { |
|
|
|
if (newState == BluetoothProfile.STATE_CONNECTED) { |
|
|
|
Log.d(TAG, "已连接到 " + device.getAddress() + ",开始发现服务"); |
|
|
|
gatt.discoverServices(); |
|
|
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) { |
|
|
|
Log.d(TAG, "与 " + device.getAddress() + " 断开连接"); |
|
|
|
gatt.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@SuppressLint("MissingPermission") |
|
|
|
@Override |
|
|
|
public void onServicesDiscovered(BluetoothGatt gatt, int status) { |
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) { |
|
|
|
Log.d(TAG, "在 " + device.getAddress() + " 上发现服务"); |
|
|
|
BluetoothGattService service = gatt.getService(SERVICE_UUID); |
|
|
|
if (service != null) { |
|
|
|
// 设备支持目标服务,添加到 scannedDevices 列表(切换到主线程操作) |
|
|
|
new Handler(Looper.getMainLooper()).post(() -> { |
|
|
|
if (!scannedDevices.contains(device)) { |
|
|
|
scannedDevices.add(device); |
|
|
|
Log.d(TAG, "设备 " + device.getName() + " 支持目标服务,已添加"); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
Log.d(TAG, "设备 " + device.getAddress() + " 不支持目标服务"); |
|
|
|
} |
|
|
|
} |
|
|
|
// 完成服务发现后断开连接 |
|
|
|
gatt.disconnect(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 私有构造函数,防止外部直接实例化。 |
|
|
|
* |
|
|
@ -190,7 +143,7 @@ public class BleManager { |
|
|
|
* @param context 上下文 |
|
|
|
* @return BleManager 实例 |
|
|
|
*/ |
|
|
|
public static synchronized BleManager getInstance(Context context) { |
|
|
|
public static synchronized BleManager init(Context context) { |
|
|
|
if (instance == null) { |
|
|
|
instance = new BleManager(context); |
|
|
|
} |
|
|
@ -253,7 +206,7 @@ public class BleManager { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 开始扫描 BLE 设备,扫描到的每个设备均依次连接进行服务查询。 |
|
|
|
* 开始扫描 BLE 设备,通过设备名称过滤(只处理名称以 "SPM" 开头的设备)。 |
|
|
|
* 如果扫描到的设备其 MAC 地址与存储的地址一致,则自动调用 connectToDeviceSync() 进行连接。 |
|
|
|
*/ |
|
|
|
@SuppressLint("MissingPermission") |
|
|
@ -292,7 +245,6 @@ public class BleManager { |
|
|
|
// 清空之前的数据 |
|
|
|
scannedDevices.clear(); |
|
|
|
deviceLastSeenMap.clear(); |
|
|
|
processedDevices.clear(); |
|
|
|
|
|
|
|
// 定义扫描回调 |
|
|
|
scanCallback = new ScanCallback() { |
|
|
@ -326,7 +278,7 @@ public class BleManager { |
|
|
|
public void onBatchScanResults(List<ScanResult> results) { |
|
|
|
for (ScanResult result : results) { |
|
|
|
BluetoothDevice device = result.getDevice(); |
|
|
|
if (device != null && device.getName() != null) { |
|
|
|
if (device != null && device.getName() != null && device.getName().startsWith("SPM")) { |
|
|
|
if (result.getScanRecord() != null) { |
|
|
|
deviceLastSeenMap.put(device, System.currentTimeMillis()); |
|
|
|
} |
|
|
@ -338,14 +290,12 @@ public class BleManager { |
|
|
|
stopScan(); |
|
|
|
connectToDeviceSync(address); |
|
|
|
} |
|
|
|
processedDevices.add(address); |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (processedDevices.contains(address)) { |
|
|
|
continue; |
|
|
|
if (!scannedDevices.contains(device)) { |
|
|
|
scannedDevices.add(device); |
|
|
|
Log.d(TAG, "设备 " + device.getName() + "," + device.getAddress() + " ,已添加至列表 (batch)"); |
|
|
|
} |
|
|
|
processedDevices.add(address); |
|
|
|
device.connectGatt(context, USE_AUTO_CONNECT, new ServiceDiscoveryGattCallback(device)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -389,7 +339,7 @@ public class BleManager { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 返回当前扫描到的设备列表(仅包含支持目标服务的设备)。 |
|
|
|
* 返回当前扫描到的设备列表(仅包含符合名称条件的设备)。 |
|
|
|
*/ |
|
|
|
public List<BluetoothDevice> getScannedDevices() { |
|
|
|
return scannedDevices; |
|
|
@ -491,7 +441,6 @@ public class BleManager { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { |
|
|
|
if (NOTIFY_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { |
|
|
|