Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve how we persist the last manually disconnected device #51

Merged
merged 25 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
10 changes: 5 additions & 5 deletions Sources/SpeziBluetooth/CoreBluetooth/BluetoothManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public class BluetoothManager: Observable, Sendable, Identifiable { // swiftlint

/// Currently ongoing discovery session.
private var discoverySession: DiscoverySession?
/// The identifier of the last manually disconnected device.
/// This is to avoid automatically reconnecting to a device that was manually disconnected.
private(set) var lastManuallyDisconnectedDevice: UUID?

/// The list of nearby bluetooth devices.
///
Expand Down Expand Up @@ -197,6 +200,7 @@ public class BluetoothManager: Observable, Sendable, Identifiable { // swiftlint
func cleanupCBCentral() {
_centralManager = nil
isScanningObserver = nil
lastManuallyDisconnectedDevice = nil
logger.debug("Destroyed the underlying CBCentralManager.")
}

Expand Down Expand Up @@ -407,8 +411,6 @@ public class BluetoothManager: Observable, Sendable, Identifiable { // swiftlint

storage.discoveredPeripherals.removeValue(forKey: id)

discoverySession?.clearManuallyDisconnectedDevice(for: id)

checkForCentralDeinit()
}

Expand Down Expand Up @@ -443,8 +445,6 @@ public class BluetoothManager: Observable, Sendable, Identifiable { // swiftlint
func handlePeripheralDeinit(id uuid: UUID) {
storage.retrievedPeripherals.removeValue(forKey: uuid)

discoverySession?.clearManuallyDisconnectedDevice(for: uuid)

checkForCentralDeinit()
}

Expand Down Expand Up @@ -488,7 +488,7 @@ public class BluetoothManager: Observable, Sendable, Identifiable { // swiftlint
// stale timer is handled in the delegate method
centralManager.cancelPeripheralConnection(peripheral.cbPeripheral)

discoverySession?.deviceManuallyDisconnected(id: peripheral.id)
lastManuallyDisconnectedDevice = peripheral.id
}

private func handledConnected(device: BluetoothPeripheral) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ class DiscoverySession: Sendable {

private var configuration: BluetoothManagerDiscoveryState

/// The identifier of the last manually disconnected device.
/// This is to avoid automatically reconnecting to a device that was manually disconnected.
private(set) var lastManuallyDisconnectedDevice: UUID?

private var autoConnectItem: BluetoothWorkItem?
private(set) var staleTimer: DiscoveryStaleTimer?

Expand Down Expand Up @@ -149,16 +145,6 @@ class DiscoverySession: Sendable {
rssi.intValue >= minimumRSSI && rssi.intValue != 127
}

func deviceManuallyDisconnected(id uuid: UUID) {
lastManuallyDisconnectedDevice = uuid
}

func clearManuallyDisconnectedDevice(for uuid: UUID) {
if lastManuallyDisconnectedDevice == uuid {
lastManuallyDisconnectedDevice = nil
}
}

func deviceDiscoveryPostAction(device: BluetoothPeripheral, newlyDiscovered: Bool) {
if newlyDiscovered {
if staleTimer == nil {
Expand Down Expand Up @@ -204,7 +190,7 @@ extension DiscoverySession {
return nil // auto-connect is disabled
}

guard lastManuallyDisconnectedDevice == nil && !manager.sbHasConnectedDevices else {
guard manager.lastManuallyDisconnectedDevice == nil && !manager.sbHasConnectedDevices else {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ extension CharacteristicPeripheralInjection where Value: ControlPointCharacteris

throw error
}

async let _ = withTimeout(of: timeout) { @SpeziBluetooth in
transaction.signalTimeout()
async let _ = withTimeout(of: timeout) {
await transaction.signalTimeout()
}

return try await response
Expand Down
19 changes: 8 additions & 11 deletions Tests/UITests/UITests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "-enable-upcoming-feature InferSendableFromCaptures";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 6.0;
XROS_DEPLOYMENT_TARGET = 1.0;
};
name = Debug;
Expand Down Expand Up @@ -432,11 +432,11 @@
MACOSX_DEPLOYMENT_TARGET = 14.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
OTHER_SWIFT_FLAGS = "-enable-upcoming-feature InferSendableFromCaptures";
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 6.0;
VALIDATE_PRODUCT = YES;
XROS_DEPLOYMENT_TARGET = 1.0;
};
Expand All @@ -448,10 +448,11 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 637867499T;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = 484YT3X9X7;
DEVELOPMENT_TEAM = 637867499T;
ENABLE_HARDENED_RUNTIME = NO;
ENABLE_PREVIEWS = YES;
ENABLE_TESTING_SEARCH_PATHS = YES;
Expand All @@ -469,15 +470,14 @@
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = edu.stanford.spezi.bluetooth.testapplication2;
PRODUCT_BUNDLE_IDENTIFIER = edu.stanford.spezi.bluetooth.testapplication;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator xros xrsimulator";
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,7";
};
name = Debug;
Expand All @@ -489,6 +489,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "";
Expand Down Expand Up @@ -520,7 +521,6 @@
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,7";
};
name = Release;
Expand All @@ -543,7 +543,6 @@
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_TARGET_NAME = TestApp;
};
Expand Down Expand Up @@ -571,7 +570,6 @@
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_TARGET_NAME = TestApp;
};
Expand Down Expand Up @@ -634,11 +632,11 @@
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "-enable-upcoming-feature InferSendableFromCaptures";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = TEST;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 6.0;
XROS_DEPLOYMENT_TARGET = 1.0;
};
name = Test;
Expand All @@ -650,6 +648,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "";
Expand Down Expand Up @@ -681,7 +680,6 @@
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,7";
};
name = Test;
Expand All @@ -708,7 +706,6 @@
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_TARGET_NAME = TestApp;
};
Expand Down
Loading