Skip to content

Commit

Permalink
#1 added battery level state
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos21 committed Jul 2, 2018
1 parent d32cc42 commit 8474eaa
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 80 deletions.
14 changes: 11 additions & 3 deletions BluetoothExplorer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
35298F1A20EACCB10093B830 /* BatteryPowerStateCharacteristic.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 35298F1920EACCB10093B830 /* BatteryPowerStateCharacteristic.storyboard */; };
35298F2620EACD700093B830 /* BatteryPowerStateCharacteristicViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35298F2520EACD700093B830 /* BatteryPowerStateCharacteristicViewController.swift */; };
35317A2E20E53BD500D31493 /* HardwareRevisionStringCharacteristic.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 35317A2D20E53BD500D31493 /* HardwareRevisionStringCharacteristic.storyboard */; };
35317A3720E53C2C00D31493 /* HardwareRevisionStringCharacteristicViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35317A3620E53C2C00D31493 /* HardwareRevisionStringCharacteristicViewController.swift */; };
35317A3920E5485400D31493 /* SerialNumberStringCharacteristic.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 35317A3820E5485400D31493 /* SerialNumberStringCharacteristic.storyboard */; };
Expand Down Expand Up @@ -233,6 +235,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
35298F1920EACCB10093B830 /* BatteryPowerStateCharacteristic.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = BatteryPowerStateCharacteristic.storyboard; sourceTree = "<group>"; };
35298F2520EACD700093B830 /* BatteryPowerStateCharacteristicViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryPowerStateCharacteristicViewController.swift; sourceTree = "<group>"; };
35317A2D20E53BD500D31493 /* HardwareRevisionStringCharacteristic.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = HardwareRevisionStringCharacteristic.storyboard; sourceTree = "<group>"; };
35317A3620E53C2C00D31493 /* HardwareRevisionStringCharacteristicViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HardwareRevisionStringCharacteristicViewController.swift; sourceTree = "<group>"; };
35317A3820E5485400D31493 /* SerialNumberStringCharacteristic.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = SerialNumberStringCharacteristic.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -375,6 +379,7 @@
6E98B69E2059B51000B6F016 /* LaunchScreen.storyboard */,
6E98B6992059B51000B6F016 /* Main.storyboard */,
6EDE00CE20DB12B3002E951A /* BatteryLevelCharacteristic.storyboard */,
35298F1920EACCB10093B830 /* BatteryPowerStateCharacteristic.storyboard */,
35335A0420E1316F006ABD4D /* ModelNumberCharacteristic.storyboard */,
35335A0A20E13FC0006ABD4D /* FirmwareRevisionStringCharacteristic.storyboard */,
35335A1520E14353006ABD4D /* SoftwareRevisionStringCharacteristic.storyboard */,
Expand Down Expand Up @@ -484,6 +489,7 @@
35317A3620E53C2C00D31493 /* HardwareRevisionStringCharacteristicViewController.swift */,
35317A3A20E5486200D31493 /* SerialNumberStringCharacteristicViewController.swift */,
35317A4720E56CCA00D31493 /* AlertCategoryCharacteristicViewController.swift */,
35298F2520EACD700093B830 /* BatteryPowerStateCharacteristicViewController.swift */,
);
path = "GATT Characteristic Editors";
sourceTree = "<group>";
Expand Down Expand Up @@ -593,7 +599,7 @@
TargetAttributes = {
6E98B6922059B51000B6F016 = {
CreatedOnToolsVersion = 9.2;
DevelopmentTeam = 4W79SG34MW;
DevelopmentTeam = RH4PC25J9C;
ProvisioningStyle = Automatic;
SystemCapabilities = {
com.apple.BackgroundModes = {
Expand Down Expand Up @@ -734,6 +740,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
35298F1A20EACCB10093B830 /* BatteryPowerStateCharacteristic.storyboard in Resources */,
35CA5E1520E579B100F6B589 /* Localizable.strings in Resources */,
6EDE00CF20DB12B3002E951A /* BatteryLevelCharacteristic.storyboard in Resources */,
6E98B6A02059B51000B6F016 /* LaunchScreen.storyboard in Resources */,
Expand Down Expand Up @@ -811,6 +818,7 @@
35335A1420E13FF0006ABD4D /* FirmwareRevisionStringCharacteristicViewController.swift in Sources */,
6E8C68EA20DA02DA00232169 /* UIAlertAction.swift in Sources */,
35335A0720E13203006ABD4D /* ModelNumberCharacteristicViewController.swift in Sources */,
35298F2620EACD700093B830 /* BatteryPowerStateCharacteristicViewController.swift in Sources */,
6EC0D9C720DE076A005FB128 /* Appearance.swift in Sources */,
6EFB3E9720D3F0D700364CA0 /* CentralManagedObject.swift in Sources */,
6EFD558420D83C76003B75A1 /* CharacteristicManagedObject.swift in Sources */,
Expand Down Expand Up @@ -920,7 +928,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 4W79SG34MW;
DEVELOPMENT_TEAM = RH4PC25J9C;
FRAMEWORK_SEARCH_PATHS = (
$SRCROOT/../Carthage/Build/iOS,
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand Down Expand Up @@ -948,7 +956,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 4W79SG34MW;
DEVELOPMENT_TEAM = RH4PC25J9C;
FRAMEWORK_SEARCH_PATHS = (
$SRCROOT/../Carthage/Build/iOS,
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand Down
31 changes: 31 additions & 0 deletions BluetoothExplorer/BatteryPowerStateCharacteristic.storyboard
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>
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
}
}
}
}
Loading

0 comments on commit 8474eaa

Please sign in to comment.