6
\$\begingroup\$

I've implemented a class that lets you check out whether there is an ApplePencil connected to the device or not.

import Foundation
import CoreBluetooth

class ApplePencilReachability: NSObject {

    fileprivate let centralManager = CBCentralManager()

    var pencilReachabilityDidChangeClosure:((_ isAvailable: Bool) -> Void)?

    var isPencilReachable = false
    var timer: Timer?

    convenience init(didChangeClosure:@escaping ((_ isAvailable: Bool) -> Void)) {

        self.init()

        centralManager.delegate = self

        pencilReachabilityDidChangeClosure = didChangeClosure
    }

    @objc fileprivate func checkReachability() {

        let peripherals = centralManager.retrieveConnectedPeripherals(withServices: [CBUUID(string: "180A")])
        let wasPencilReachable = isPencilReachable

        isPencilReachable = peripherals.contains(where: isApplePencil)

        if wasPencilReachable != isPencilReachable {

            pencilReachabilityDidChangeClosure?(isPencilReachable)
        }

        if isPencilReachable {

            timer?.invalidate()
        }
    }

    fileprivate func isApplePencil(peripheral: CBPeripheral) -> Bool {

        return peripheral.name == "Apple Pencil"
    }
}

// MARK: CBCentralManagerDelegate

extension ApplePencilReachability: CBCentralManagerDelegate {

    func centralManagerDidUpdateState(_ central: CBCentralManager) {

        if central.state == .poweredOn {

            timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(checkReachability), userInfo: nil, repeats: true)
        }
        else {

            timer?.invalidate()

            pencilReachabilityDidChangeClosure?(false)
        }
    }
}

It really like to get some tipps to improve this class and make it more 'swifty', if possible :)

This class is also available open-source on Github so feel free to submit Pull-Requests for improvements :)

While writing this class I was inspired by HAS' answer on StackOverflow on this topic.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ It might be pure chance, but your code looks quite similar to this one posted on SO: stackoverflow.com/a/41264961/1187415. \$\endgroup\$ – Martin R Jan 24 '17 at 9:50
  • \$\begingroup\$ @MartinR this answer was an inspiration to write this code like it looks like, but thinks like the extension aren't there. Also I've added an conenience initializer which takes directly the closure to notify the callee. \$\endgroup\$ – regetskcob Jan 24 '17 at 9:53
  • \$\begingroup\$ What's the point of it being a subclass of NSObject? \$\endgroup\$ – GetSwifty Feb 6 '17 at 16:10
  • \$\begingroup\$ @PEEJWEEJ - It wasn't possible to extend the class, if i do not subclass from NSObject, because the class doesn't conform to NSObjectProtocol then. \$\endgroup\$ – regetskcob Feb 7 '17 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.