-
Notifications
You must be signed in to change notification settings - Fork 1
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
5 changed files
with
261 additions
and
80 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
31 changes: 31 additions & 0 deletions
31
BluetoothExplorer/BatteryPowerStateCharacteristic.storyboard
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,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES"> | ||
<device id="retina4_7" orientation="portrait"> | ||
<adaptation id="fullscreen"/> | ||
</device> | ||
<dependencies> | ||
<deployment identifier="iOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<scenes> | ||
<!--PnP ID--> | ||
<scene sceneID="gRR-mt-nIb"> | ||
<objects> | ||
<tableViewController storyboardIdentifier="PnPIDCharacteristicViewController" title="PnP ID" id="f6t-8S-6TY" customClass="PnPIDCharacteristicViewController" customModule="BluetoothExplorer" customModuleProvider="target" sceneMemberID="viewController"> | ||
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="Tum-PO-yQQ"> | ||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/> | ||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | ||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> | ||
<connections> | ||
<outlet property="dataSource" destination="f6t-8S-6TY" id="AzQ-c1-QdC"/> | ||
<outlet property="delegate" destination="f6t-8S-6TY" id="Umy-aq-6E7"/> | ||
</connections> | ||
</tableView> | ||
</tableViewController> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="Xog-ji-duU" userLabel="First Responder" sceneMemberID="firstResponder"/> | ||
</objects> | ||
<point key="canvasLocation" x="206" y="14"/> | ||
</scene> | ||
</scenes> | ||
</document> |
168 changes: 168 additions & 0 deletions
168
...ontroller/GATT Characteristic Editors/BatteryPowerStateCharacteristicViewController.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,168 @@ | ||
// | ||
// BatteryPowerStateCharacteristicViewController.swift | ||
// BluetoothExplorer | ||
// | ||
// Created by Carlos Duclos on 7/2/18. | ||
// Copyright © 2018 PureSwift. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
import UIKit | ||
import Bluetooth | ||
|
||
final class PnPIDCharacteristicViewController: UITableViewController, CharacteristicViewController, InstantiableViewController { | ||
|
||
typealias VendorIDSource = GATTPnPID.VendorIDSource | ||
|
||
// MARK: - Properties | ||
|
||
private let cellIdentifier = "InputTextViewCell" | ||
|
||
private var fields = [Field]() | ||
|
||
var value = GATTPnPID(vendorIdSource: .fromAssignedNumbersDocument, vendorId: 0, productId: 0, productVersion: 0) | ||
|
||
var valueDidChange: ((GATTPnPID) -> ())? | ||
|
||
// MARK: - Loading | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
fields = [.vendorIdSource("\(value.vendorIdSource)"), | ||
.vendorId("\(value.vendorId)"), | ||
.productId("\(value.productId)"), | ||
.productVersionId("\(value.productVersion)"),] | ||
|
||
tableView.register(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier) | ||
tableView.separatorStyle = .none | ||
} | ||
|
||
// MARK: - UITableViewController | ||
|
||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return fields.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
|
||
let field = fields[indexPath.row] | ||
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! InputTextViewCell | ||
cell.selectionStyle = .none | ||
|
||
configure(cell, field: field) | ||
|
||
return cell | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
|
||
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! InputTextViewCell | ||
cell.inputTextView.textField.becomeFirstResponder() | ||
} | ||
|
||
// MARK: - Methods | ||
|
||
func configure(_ cell: InputTextViewCell, field: Field) { | ||
|
||
cell.inputTextView.value = field.bluetoothValue | ||
cell.inputTextView.posibleInputValues = field.posibleValues | ||
// cell.inputTextView.isEnabled = valueDidChange != nil | ||
cell.inputTextView.keyboardType = field.keyboardType | ||
cell.inputTextView.fieldLabelText = field.title | ||
cell.inputTextView.placeholder = field.title | ||
cell.inputTextView.validator = { value in | ||
|
||
guard value.trim() != "" | ||
else { return .none } | ||
|
||
switch field { | ||
case .vendorId(let value): | ||
|
||
guard let _ = UInt16(value) | ||
else { return .error("Maximum value is 0xFFFF)") } | ||
|
||
case .productId(let value): | ||
|
||
guard let _ = UInt16(value) | ||
else { return .error("Maximum value is 0xFFFF)") } | ||
|
||
case .productVersionId(let value): | ||
|
||
guard let _ = UInt16(value) | ||
else { return .error("Maximum value is 0xFFFF)") } | ||
|
||
default: | ||
break | ||
} | ||
|
||
return .none | ||
} | ||
} | ||
} | ||
|
||
extension PnPIDCharacteristicViewController { | ||
|
||
enum Field { | ||
|
||
case vendorIdSource(String) | ||
case vendorId(String) | ||
case productId(String) | ||
case productVersionId(String) | ||
|
||
var title: String { | ||
|
||
switch self { | ||
case .vendorIdSource: | ||
return "Vendor ID Source" | ||
|
||
case .vendorId: | ||
return "Vendor ID" | ||
|
||
case .productId: | ||
return "Product ID" | ||
|
||
case .productVersionId: | ||
return "Product Version ID" | ||
} | ||
} | ||
|
||
var bluetoothValue: String { | ||
switch self { | ||
case .vendorIdSource(let value): | ||
return value | ||
|
||
case .vendorId(let value): | ||
return value | ||
|
||
case .productId(let value): | ||
return value | ||
|
||
case .productVersionId(let value): | ||
return value | ||
} | ||
} | ||
|
||
var posibleValues: [String] { | ||
switch self { | ||
case .vendorIdSource: | ||
return [VendorIDSource.fromAssignedNumbersDocument.rawValue.description, | ||
VendorIDSource.fromVendorIDValue.rawValue.description] | ||
|
||
default: | ||
return [] | ||
} | ||
} | ||
|
||
var keyboardType: UIKeyboardType { | ||
switch self { | ||
case .vendorIdSource: | ||
return .default | ||
|
||
default: | ||
return.numberPad | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.