-
Notifications
You must be signed in to change notification settings - Fork 1
Added LocationManagerProtocol to support macOS and Linux #4
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
base: feature/services/gatt-services
Are you sure you want to change the base?
Changes from 1 commit
9f84c07
722f83a
b032150
2ebeaf1
9466ca9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // | ||
| // DarwinLocationManager.swift | ||
| // gattserver | ||
| // | ||
| // Created by Carlos Duclos on 7/4/18. | ||
| // | ||
|
|
||
| #if os(macOS) && swift(>=3.2) | ||
|
|
||
| import Foundation | ||
| import CoreLocation | ||
|
|
||
| public typealias LocationManager = DarwinLocation | ||
|
|
||
| public class DarwinLocation: NSObject, LocationManagerProtocol { | ||
|
|
||
| internal private(set) var internalManager: CLLocationManager! | ||
|
|
||
| public var locationServicesEnabled: Bool { | ||
|
|
||
| return CLLocationManager.locationServicesEnabled() | ||
| } | ||
|
|
||
| public static var authorizationStatus: CLAuthorizationStatus { | ||
|
|
||
| return CLLocationManager.authorizationStatus() | ||
| } | ||
|
|
||
| override init() { | ||
| super.init() | ||
|
|
||
| internalManager = CLLocationManager() | ||
| internalManager.delegate = self | ||
| } | ||
|
|
||
| public func startUpdatingLocation() { | ||
| print("startUpdatingLocation") | ||
| internalManager.startUpdatingLocation() | ||
| } | ||
|
|
||
| public func stopUpdatingLocation() { | ||
| print("stopUpdatingLocation") | ||
| internalManager.stopUpdatingLocation() | ||
| } | ||
| } | ||
|
|
||
| extension DarwinLocation: CLLocationManagerDelegate { | ||
|
|
||
| public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
|
|
||
| guard let location = locations.first | ||
| else { return } | ||
|
|
||
| didUpdateLocation?(location.coordinate.latitude, location.coordinate.longitude) | ||
| } | ||
|
|
||
| public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { | ||
| print("Error location", error) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // | ||
| // IndoorPositioningService.swift | ||
| // gattserver | ||
| // | ||
| // Created by Carlos Duclos on 7/4/18. | ||
| // | ||
|
|
||
| import Foundation | ||
| import Bluetooth | ||
| import GATT | ||
|
|
||
| public final class GATTIndoorPositioningnServiceController: GATTServiceController { | ||
|
|
||
| public static let service: BluetoothUUID = .indoorPositioning | ||
|
|
||
| // MARK: - Properties | ||
|
|
||
| public let peripheral: PeripheralManager | ||
|
|
||
| public private(set) var latitude: GATTLatitude = 0 | ||
|
|
||
| internal let serviceHandle: UInt16 | ||
|
|
||
| internal let latitudeHandle: UInt16 | ||
|
|
||
| internal let locationManager: LocationManager | ||
|
|
||
| // MARK: - Initialization | ||
|
|
||
| public init(peripheral: PeripheralManager) throws { | ||
|
|
||
| self.peripheral = peripheral | ||
|
|
||
| self.locationManager = LocationManager() | ||
|
|
||
| #if os(macOS) | ||
| let serviceUUID = BluetoothUUID() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is wrong, it should only be done on characteristics CoreBluetooth has blacklisted internally (because of collision with the ones a Mac advertises). |
||
| #else | ||
| let serviceUUID = type(of: self).service | ||
| #endif | ||
|
|
||
| #if os(Linux) | ||
| let descriptors = [GATTClientCharacteristicConfiguration().descriptor] | ||
| #else | ||
| let descriptors: [GATT.Descriptor] = [] | ||
| #endif | ||
|
|
||
| let characteristics = [ | ||
| GATT.Characteristic(uuid: type(of: latitude).uuid, | ||
| value: latitude.data, | ||
| permissions: [.read], | ||
| properties: [.read], | ||
| descriptors: descriptors) | ||
| ] | ||
|
|
||
| let service = GATT.Service(uuid: serviceUUID, | ||
| primary: true, | ||
| characteristics: characteristics) | ||
|
|
||
| self.serviceHandle = try peripheral.add(service: service) | ||
| self.latitudeHandle = peripheral.characteristics(for: type(of: latitude).uuid)[0] | ||
|
|
||
| locationManager.didUpdateLocation = { latitude, longitude in | ||
| print("latitude", latitude) | ||
| print("longitude", longitude) | ||
| } | ||
|
|
||
| locationManager.startUpdatingLocation() | ||
|
|
||
| updateValues() | ||
| } | ||
|
|
||
| deinit { | ||
|
|
||
| self.peripheral.remove(service: serviceHandle) | ||
| } | ||
|
|
||
| // MARK: - Methods | ||
|
|
||
| func updateValues() { | ||
|
|
||
| latitude = GATTLatitude(rawValue: 123) | ||
|
|
||
| peripheral[characteristic: latitudeHandle] = latitude.data | ||
|
|
||
| // UIDevice.current. | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // | ||
| // LinuxLocation.swift | ||
| // gattserver | ||
| // | ||
| // Created by Carlos Duclos on 7/4/18. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| #if os(Linux) | ||
|
|
||
| public typealias LocationManager = LinuxLocation | ||
|
|
||
| public final class LinuxLocation: LocationManagerProtocol { | ||
|
|
||
| public var locationServicesEnabled: Bool { | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| public func startUpdatingLocation() { | ||
|
|
||
| } | ||
|
|
||
| public func stopUpdatingLocation() { | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // | ||
| // LocationManager.swift | ||
| // gattserver | ||
| // | ||
| // Created by Carlos Duclos on 7/4/18. | ||
| // | ||
|
|
||
| import Foundation | ||
| import CoreLocation | ||
|
|
||
| public protocol LocationManagerProtocol: class { | ||
|
|
||
| var didUpdateLocation: ((Double, Double) -> Void)? { get set } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a struct like |
||
|
|
||
| var locationServicesEnabled: Bool { get } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should only exist on the Darwin version, and in any case should be |
||
|
|
||
| func startUpdatingLocation() | ||
|
|
||
| func stopUpdatingLocation() | ||
| } | ||
|
|
||
| extension LocationManagerProtocol { | ||
|
|
||
| public var didUpdateLocation: ((Double, Double) -> Void)? { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason to provide default implementation, we want compiler error if this is not implemented |
||
| get { return nil } | ||
| set { } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
letsince this will never change