Created
July 25, 2017 23:39
-
-
Save bricklife/0bd24e82e8a4f539dc9e9da8af0065f4 to your computer and use it in GitHub Desktop.
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
// | |
// BLEDump.swift | |
// BLEDump | |
// | |
// Created by Shinichiro Oba on 2017/07/25. | |
// Copyright © 2017 Shinichiro Oba. All rights reserved. | |
// | |
import Foundation | |
import CoreBluetooth | |
class BLEDump: NSObject { | |
var centralManager: CBCentralManager? | |
var peripherals: [UUID: CBPeripheral] = [:] | |
let serviceUUIDs: [CBUUID]? = nil //[CBUUID(string: "00001523-1212-EFDE-1523-785FEABCD123")] | |
override init() { | |
super.init() | |
centralManager = CBCentralManager(delegate: self, queue: nil) | |
} | |
} | |
extension BLEDump: CBCentralManagerDelegate { | |
func centralManagerDidUpdateState(_ central: CBCentralManager) { | |
switch central.state { | |
case .poweredOn: | |
print("poweredOn") | |
centralManager?.scanForPeripherals(withServices: serviceUUIDs, options: nil) | |
case .poweredOff: | |
print("poweredOff") | |
case .resetting: | |
print("resetting") | |
case .unsupported: | |
print("unsupported") | |
case .unauthorized: | |
print("unauthorized") | |
case .unknown: | |
print("unknown") | |
} | |
} | |
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { | |
print("didDiscover:", peripheral, advertisementData) | |
if peripheral.name != nil, !peripherals.keys.contains(peripheral.identifier) { | |
peripherals[peripheral.identifier] = peripheral | |
centralManager?.connect(peripheral, options: nil) | |
} | |
} | |
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { | |
print("didConnect:", peripheral.identifier) | |
peripheral.delegate = self | |
peripheral.discoverServices(nil) | |
} | |
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { | |
print("didFailToConnect:", error ?? "") | |
peripherals[peripheral.identifier] = nil | |
} | |
} | |
extension BLEDump: CBPeripheralDelegate { | |
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { | |
print("didDiscoverServices:", peripheral.identifier, error ?? "") | |
peripheral.services?.forEach { service in | |
peripheral.discoverCharacteristics(nil, for: service) | |
} | |
} | |
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { | |
print("didDiscoverCharacteristicsFor:", peripheral.identifier, error ?? "") | |
print("\t", service.uuid) | |
service.characteristics?.forEach { characteristic in | |
print("\t\t", characteristic.uuid, characteristic.properties.strings) | |
if characteristic.properties.contains(.read) { | |
peripheral.readValue(for: characteristic) | |
} | |
} | |
} | |
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { | |
print("didUpdateValueFor:", characteristic.uuid, error ?? "") | |
if let value = characteristic.value { | |
print(value) | |
} | |
} | |
} | |
extension CBCharacteristicProperties { | |
var strings: [String] { | |
return [ | |
(.broadcast, "broadcast"), | |
(.read, "read"), | |
(.writeWithoutResponse, "writeWithoutResponse"), | |
(.write, "write"), | |
(.notify, "notify"), | |
(.indicate, "indicate"), | |
(.authenticatedSignedWrites, "authenticatedSignedWrites"), | |
(.extendedProperties, "extendedProperties"), | |
(.notifyEncryptionRequired, "notifyEncryptionRequired"), | |
(.indicateEncryptionRequired, "indicateEncryptionRequired"), | |
] | |
.flatMap { self.contains($0) ? $1 : nil } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment