Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions Sources/gattserver/DarwinLocation.swift
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!
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be let since this will never change


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
3 changes: 2 additions & 1 deletion Sources/gattserver/GATTServiceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public protocol GATTServiceController: class {

internal let serviceControllers: [GATTServiceController.Type] = [
GATTBatteryServiceController.self,
GATTDeviceInformationServiceController.self
GATTDeviceInformationServiceController.self,
GATTIndoorPositioningnServiceController.self
]
88 changes: 88 additions & 0 deletions Sources/gattserver/IndoorPositioningService.swift
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()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.
}
}
31 changes: 31 additions & 0 deletions Sources/gattserver/LinuxLocation.swift
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
29 changes: 29 additions & 0 deletions Sources/gattserver/Location.swift
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 }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a struct like CLLocation


var locationServicesEnabled: Bool { get }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 isEnabled


func startUpdatingLocation()

func stopUpdatingLocation()
}

extension LocationManagerProtocol {

public var didUpdateLocation: ((Double, Double) -> Void)? {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 { }
}

}