Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions ios/Classes/Bluetooth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class BLEConnectionContext {
let semaphore = DispatchSemaphore(value: 0)
var readBuffer = Data()
var readBufferLock = NSLock()
// Track received packets to detect and skip duplicates
// Key: packet data hash, Value: true if seen
var seenPackets: Set<Data> = []
}

class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {
Expand Down Expand Up @@ -108,7 +105,6 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
if let ctx = currentContext {
ctx.readBufferLock.lock()
ctx.readBuffer.removeAll()
ctx.seenPackets.removeAll()
// Drain semaphore to match the cleared buffer — prevents stale signals
// from unblocking a future readBlocking() call with no data
while ctx.semaphore.wait(timeout: .now()) == .success {}
Expand Down Expand Up @@ -332,15 +328,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
print("BLE: ERROR, expected 64 bytes")
}

// Deduplicate: skip if we've already seen this exact packet
ctx.readBufferLock.lock()
if ctx.seenPackets.contains(data) {
print("BLE: skipping duplicate packet: \(data.prefix(8).hexEncodedString())...")
ctx.readBufferLock.unlock()
return
}
ctx.seenPackets.insert(data)

print("BLE: received data: \(data.hexEncodedString())")
ctx.readBuffer.append(data)
// Signal inside lock to keep buffer and semaphore count in sync
Expand Down