-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
554 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
185 changes: 185 additions & 0 deletions
185
sdk-xyobleinterface-swift/devices/androidX/XyoAndroidXDevice.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// | ||
// XyoAndroidXDevice.swift | ||
// sdk-xyobleinterface-swift | ||
// | ||
// Created by Carter Harrison on 4/9/19. | ||
// | ||
|
||
import Foundation | ||
import sdk_objectmodel_swift | ||
import XyBleSdk | ||
import CoreBluetooth | ||
|
||
public enum XyoAndroidWifiStatus { | ||
case notConnected | ||
case connecting | ||
case connected | ||
case unknown | ||
} | ||
|
||
class XyoAndroidNetworkStausListener : XYBluetoothDeviceNotifyDelegate { | ||
var onWifiChangeCallback: ((_: XyoAndroidWifiStatus) -> ())? = nil | ||
|
||
func update(for serviceCharacteristic: XYServiceCharacteristic, value: XYBluetoothResult) { | ||
print("XyoAndroidNetworkStausListener \(value.asString)") | ||
guard let stringValue = value.asString else { | ||
return | ||
} | ||
|
||
if (stringValue == "0") { | ||
onWifiChangeCallback?(XyoAndroidWifiStatus.notConnected) | ||
return | ||
} | ||
|
||
if (stringValue == "1") { | ||
onWifiChangeCallback?(XyoAndroidWifiStatus.connecting) | ||
return | ||
} | ||
|
||
if (stringValue == "2") { | ||
onWifiChangeCallback?(XyoAndroidWifiStatus.connected) | ||
return | ||
} | ||
|
||
onWifiChangeCallback?(XyoAndroidWifiStatus.unknown) | ||
} | ||
} | ||
|
||
class XyoAndroidIpListener : XYBluetoothDeviceNotifyDelegate { | ||
var onIpChange: ((_: String) -> ())? = nil | ||
|
||
func update(for serviceCharacteristic: XYServiceCharacteristic, value: XYBluetoothResult) { | ||
print("XyoAndroidIpListener \(value.asString)") | ||
guard let stringValue = value.asString else { | ||
return | ||
} | ||
|
||
onIpChange?(stringValue) | ||
} | ||
} | ||
|
||
class XyoAndroidSsidListener : XYBluetoothDeviceNotifyDelegate { | ||
var onSsidChange: ((_: String) -> ())? = nil | ||
|
||
func update(for serviceCharacteristic: XYServiceCharacteristic, value: XYBluetoothResult) { | ||
print("XyoAndroidSsidListener \(value.asString)") | ||
guard let stringValue = value.asString else { | ||
return | ||
} | ||
|
||
onSsidChange?(stringValue) | ||
} | ||
} | ||
|
||
|
||
public class XyoAndroidXDevice : XyoDiffereniableDevice { | ||
private var delgateKey = "" | ||
private var hasMutex = false | ||
private let networkStatusListener = XyoAndroidNetworkStausListener() | ||
private let ipListener = XyoAndroidIpListener() | ||
private let ssidListener = XyoAndroidSsidListener() | ||
|
||
|
||
public func onNetworkStatusChange (callback: @escaping (_: XyoAndroidWifiStatus) -> ()) -> Bool { | ||
let key = "onNetworkStatusChange [DBG: \(#function)]: \(Unmanaged.passUnretained(networkStatusListener).toOpaque())" | ||
let result = self.subscribe(to: XyoAndroidXService.status, | ||
delegate: (key: key, delegate: networkStatusListener)) | ||
|
||
networkStatusListener.onWifiChangeCallback = callback | ||
|
||
return result.error == nil | ||
} | ||
|
||
public func onIpChange (callback: @escaping (_: String) -> ()) -> Bool { | ||
let key = "onIpChange [DBG: \(#function)]: \(Unmanaged.passUnretained(ipListener).toOpaque())" | ||
let result = self.subscribe(to: XyoAndroidXService.ip, | ||
delegate: (key: key, delegate: ipListener)) | ||
|
||
ipListener.onIpChange = callback | ||
|
||
return result.error == nil | ||
} | ||
|
||
public func onSsidChange (callback: @escaping (_: String) -> ()) -> Bool { | ||
let key = "onSsidChange [DBG: \(#function)]: \(Unmanaged.passUnretained(ssidListener).toOpaque())" | ||
let result = self.subscribe(to: XyoAndroidXService.ssid, | ||
delegate: (key: key, delegate: ssidListener)) | ||
|
||
ssidListener.onSsidChange = callback | ||
|
||
return result.error == nil | ||
} | ||
|
||
|
||
public func getMutex () -> Bool { | ||
self.delgateKey = "mutex [DBG: \(#function)]: \(Unmanaged.passUnretained(self).toOpaque())" | ||
print("A") | ||
let result = self.subscribe(to: XyoAndroidXService.mutex, | ||
delegate: (key: self.delgateKey, delegate: self)) | ||
|
||
if (result.error == nil) { | ||
self.hasMutex = true | ||
} | ||
|
||
|
||
return result.error == nil | ||
} | ||
|
||
public func connectToWifi (ssid: String, password: String) -> Bool { | ||
let json = "{\"ssid\": \"\(ssid)\",\"password\": \"\(password)\"}".data(using: .utf8) | ||
|
||
let error = self.set(XyoAndroidXService.connect, value: XYBluetoothResult(data: json)).error | ||
return error == nil | ||
} | ||
|
||
public func releaseMutex () -> Bool { | ||
if (hasMutex) { | ||
let result = self.unsubscribe(from: XyoAndroidXService.mutex, key: self.delgateKey) | ||
|
||
if (result.error == nil) { | ||
hasMutex = false | ||
} | ||
|
||
return result.error == nil | ||
} | ||
|
||
return false | ||
} | ||
|
||
public func getSsids () -> [Substring] { | ||
if (hasMutex) { | ||
guard let result = self.get(XyoAndroidXService.scan).asString else { | ||
return [] | ||
} | ||
|
||
return result.split(separator: ",") | ||
} | ||
|
||
return [] | ||
} | ||
|
||
public func isClaimed () -> Bool { | ||
guard let minor = self.iBeacon?.minor else { | ||
return false | ||
} | ||
|
||
let flags = XyoBuffer() | ||
.put(bits: minor) | ||
.getUInt8(offset: 1) | ||
|
||
return flags & 1 != 0 | ||
} | ||
|
||
public func isConnected () -> Bool { | ||
guard let minor = self.iBeacon?.minor else { | ||
return false | ||
} | ||
|
||
let flags = XyoBuffer() | ||
.put(bits: minor) | ||
.getUInt8(offset: 1) | ||
|
||
return flags & 2 != 0 | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
sdk-xyobleinterface-swift/devices/androidX/XyoAndroidXDeviceCreator.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// XyoAndroidXDeviceCreator.swift | ||
// sdk-xyobleinterface-swift | ||
// | ||
// Created by Carter Harrison on 4/9/19. | ||
// | ||
|
||
import Foundation | ||
import XyBleSdk | ||
|
||
public struct XyoAndroidXDeviceCreator : XyoManufactorDeviceCreator { | ||
public init () {} | ||
|
||
public func createFromIBeacon(iBeacon: XYIBeaconDefinition, rssi: Int) -> XYBluetoothDevice? { | ||
return XyoAndroidXDevice(iBeacon: iBeacon, rssi: rssi) | ||
} | ||
|
||
public func enable (enable : Bool) { | ||
if (enable) { | ||
XyoBluetoothDeviceCreator.manufactorMap[0x04] = XyoAndroidXDeviceCreator() | ||
} else { | ||
XyoBluetoothDeviceCreator.manufactorMap.removeValue(forKey: 0x04) | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
sdk-xyobleinterface-swift/devices/androidX/XyoAndroidXService.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// XyoAndroidXService.swift | ||
// sdk-xyobleinterface-swift | ||
// | ||
// Created by Carter Harrison on 4/9/19. | ||
// | ||
|
||
import Foundation | ||
import XyBleSdk | ||
import CoreBluetooth | ||
|
||
public enum XyoAndroidXService : XYServiceCharacteristic { | ||
case mutex | ||
case connect | ||
case status | ||
case scan | ||
case ip | ||
case ssid | ||
|
||
/// The display name of the service. | ||
public var serviceDisplayName: String { | ||
return "Primary" | ||
} | ||
|
||
public var serviceUuid: CBUUID { | ||
switch self { | ||
case .mutex: return CBUUID(string: "c9be9850-7a57-414a-b797-64c230d9ecb9") | ||
case .connect: return CBUUID(string: "00010000-89BD-43C8-9231-40F6E305F96D") | ||
case .status: return CBUUID(string: "00010000-89BD-43C8-9231-40F6E305F96D") | ||
case .scan: return CBUUID(string: "00010000-89BD-43C8-9231-40F6E305F96D") | ||
case .ip: return CBUUID(string: "00010000-89BD-43C8-9231-40F6E305F96D") | ||
case .ssid: return CBUUID(string: "00010000-89BD-43C8-9231-40F6E305F96D") | ||
} | ||
} | ||
|
||
// 00010000-89BD-43C8-9231-40F6E305F96D | ||
|
||
public var characteristicUuid: CBUUID { | ||
switch self { | ||
case .mutex: return CBUUID(string: "e047295e-3df0-47cd-a841-be2d2a97dd21") | ||
case .connect: return CBUUID(string: "00010001-89BD-43C8-9231-40F6E305F96D") | ||
case .status: return CBUUID(string: "00010030-89BD-43C8-9231-40F6E305F96D") | ||
case .scan: return CBUUID(string: "00010040-89BD-43C8-9231-40F6E305F96D") | ||
case .ip: return CBUUID(string: "00010020-89BD-43C8-9231-40F6E305F96D") | ||
case .ssid: return CBUUID(string: "00010010-89BD-43C8-9231-40F6E305F96D") | ||
} | ||
} | ||
|
||
public var characteristicType: XYServiceCharacteristicType { return XYServiceCharacteristicType.byte } | ||
|
||
public var displayName: String { | ||
switch self { | ||
case .mutex: return "Mutex" | ||
case .connect: return "Connect" | ||
case .status: return "Status" | ||
case .scan: return "Scan" | ||
case .ssid: return "Ssid" | ||
case .ip: return "Ip" | ||
} | ||
} | ||
|
||
public static var values: [XYServiceCharacteristic] = [ XyoAndroidXService.mutex ] | ||
|
||
} | ||
|
Oops, something went wrong.