Skip to content
Merged
Show file tree
Hide file tree
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: 6 additions & 6 deletions AirSync.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 30;
CURRENT_PROJECT_VERSION = 31;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = WCB4HTANA6;
ENABLE_APP_SANDBOX = NO;
Expand All @@ -295,7 +295,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.5;
MARKETING_VERSION = 4.0.0;
MARKETING_VERSION = 4.0.1;
PRODUCT_BUNDLE_IDENTIFIER = "sameerasw.airsync-mac";
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
Expand Down Expand Up @@ -456,7 +456,7 @@
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 30;
CURRENT_PROJECT_VERSION = 31;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = WCB4HTANA6;
ENABLE_APP_SANDBOX = NO;
Expand All @@ -475,7 +475,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.5;
MARKETING_VERSION = 4.0.0;
MARKETING_VERSION = 4.0.1;
PRODUCT_BUNDLE_IDENTIFIER = "sameerasw.airsync-mac";
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
Expand Down Expand Up @@ -508,7 +508,7 @@
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 30;
CURRENT_PROJECT_VERSION = 31;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = WCB4HTANA6;
ENABLE_APP_SANDBOX = NO;
Expand All @@ -527,7 +527,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.5;
MARKETING_VERSION = 4.0.0;
MARKETING_VERSION = 4.0.1;
PRODUCT_BUNDLE_IDENTIFIER = "sameerasw.airsync-mac";
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
Expand Down
75 changes: 70 additions & 5 deletions airsync-mac/Core/BLE/BLECentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class BLECentralManager: NSObject, ObservableObject {
isManuallyDisconnected = true

if let peripheral = discoveredPeripheral {
peripheral.delegate = nil
centralManager.cancelPeripheralConnection(peripheral)
}
connectionStatus = .disconnected
Expand Down Expand Up @@ -220,6 +221,7 @@ class BLECentralManager: NSObject, ObservableObject {
guard let self = self else { return }
print("[BLE] Manual connection timed out, cancelling...")
if let p = self.discoveredPeripheral {
p.delegate = nil
self.centralManager.cancelPeripheralConnection(p)
}
self.discoveredPeripheral = nil
Expand Down Expand Up @@ -300,6 +302,7 @@ extension BLECentralManager: CBCentralManagerDelegate {
guard let self = self else { return }
print("[BLE] Connection timed out, cancelling and retrying...")
if let p = self.discoveredPeripheral {
p.delegate = nil
self.centralManager.cancelPeripheralConnection(p)
}
self.discoveredPeripheral = nil
Expand Down Expand Up @@ -334,6 +337,7 @@ extension BLECentralManager: CBCentralManagerDelegate {
}

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
peripheral.delegate = nil
connectionTimer?.invalidate()
connectionTimer = nil
connectingDeviceUUID = nil
Expand All @@ -353,6 +357,7 @@ extension BLECentralManager: CBCentralManagerDelegate {
}

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
peripheral.delegate = nil
connectionTimer?.invalidate()
connectionTimer = nil
watchdogTimer?.invalidate()
Expand Down Expand Up @@ -384,16 +389,26 @@ extension BLECentralManager: CBCentralManagerDelegate {

extension BLECentralManager: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let services = peripheral.services else { return }
for service in services {
if let error = error {
print("[BLE] Error discovering services: \(error.localizedDescription)")
return
}
guard let rawServices = peripheral.services as NSArray? else { return }
let validServices = rawServices.compactMap { $0 as? CBService }
for service in validServices {
peripheral.discoverCharacteristics(nil, for: service)
}
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let chars = service.characteristics else { return }
print("[BLE] Discovered \(chars.count) characteristics for service \(service.uuid)")
for char in chars {
if let error = error {
print("[BLE] Error discovering characteristics for \(service.uuid): \(error.localizedDescription)")
return
}
guard let rawChars = service.characteristics as NSArray? else { return }
let validChars = rawChars.compactMap { $0 as? CBCharacteristic }
print("[BLE] Discovered \(validChars.count) characteristics for service \(service.uuid)")
for char in validChars {
characteristics[char.uuid] = char

if char.properties.contains(.notify) {
Expand Down Expand Up @@ -430,6 +445,10 @@ extension BLECentralManager: CBPeripheralDelegate {
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
print("[BLE] Error updating value for characteristic \(characteristic.uuid): \(error.localizedDescription)")
return
}
resetWatchdog()
guard let data = characteristic.value else { return }

Expand Down Expand Up @@ -484,6 +503,52 @@ extension BLECentralManager: CBPeripheralDelegate {
}
}

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
print("[BLE] Error writing value to characteristic \(characteristic.uuid): \(error.localizedDescription)")
}
}

func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
print("[BLE] Error updating notification state for characteristic \(characteristic.uuid): \(error.localizedDescription)")
} else {
print("[BLE] Notification state updated for \(characteristic.uuid): \(characteristic.isNotifying)")
}
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
print("[BLE] Error discovering descriptors for \(characteristic.uuid): \(error.localizedDescription)")
}
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: Error?) {
if let error = error {
print("[BLE] Error updating value for descriptor \(descriptor.uuid): \(error.localizedDescription)")
}
}

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
if let error = error {
print("[BLE] Error writing value for descriptor \(descriptor.uuid): \(error.localizedDescription)")
}
}

func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral) {
print("[BLE] Peripheral ready to send write without response")
}

func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]) {
print("[BLE] Services invalidated: \(invalidatedServices.map { $0.uuid.uuidString })")
}

func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {
if let error = error {
print("[BLE] Error reading RSSI: \(error.localizedDescription)")
}
}

private func handleChunkedUpdate(uuid: CBUUID, data: Data) {
guard let (current, total) = BLEChunkUtil.parseHeader(from: data) else { return }
let payload = BLEChunkUtil.getPayload(from: data)
Expand Down