Skip to content

Commit

Permalink
Adding Aliases.swift and fixing compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
philips77 committed Nov 17, 2023
1 parent 9abf667 commit 1daa48a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
4 changes: 4 additions & 0 deletions MockingExample.xcodeproj/project.pbxproj
Expand Up @@ -29,6 +29,7 @@
F0A6C9E2299649A500FA75EB /* ForEachWithIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0A6C9E1299649A500FA75EB /* ForEachWithIndex.swift */; };
F0A6C9E5299B95BF00FA75EB /* Data+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0A6C9E4299B95BF00FA75EB /* Data+String.swift */; };
F0CC18AB29C0914300077C3E /* CoreBluetoothMock in Frameworks */ = {isa = PBXBuildFile; productRef = F0CC18AA29C0914300077C3E /* CoreBluetoothMock */; };
F0CC18AD29C092E000077C3E /* Aliases.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0CC18AC29C092E000077C3E /* Aliases.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -76,6 +77,7 @@
F0A6C9DF299544B100FA75EB /* Attribute.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Attribute.swift; sourceTree = "<group>"; };
F0A6C9E1299649A500FA75EB /* ForEachWithIndex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForEachWithIndex.swift; sourceTree = "<group>"; };
F0A6C9E4299B95BF00FA75EB /* Data+String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+String.swift"; sourceTree = "<group>"; };
F0CC18AC29C092E000077C3E /* Aliases.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Aliases.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -135,6 +137,7 @@
F0A6C9C72992982400FA75EB /* Strings.swift */,
F0A6C9D329944BCF00FA75EB /* Colors.swift */,
F0A6C9E4299B95BF00FA75EB /* Data+String.swift */,
F0CC18AC29C092E000077C3E /* Aliases.swift */,
F0A6C9C92993015B00FA75EB /* Info.plist */,
F0A6C9992992752700FA75EB /* Assets.xcassets */,
F0A6C99B2992752700FA75EB /* MockingExample.entitlements */,
Expand Down Expand Up @@ -347,6 +350,7 @@
files = (
F0A6C9C42992778700FA75EB /* ScannedPeripheral.swift in Sources */,
F0A6C9DE2994EA4600FA75EB /* IncludedService.swift in Sources */,
F0CC18AD29C092E000077C3E /* Aliases.swift in Sources */,
F0A6C9DA2994EA3100FA75EB /* Characteristic.swift in Sources */,
F0A6C9DC2994EA3E00FA75EB /* Descriptor.swift in Sources */,
F0A6C9E0299544B100FA75EB /* Attribute.swift in Sources */,
Expand Down
87 changes: 87 additions & 0 deletions MockingExample/Aliases.swift
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2020, Nordic Semiconductor
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

import CoreBluetoothMock

// Copy this file to your project to start using CoreBluetoothMock classes
// without having to refactor any of your code. You will just have to remove
// the imports to CoreBluetooth to fix conflicts and initiate the manager
// using CBCentralManagerFactory, instad of just creating a CBCentralManager.

// disabled for Xcode 12.5 beta
//typealias CBPeer = CBMPeer
//typealias CBAttribute = CBMAttribute
typealias CBCentralManagerFactory = CBMCentralManagerFactory
typealias CBUUID = CBMUUID
typealias CBError = CBMError
typealias CBATTError = CBMATTError
typealias CBManagerState = CBMManagerState
typealias CBPeripheralState = CBMPeripheralState
typealias CBCentralManager = CBMCentralManager
typealias CBCentralManagerDelegate = CBMCentralManagerDelegate
typealias CBPeripheral = CBMPeripheral
typealias CBPeripheralDelegate = CBMPeripheralDelegate
typealias CBService = CBMService
typealias CBCharacteristic = CBMCharacteristic
typealias CBCharacteristicWriteType = CBMCharacteristicWriteType
typealias CBCharacteristicProperties = CBMCharacteristicProperties
typealias CBDescriptor = CBMDescriptor
typealias CBConnectionEvent = CBMConnectionEvent
typealias CBConnectionEventMatchingOption = CBMConnectionEventMatchingOption
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, *)
typealias CBL2CAPPSM = CBML2CAPPSM
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, *)
typealias CBL2CAPChannel = CBML2CAPChannel

let CBCentralManagerScanOptionAllowDuplicatesKey = CBMCentralManagerScanOptionAllowDuplicatesKey
let CBCentralManagerOptionShowPowerAlertKey = CBMCentralManagerOptionShowPowerAlertKey
let CBCentralManagerOptionRestoreIdentifierKey = CBMCentralManagerOptionRestoreIdentifierKey
let CBCentralManagerScanOptionSolicitedServiceUUIDsKey = CBMCentralManagerScanOptionSolicitedServiceUUIDsKey
let CBConnectPeripheralOptionStartDelayKey = CBMConnectPeripheralOptionStartDelayKey
#if !os(macOS)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
let CBConnectPeripheralOptionRequiresANCS = CBMConnectPeripheralOptionRequiresANCS
#endif
let CBCentralManagerRestoredStatePeripheralsKey = CBMCentralManagerRestoredStatePeripheralsKey
let CBCentralManagerRestoredStateScanServicesKey = CBMCentralManagerRestoredStateScanServicesKey
let CBCentralManagerRestoredStateScanOptionsKey = CBMCentralManagerRestoredStateScanOptionsKey

let CBAdvertisementDataLocalNameKey = CBMAdvertisementDataLocalNameKey
let CBAdvertisementDataServiceUUIDsKey = CBMAdvertisementDataServiceUUIDsKey
let CBAdvertisementDataIsConnectable = CBMAdvertisementDataIsConnectable
let CBAdvertisementDataTxPowerLevelKey = CBMAdvertisementDataTxPowerLevelKey
let CBAdvertisementDataServiceDataKey = CBMAdvertisementDataServiceDataKey
let CBAdvertisementDataManufacturerDataKey = CBMAdvertisementDataManufacturerDataKey
let CBAdvertisementDataOverflowServiceUUIDsKey = CBMAdvertisementDataOverflowServiceUUIDsKey
let CBAdvertisementDataSolicitedServiceUUIDsKey = CBMAdvertisementDataSolicitedServiceUUIDsKey

let CBConnectPeripheralOptionNotifyOnConnectionKey = CBMConnectPeripheralOptionNotifyOnConnectionKey
let CBConnectPeripheralOptionNotifyOnDisconnectionKey = CBMConnectPeripheralOptionNotifyOnDisconnectionKey
let CBConnectPeripheralOptionNotifyOnNotificationKey = CBMConnectPeripheralOptionNotifyOnNotificationKey
6 changes: 5 additions & 1 deletion MockingExample/Screens/DeviceScreen.swift
Expand Up @@ -128,7 +128,11 @@ extension DeviceScreen {
super.init()

// Try to connect even if a non-connectable packet was received.
self.centralManager = CBCentralManager(delegate: self, queue: .main)

// If you're creating the central manager in multiple places, set the `forceMock`
// parameter to the same value.
self.centralManager = CBCentralManagerFactory.instance(delegate: self, queue: .main,
forceMock: false)
}

func connect() {
Expand Down
18 changes: 16 additions & 2 deletions MockingExample/Screens/ScannerScreen.swift
Expand Up @@ -52,7 +52,17 @@ extension ScannerScreen {

override init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: .main)

// After adding Aliases.swift, the CBCentralManager becomes an alias for
// CBMCentralManager (note the M in CBM...). The line below throws a compilation
// error, as there are now two implementations of the manager: native and mock.
//
// To fix the error, instead of creating CBCentralManager directly, use the factory.
//
// The last parameter, `forceMock`, can apply mock implementation also on
// physical devices.
centralManager = CBCentralManagerFactory.instance(delegate: self, queue: .main,
forceMock: false)
}

func startScan() {
Expand Down Expand Up @@ -87,7 +97,11 @@ extension ScannerScreen.ViewModel: CBCentralManagerDelegate {

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// Search for the device if we already have it
var p = self.peripherals.first { $0.peripheral == peripheral }
var p = self.peripherals.first {
// CBMPeripheral is a protocol and cannot be Equatable.
// Instead, compare the identifiers.
$0.peripheral.identifier == peripheral.identifier
}
if p == nil {
// Filter those without name, why not.
guard let _ = advertisementData[CBAdvertisementDataLocalNameKey] else {
Expand Down

0 comments on commit 1daa48a

Please sign in to comment.