Skip to content

Commit

Permalink
v0.8.3 - Fix unpairing crash (#202)
Browse files Browse the repository at this point in the history
* Fix unpairing crash, #198

* Bump version to 0.8.3, #198
  • Loading branch information
Jeremy Chiang authored Apr 3, 2019
1 parent 3c8908b commit 1eb3e74
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Bluejay.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |spec|
spec.name = 'Bluejay'
spec.version = '0.8.2'
spec.version = '0.8.3'
spec.license = { type: 'MIT', file: 'LICENSE' }
spec.homepage = 'https://github.com/steamclock/bluejay'
spec.authors = { 'Jeremy Chiang' => '[email protected]' }
spec.summary = 'Bluejay is a simple Swift framework for building reliable Bluetooth apps.'
spec.homepage = 'https://github.com/steamclock/bluejay'
spec.source = { git: 'https://github.com/steamclock/bluejay.git', tag: 'v0.8.2' }
spec.source = { git: 'https://github.com/steamclock/bluejay.git', tag: 'v0.8.3' }
spec.source_files = 'Bluejay/Bluejay/*.{h,swift}'
spec.framework = 'SystemConfiguration'
spec.platform = :ios, '10.0'
Expand Down
2 changes: 1 addition & 1 deletion Bluejay/.jazzy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ output: ../docs
author: Steamclock Software
author_url: http://steamclock.com
module: Bluejay
module_version: 0.8.2
module_version: 0.8.3
readme: ../README.md
sdk: iphone
copyright: Copyright © 2017 Steamclock Software. All rights reserved.
Expand Down
4 changes: 3 additions & 1 deletion Bluejay/Bluejay/Bluejay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,9 @@ extension Bluejay: CBCentralManagerDelegate {
debugLog("Disconnect clean up: delivering expected disconnected event back to the pending connection in the queue...")

if let connection = weakSelf.queue.first as? Connection {
if case let .stopping(error) = connection.state {
if case .running = connection.state {
connectingError = BluejayError.unexpectedDisconnect
} else if case let .stopping(error) = connection.state {
connectingError = error
}
}
Expand Down
2 changes: 1 addition & 1 deletion Bluejay/Bluejay/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.8.2</string>
<string>0.8.3</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
17 changes: 16 additions & 1 deletion Bluejay/BluejayHeartSensorDemo/SensorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ let chirpCharacteristic = CharacteristicIdentifier(
uuid: "83B4A431-A6F1-4540-B3EE-3C14AEF71A04",
service: ServiceIdentifier(uuid: "CED261B7-F120-41C8-9A92-A41DE69CF2A8")
)
let pairingCharacteristic = CharacteristicIdentifier(
uuid: "E4D4A76C-B9F1-422F-8BBA-18508356A145",
service: ServiceIdentifier(uuid: "16274BFE-C539-416C-9646-CA3F991DADD6")
)

class SensorViewController: UITableViewController {

Expand Down Expand Up @@ -55,7 +59,7 @@ class SensorViewController: UITableViewController {
if section == 0 {
return 3
} else {
return 7
return 8
}
}

Expand Down Expand Up @@ -104,6 +108,8 @@ class SensorViewController: UITableViewController {
cell.textLabel?.text = "Stop listening to Dittojay"
} else if indexPath.row == 6 {
cell.textLabel?.text = "Terminate app"
} else if indexPath.row == 7 {
cell.textLabel?.text = "Pair"
}

return cell
Expand Down Expand Up @@ -138,6 +144,15 @@ class SensorViewController: UITableViewController {
endListen(to: chirpCharacteristic)
} else if indexPath.row == 6 {
kill(getpid(), SIGKILL)
} else if indexPath.row == 7 {
bluejay.read(from: pairingCharacteristic) { (result: ReadResult<Data>) in
switch result {
case .success(let data):
bluejay.log("Pairing success: \(String(data: data, encoding: .utf8) ?? "")")
case .failure(let error):
bluejay.log("Pairing failed with error: \(error.localizedDescription)")
}
}
}

tableView.deselectRow(at: indexPath, animated: true)
Expand Down
36 changes: 36 additions & 0 deletions Bluejay/DittojayHeartSensorDemo/DittojayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class DittojayViewController: UITableViewController {
var wakeAppCharacteristic: CBMutableCharacteristic!
var wakeAppService: CBMutableService!

var pairingCharacteristic: CBMutableCharacteristic!
var pairingService: CBMutableService!

var addedServices: [CBService] = []

var heartRate: UInt8 = 0
Expand Down Expand Up @@ -87,6 +90,31 @@ class DittojayViewController: UITableViewController {
advertiseServices([heartRateService.uuid])
}

private func addPairingService() {
let pairingServiceUUID = CBUUID(string: "16274BFE-C539-416C-9646-CA3F991DADD6")
let pairingCharacteristicUUID = CBUUID(string: "E4D4A76C-B9F1-422F-8BBA-18508356A145")

if addedServices.contains(where: { addedService -> Bool in
addedService.uuid == pairingServiceUUID
}) {
return
}

pairingCharacteristic = CBMutableCharacteristic(
type: pairingCharacteristicUUID,
properties: .read,
value: "Steamclock Software".data(using: .utf8),
permissions: .readEncryptionRequired
)

pairingService = CBMutableService(type: pairingServiceUUID, primary: true)
pairingService.characteristics = [pairingCharacteristic]

debugPrint("Will add pairing service...")

manager.add(pairingService)
}

private func advertiseServices(_ services: [CBUUID]) {
manager.startAdvertising([CBAdvertisementDataServiceUUIDsKey: services])
}
Expand Down Expand Up @@ -188,6 +216,7 @@ extension DittojayViewController: CBPeripheralManagerDelegate {
if peripheral.state == .poweredOn {
addHeartRateService()
addWakeAppService()
addPairingService()
}
}

Expand Down Expand Up @@ -234,4 +263,11 @@ extension DittojayViewController: CBPeripheralManagerDelegate {
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {
debugPrint("Did unsubscribe from: \(characteristic.uuid.uuidString)")
}

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
debugPrint("Did receive read request for: \(request.characteristic.uuid.uuidString)")
if request.characteristic.uuid == pairingCharacteristic.uuid {
peripheral.respond(to: request, withResult: .success)
}
}
}

0 comments on commit 1eb3e74

Please sign in to comment.