Skip to content

Commit

Permalink
Add CharacteristicNotifyViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Piechocki committed Nov 2, 2020
1 parent 653b992 commit 853e50e
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 10 deletions.
16 changes: 16 additions & 0 deletions ExampleApp/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
8D117DB3254FD1DE003807B3 /* CharacteristicNotifyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D117DB2254FD1DE003807B3 /* CharacteristicNotifyViewController.swift */; };
8D117DB7254FD20D003807B3 /* CharacteristicNotifyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D117DB6254FD20D003807B3 /* CharacteristicNotifyView.swift */; };
8D4839A2254AAC0900266106 /* CharacteristicsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D4839A1254AAC0900266106 /* CharacteristicsViewController.swift */; };
8D61CC8B254C321200952B2B /* CharacteristicInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D61CC8A254C321200952B2B /* CharacteristicInfo.swift */; };
8D61CC92254C479900952B2B /* PeripheralViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D61CC91254C479900952B2B /* PeripheralViewController.swift */; };
Expand Down Expand Up @@ -39,6 +41,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
8D117DB2254FD1DE003807B3 /* CharacteristicNotifyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacteristicNotifyViewController.swift; sourceTree = "<group>"; };
8D117DB6254FD20D003807B3 /* CharacteristicNotifyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacteristicNotifyView.swift; sourceTree = "<group>"; };
8D4839A1254AAC0900266106 /* CharacteristicsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacteristicsViewController.swift; sourceTree = "<group>"; };
8D61CC8A254C321200952B2B /* CharacteristicInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacteristicInfo.swift; sourceTree = "<group>"; };
8D61CC91254C479900952B2B /* PeripheralViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeripheralViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -84,6 +88,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
8D117DB1254FD1C5003807B3 /* CharacteristicNotify */ = {
isa = PBXGroup;
children = (
8D117DB2254FD1DE003807B3 /* CharacteristicNotifyViewController.swift */,
8D117DB6254FD20D003807B3 /* CharacteristicNotifyView.swift */,
);
path = CharacteristicNotify;
sourceTree = "<group>";
};
8D4839A0254AABDC00266106 /* CentralServiceChacacteristics */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -144,6 +157,7 @@
8D72C5832539C34400456D1A /* Screens */ = {
isa = PBXGroup;
children = (
8D117DB1254FD1C5003807B3 /* CharacteristicNotify */,
8D61CCA3254C553500952B2B /* CharacteristicRead */,
8D61CC98254C4A6800952B2B /* PeripheralRead */,
8D896D502542FFA900FD5FE5 /* Central */,
Expand Down Expand Up @@ -334,9 +348,11 @@
files = (
8D61CC92254C479900952B2B /* PeripheralViewController.swift in Sources */,
8DF924C3254AE4F40027627D /* Error+Printable.swift in Sources */,
8D117DB7254FD20D003807B3 /* CharacteristicNotifyView.swift in Sources */,
8D896D552542FFE000FD5FE5 /* CentralView.swift in Sources */,
8D61CC8B254C321200952B2B /* CharacteristicInfo.swift in Sources */,
8D4839A2254AAC0900266106 /* CharacteristicsViewController.swift in Sources */,
8D117DB3254FD1DE003807B3 /* CharacteristicNotifyViewController.swift in Sources */,
8D61CC9E254C4AA000952B2B /* PeripheralReadView.swift in Sources */,
8D61CC96254C47BF00952B2B /* PeripheralView.swift in Sources */,
8D61CCA5254C554F00952B2B /* CharacteristicReadViewController.swift in Sources */,
Expand Down
17 changes: 15 additions & 2 deletions ExampleApp/ExampleApp/BluetoothProvider/BluetoothProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ class BluetoothProvider {
}

func readValue(for characteristic: Characteristic) -> Observable<String> {
return characteristic.readValue().asObservable()
.map { $0.value.flatMap { String(data: $0, encoding: .utf8) } ?? "-" }
characteristic.readValue().asObservable()
.map { $0.stringValue }
}

func getValueUpdates(for characteristic: Characteristic) -> Observable<String> {
characteristic.observeValueUpdateAndSetNotification()
.map { $0.stringValue }
}

func shutDown() {
Expand All @@ -50,3 +55,11 @@ class BluetoothProvider {
private let scannedPeripheralSubject = PublishSubject<ScannedPeripheral>()

}

private extension Characteristic {

var stringValue: String {
self.value.flatMap { String(data: $0, encoding: .utf8) } ?? "-"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CharacteristicCell: UITableViewCell {
override func prepareForReuse() {
identifierLabel.text = nil
readButton.tag = 0
updateButton.tag = 0
notifyButton.tag = 0
writeButton.tag = 0
}

Expand All @@ -32,9 +32,9 @@ class CharacteristicCell: UITableViewCell {
return button
}()

let updateButton: UIButton = {
let notifyButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Update", for: .normal)
button.setTitle("Notify", for: .normal)
return button
}()

Expand All @@ -56,7 +56,7 @@ class CharacteristicCell: UITableViewCell {
private func setupLayout() {
stackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(stackView)
[identifierLabel, readButton, updateButton, writeButton].forEach(stackView.addArrangedSubview)
[identifierLabel, readButton, notifyButton, writeButton].forEach(stackView.addArrangedSubview)

NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class CharacteristicsViewController: UITableViewController {

let characterstic = characteristics[indexPath.row]
cell.identifierLabel.text = characterstic.uuid.uuidString
[cell.readButton, cell.updateButton, cell.writeButton].forEach { $0.tag = indexPath.row }
[cell.readButton, cell.notifyButton, cell.writeButton].forEach { $0.tag = indexPath.row }
cell.readButton.addTarget(self, action: #selector(handleReadButton), for: .touchUpInside)
cell.updateButton.addTarget(self, action: #selector(handleUpdateButton), for: .touchUpInside)
cell.notifyButton.addTarget(self, action: #selector(handleNotifyButton), for: .touchUpInside)
cell.writeButton.addTarget(self, action: #selector(handleWriteButton), for: .touchUpInside)
return cell
}
Expand All @@ -64,8 +64,13 @@ class CharacteristicsViewController: UITableViewController {
navigationController?.pushViewController(controller, animated: true)
}

@objc private func handleUpdateButton(_ sender: UIButton) {
print("update \(sender.tag)")
@objc private func handleNotifyButton(_ sender: UIButton) {
let characteristic = characteristics[sender.tag]
let controller = CharacteristicNotifyViewController(
characteristic: characteristic,
bluetoothProvider: bluetoothProvider
)
navigationController?.pushViewController(controller, animated: true)
}

@objc private func handleWriteButton(_ sender: UIButton) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import UIKit

class CharacteristicNotifyView: UIView {

init() {
super.init(frame: .zero)
backgroundColor = .systemBackground
setupLayout()
}

required init?(coder: NSCoder) { nil }

// MARK: - Subviews

let label: UILabel = {
let label = UILabel()
label.text = "Updated value: --"
label.font = UIFont.systemFont(ofSize: 20.0)
label.textColor = .green
return label
}()

// MARK: - Private

private func setupLayout() {
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)

NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: centerXAnchor),
label.centerYAnchor.constraint(equalTo: centerYAnchor),
label.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, constant: -32)
])
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import RxBluetoothKit
import RxSwift
import UIKit

class CharacteristicNotifyViewController: UIViewController {

init(characteristic: Characteristic, bluetoothProvider: BluetoothProvider) {
self.characteristic = characteristic
self.bluetoothProvider = bluetoothProvider
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) { nil }

// MARK: - View

private(set) lazy var characteristicNotifyView = CharacteristicNotifyView()

override func loadView() {
view = characteristicNotifyView
}

override func viewDidLoad() {
super.viewDidLoad()
setupBindings()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

viewDidAppearSubject.onNext(())
}

// MARK: - Private

private let characteristic: Characteristic
private let bluetoothProvider: BluetoothProvider
private let viewDidAppearSubject = PublishSubject<Void>()
private let disposeBag = DisposeBag()

private func setupBindings() {
viewDidAppearSubject
.take(1)
.flatMap { [characteristic, bluetoothProvider] in bluetoothProvider.getValueUpdates(for: characteristic) }
.subscribe(
onNext: { [weak self] in self?.characteristicNotifyView.label.text = "Updated value: " + $0 },
onError: { [weak self] in AlertPresenter.presentError(with: $0.printable, on: self?.navigationController) }
)
.disposed(by: disposeBag)
}

}

0 comments on commit 853e50e

Please sign in to comment.