Skip to content

Commit

Permalink
Merge pull request #30 from XYOracleNetwork/develop
Browse files Browse the repository at this point in the history
include swift package support with minor doc update
  • Loading branch information
Phillip Lorenzo authored Feb 27, 2020
2 parents 3ec0f70 + db438c2 commit f181df2
Show file tree
Hide file tree
Showing 65 changed files with 5,985 additions and 18 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/build.yml

This file was deleted.

17 changes: 17 additions & 0 deletions .github/workflows/swiftBuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Swift Build

on:
push:
branches-ignore:
- "master"

jobs:
build:

runs-on: macOS-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: |
swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios13.0-simulator"
61 changes: 61 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"object": {
"pins": [
{
"package": "Apollo",
"repositoryURL": "https://github.com/apollographql/apollo-ios.git",
"state": {
"branch": null,
"revision": "6b1b04334f73879232a4ab5320c5431ad295020a",
"version": "0.23.0"
}
},
{
"package": "Promises",
"repositoryURL": "https://github.com/google/promises",
"state": {
"branch": null,
"revision": "6b6f4fe3385ed37389fb231bc40be81de2f1230f",
"version": "1.2.8"
}
},
{
"package": "XyBaseSdk",
"repositoryURL": "https://github.com/XYOracleNetwork/sdk-base-swift.git",
"state": {
"branch": null,
"revision": "837e268d9b167d02fa8a047b15b0f7cca6eff75b",
"version": "1.0.4"
}
},
{
"package": "SQLite.swift",
"repositoryURL": "https://github.com/stephencelis/SQLite.swift.git",
"state": {
"branch": null,
"revision": "0a9893ec030501a3956bee572d6b4fdd3ae158a1",
"version": "0.12.2"
}
},
{
"package": "Starscream",
"repositoryURL": "https://github.com/daltoniam/Starscream",
"state": {
"branch": null,
"revision": "e6b65c6d9077ea48b4a7bdda8994a1d3c6969c8d",
"version": "3.1.1"
}
},
{
"package": "swift-nio-zlib-support",
"repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git",
"state": {
"branch": null,
"revision": "37760e9a52030bb9011972c5213c3350fa9d41fd",
"version": "1.0.0"
}
}
]
},
"version": 1
}
25 changes: 23 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,28 @@ import PackageDescription

let package = Package(
name: "XyBleSdk",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "sdk-ble-swift",
targets: ["sdk-ble-swift"]),
],
dependencies: [
.package(url: "https://github.com/google/promises", from: "1.2.8")
]
.package(url: "https://github.com/google/promises", from: "1.2.8"),
.package(url: "https://github.com/XYOracleNetwork/sdk-base-swift.git", from: "1.0.4")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "sdk-ble-swift",
dependencies: ["Promises", "XyBaseSdk"]),
.testTarget(
name: "sdk-ble-swiftTests",
dependencies: ["sdk-ble-swift"]),
]
)
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![logo]](https://xyo.network)

![](https://github.com/XYOracleNetwork/sdk-ble-swift/workflows/Pod%20Build/badge.svg?branch=develop)
![](https://github.com/XYOracleNetwork/sdk-ble-swift/workflows/Swift%20Build/badge.svg?branch=develop)

[![BCH compliance](https://bettercodehub.com/edge/badge/XYOracleNetwork/sdk-ble-swift?branch=master)](https://bettercodehub.com/)
[![](https://img.shields.io/cocoapods/v/XyBleSdk.svg?style=flat)](https://cocoapods.org/pods/XyBleSdk)
Expand Down Expand Up @@ -36,6 +37,12 @@ A Bluetooth library, primarily for use with XY Finder devices but can be impleme

## Install

### Swift Package Manager

```swift
.package(url: "https://github.com/XYOracleNetwork/sdk-ble-swift.git", from: "3.1.3")
```

### CocoaPods

> Note that CocoaPods support is only for iOS currently
Expand Down
115 changes: 115 additions & 0 deletions Sources/sdk-ble-swift/Constants/XYConstants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// XYConstants.swift
// XYBleSdk
//
// Created by Darren Sutherland on 9/7/18.
// Copyright © 2018 XY - The Findables Company. All rights reserved.
//

import Foundation

internal struct XYConstants {
static let DEVICE_TUNING_SECONDS_INTERVAL_CONNECTED_RSSI_READ = 3
static let DEVICE_TUNING_LOCATION_CHANGE_THRESHOLD = 10.0
static let DEVICE_TUNING_SECONDS_EXIT_CHECK_INTERVAL = 1.0
static let DEVICE_TUNING_SECONDS_WITHOUT_SIGNAL_FOR_EXITING = 12.0

static let DEVICE_TUNING_SECONDS_WITHOUT_SIGNAL_FOR_EXIT_GAP_SIZE = 2.0
static let DEVICE_TUNING_SECONDS_WITHOUT_SIGNAL_FOR_EXIT_WINDOW_COUNT = 3
static let DEVICE_TUNING_SECONDS_WITHOUT_SIGNAL_FOR_EXIT_WINDOW_SIZE = 2.5

static let DEVICE_POWER_LOW: UInt8 = 0x04
static let DEVICE_POWER_HIGH: UInt8 = 0x08
static let DEVICE_LOCK_DEFAULT = Data([0x2f, 0xbe, 0xa2, 0x07, 0x52, 0xfe, 0xbf, 0x31, 0x1d, 0xac, 0x5d, 0xfa, 0x7d, 0x77, 0x76, 0x80])
static let DEVICE_LOCK_XY4 = Data([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f])

static let DEVICE_CONNECTABLE_SOURCE_UUID_XY4 = NSUUID(uuidString: "00000000-785F-0000-0000-0401F4AC4EA4")
static let DEVICE_CONNECTABLE_SOURCE_UUID_DEFAULT = NSUUID(uuidString: "a500248c-abc2-4206-9bd7-034f4fc9ed10")
}

public enum XYDeviceProximity: Int {
case none
case outOfRange
case veryFar
case far
case medium
case near
case veryNear
case touching

public static func fromSignalStrength(_ strength: Int) -> XYDeviceProximity {
if strength == -999 { return XYDeviceProximity.none }
if strength >= -40 { return XYDeviceProximity.touching }
if strength >= -60 { return XYDeviceProximity.veryNear }
if strength >= -70 { return XYDeviceProximity.near }
if strength >= -80 { return XYDeviceProximity.medium }
if strength >= -90 { return XYDeviceProximity.far }
if strength >= -200 { return XYDeviceProximity.veryFar }
return XYDeviceProximity.outOfRange
}

public static let defaultProximity: Int = -999
}

public enum XYButtonType2 : Int {
case none
case single
case double
case long
}



public enum XYFinderSong {
case off
case findIt

public func values(for device: XYDeviceFamily) -> [UInt8] {
switch self {
case .off:
switch device.id {
case XY4BluetoothDevice.id:
return [0xff, 0x03]
default:
return [0xff]
}
case .findIt:
switch device.id {
case XY4BluetoothDevice.id:
return [0x0b, 0x03]
case XY2BluetoothDevice.id:
return [0x01]
default:
return [0x02]
}
}

}
}

// A generic semaphore lock with configurable lock amounts and timeout
internal class GenericLock {

// Default 5 minute wait time
private static let genericLockTimeout: TimeInterval = 300

private let
semaphore: DispatchSemaphore,
waitTimeout: TimeInterval

init(_ value: Int = 1, timeout: TimeInterval = GenericLock.genericLockTimeout) {
self.semaphore = DispatchSemaphore(value: value)
self.waitTimeout = timeout
}

public func lock() {
if self.semaphore.wait(timeout: .now() + self.waitTimeout) == .timedOut {
self.unlock()
}
}

public func unlock() {
self.semaphore.signal()
}

}
49 changes: 49 additions & 0 deletions Sources/sdk-ble-swift/Devices/Agents/XYCentralAgent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// XYCentralAgent.swift
// XYBleSdk
//
// Created by Darren Sutherland on 11/1/18.
// Copyright © 2018 XY - The Findables Company. All rights reserved.
//

import CoreBluetooth
import Promises

// An agent that allows for adding connecting to the Central in an XYBluetoothDevice connection block
// or any other Promises chain
public final class XYCentralAgent: XYCentralDelegate {
private let
central = XYCentral.instance,
delegateKey: String

private lazy var promise = Promise<Void>.pending()

public init() {
self.delegateKey = "XYCentralAgent:\(UUID.init().uuidString)"
}

@discardableResult public func powerOn() -> Promise<Void> {
guard self.central.state != .poweredOn else { return Promise(()) }

self.central.setDelegate(self, key: self.delegateKey)
self.central.enable()

return promise.always(on: XYCentral.centralQueue) {
self.central.removeDelegate(for: self.delegateKey)
}
}

public func stateChanged(newState: CBManagerState) {
newState == .poweredOn ?
promise.fulfill(()) :
promise.reject(XYBluetoothError.couldNotPowerOnCentral)
}

// Unused
public func located(peripheral: XYPeripheral) {}
public func discovered(beacon: XYIBeaconDefinition) {}
public func connected(peripheral: XYPeripheral) {}
public func timeout() {}
public func couldNotConnect(peripheral: XYPeripheral) {}
public func disconnected(periperhal: XYPeripheral) {}
}
Loading

0 comments on commit f181df2

Please sign in to comment.