Skip to content

Commit

Permalink
Improved discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-baumann committed Oct 11, 2024
1 parent bed6e4d commit 0e3cd09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class BLECentralManager(private val context: Context, private val internal: Inte
}

private var scanJob: Job? = null
private val scanIntervalMillis = 5000L
private val scanIntervalMillis = 3000L
private val pauseBetweenScans = 1000L

companion object {
var discoveredPeripherals = mutableListOf<BluetoothDevice>()
Expand Down Expand Up @@ -143,7 +144,9 @@ class BLECentralManager(private val context: Context, private val internal: Inte
while (isActive) {
bluetoothAdapter.adapter.bluetoothLeScanner.startScan(scanFilter, settings, leScanCallback)
delay(scanIntervalMillis)
discoveredPeripherals.clear()
bluetoothAdapter.adapter.bluetoothLeScanner.stopScan(leScanCallback)
delay(pauseBetweenScans)
}
}

Expand All @@ -161,11 +164,11 @@ class BLECentralManager(private val context: Context, private val internal: Inte

@SuppressLint("MissingPermission")
private val leScanCallback: ScanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
if (!discoveredPeripherals.contains(result.device)) {
discoveredPeripherals.add(result.device)
fun addDevice(device: BluetoothDevice) {
if (!discoveredPeripherals.contains(device)) {
discoveredPeripherals.add(device)

result.device.connectGatt(
device.connectGatt(
context,
false,
BluetoothGattCallbackImplementation(internal, discoveredPeripherals),
Expand All @@ -174,5 +177,16 @@ class BLECentralManager(private val context: Context, private val internal: Inte
)
}
}


override fun onScanResult(callbackType: Int, result: ScanResult) {
addDevice(result.device)
}

override fun onBatchScanResults(results: List<ScanResult>) {
results.forEach { result ->
addDevice(result.device)
}
}
}
}
6 changes: 3 additions & 3 deletions bindings/swift/Sources/DataRCT/NearbyServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public class NearbyServer {

lastKnownIp = internalHandler.getCurrentIp()

monitor = serverRunning()
queue = serverRunning.global(qos: .background)
monitor = NWPathMonitor()
queue = DispatchQueue.global(qos: .background)
monitor.pathUpdateHandler = { [self] path in
if path.status == .satisfied {
var newIp = internalHandler.getCurrentIp()
Expand All @@ -97,7 +97,7 @@ public class NearbyServer {
}

public func changeDevice(_ newDevice: Device) {
serverRunning.changeDevice(newDevice: newDevice)
internalHandler.changeDevice(newDevice: newDevice)
}

public func start() async throws {
Expand Down

0 comments on commit 0e3cd09

Please sign in to comment.