This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 1- checks if the characteristic is correctly discovered | |
| 2- Register for notifications using the dataFuture variable | |
| */ | |
| let dataFuture = discoveryFuture.flatMap { service -> Future<Characteristic> in | |
| guard let dataCharacteristic = service.characteristic(dateCharacteristicUUID) else { | |
| throw AppError.dataCharactertisticNotFound | |
| } | |
| self.dataCharacteristic = dataCharacteristic | |
| DispatchQueue.main.async { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //we will next discover the "ec00" service in order be able to access its characteristics | |
| let discoveryFuture = connectionFuture.flatMap { peripheral -> Future<Peripheral> in | |
| return peripheral.discoverServices([serviceUUID]) | |
| }.flatMap { discoveredPeripheral -> Future<Service> in | |
| guard let service = discoveredPeripheral.service(serviceUUID) else { | |
| throw AppError.serviceNotFound | |
| } | |
| peripheral = discoveredPeripheral | |
| DispatchQueue.main.async { | |
| self.connectionStatusLabel.text = "Discovered service \(service.uuid.uuidString). Trying to discover characteristics" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //We will connect to the first scanned peripheral | |
| let connectionFuture = scanFuture.flatMap { peripheral -> FutureStream<Peripheral> in | |
| //stop the scan as soon as we find the first peripheral | |
| manager.stopScanning() | |
| DispatchQueue.main.async { | |
| self.connectionStatusLabel.text = "Found peripheral \(peripheral.identifier.uuidString). Trying to connect" | |
| print(self.connectionStatusLabel.text!) | |
| } | |
| //connect to the peripheral in order to trigger the connected mode | |
| return peripheral.connect(connectionTimeout: 10, capacity: 5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //handle state changes and return a scan future if the bluetooth is powered on. | |
| let scanFuture = stateChangeFuture.flatMap { state -> FutureStream<Peripheral> in | |
| switch state { | |
| case .poweredOn: | |
| DispatchQueue.main.async { | |
| self.connectionStatusLabel.text = "start scanning" | |
| print(self.connectionStatusLabel.text!) | |
| } | |
| //scan for peripherlas that advertise the ec00 service | |
| return manager.startScanning(forServiceUUIDs: [serviceUUID]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //initialize a central manager with a restore key. The restore key allows to resuse the same central manager in future calls | |
| let manager = CentralManager(options: [CBCentralManagerOptionRestoreIdentifierKey : "CentralMangerKey" as NSString]) | |
| //A future stram that notifies us when the state of the central manager changes | |
| let stateChangeFuture = manager.whenStateChanges() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let sem = DispatchSemaphore(value: 0) | |
| DispatchQueue.global().async { | |
| print("waiting for at least one signal for 1 second") | |
| //wait for a signal | |
| let res = sem.wait(timeout: DispatchTime.now() + 1) | |
| if(res == .timedOut){ | |
| print("wait timed out") | |
| }else{ | |
| print("At least one signal has been received") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let semaphore = DispatchSemaphore(value: 0) | |
| DispatchQueue.global().async { | |
| print("waiting for at least one signal") | |
| //wait for a signal | |
| semaphore.wait() | |
| print("At least one signal has been received") | |
| } | |
| DispatchQueue.global().async { | |
| sleep(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let dispatchWorkItem = DispatchWorkItem{ | |
| print("work item start") | |
| sleep(1) | |
| print("work item end") | |
| } | |
| let dg = DispatchGroup() | |
| //submiy work items to the group | |
| let dispatchQueue = DispatchQueue(label: "custom dq") | |
| dispatchQueue.async(group: dg) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let dwi3 = DispatchWorkItem { | |
| print("start DispatchWorkItem") | |
| sleep(2) | |
| print("end DispatchWorkItem") | |
| } | |
| //this block will be executed on a the siqpatch queue 'dq' when dwi3 completes | |
| let myDq = DispatchQueue(label: "A custom dispatch queue") | |
| dwi3.notify(queue: myDq) { | |
| print("notify") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //create the dispatch work item | |
| var dwi2:DispatchWorkItem? | |
| dwi2 = DispatchWorkItem { | |
| for i in 1...5 { | |
| print("\(dwi2?.isCancelled)") | |
| if (dwi2?.isCancelled)!{ | |
| break | |
| } | |
| sleep(1) | |
| print("DispatchWorkItem 2: \(i)") |