Commit 00f69d53 authored by Thomas Mellenthin's avatar Thomas Mellenthin

Show advertisement name, as the FW announces now the mac address

parent 62fa6907
......@@ -9,8 +9,8 @@
import CoreBluetooth
protocol BluetoothManagerDelegate: class {
func didFindNewPeripherals(_ peripherals: Array<CBPeripheral>?)
func didConnectToPeripheal(_ peripheral: CBPeripheral?)
func didFindNewPeripherals(_ peripherals: Array<FoundPeripheral>?)
func didConnectToPeripheal(_ peripheral: FoundPeripheral?)
func didDisconnectFromPeripheral()
func didGetNewLightSensorData(_ data: UInt16)
func didFinishToSendFile()
......@@ -20,8 +20,8 @@ protocol BluetoothManagerDelegate: class {
// Provide default empty implementation to avoid empty boilerplate in the delegate
extension BluetoothManagerDelegate {
func didFindNewPeripherals(_ peripherals: Array<CBPeripheral>?) {}
func didConnectToPeripheal(_ peripheral: CBPeripheral?) {}
func didFindNewPeripherals(_ peripherals: Array<FoundPeripheral>?) {}
func didConnectToPeripheal(_ peripheral: FoundPeripheral?) {}
func didDisconnectFromPeripheral() {}
func didGetNewLightSensorData(_ data: UInt16) {}
func didFinishToSendFile() {}
......@@ -39,6 +39,12 @@ struct RGBLED {
}
}
struct FoundPeripheral: Hashable {
let peripheral: CBPeripheral
let advertisementName: String?
let rssi: Int?
}
struct WeakDelegate {
private(set) weak var value: BluetoothManagerDelegate?
init (value: BluetoothManagerDelegate) {
......@@ -94,7 +100,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
let card10RXReadCharacteristicUUID = CBUUID(string: "02422342-2342-2342-2342-234223422342")
private(set) public var foundPeripheral: Array<CBPeripheral>
private(set) public var foundPeripheral: Array<FoundPeripheral>
public var connectedPeripheral: CBPeripheral?
/// Holds BluetoothManagerDelegates weakly
......@@ -344,11 +350,12 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
// I failed to scan for the CBUUD 42230100-2342-2342-2342-234223422342 :-/
guard let name = peripheral.name, name.hasPrefix("card10") else { return }
print("did discover: \(name) \(peripheral.identifier)), CBAdvertisementDataLocalNameKey: \(String(describing: advertisementData[CBAdvertisementDataLocalNameKey]))")
let advName = String("\(advertisementData[CBAdvertisementDataLocalNameKey] ?? "")")
print("did discover: \(name) \(peripheral.identifier)), CBAdvertisementDataLocalNameKey: \(advName)")
// sort & unique foundPeripherals
foundPeripheral.append(peripheral)
foundPeripheral = Array(Set(foundPeripheral)).sorted { $0.identifier.uuidString > $1.identifier.uuidString }
foundPeripheral.append(FoundPeripheral.init(peripheral: peripheral, advertisementName: advName, rssi: RSSI.intValue))
foundPeripheral = Array(Set(foundPeripheral)).sorted { $0.peripheral.identifier.uuidString > $1.peripheral.identifier.uuidString }
subscribers.forEach { $0.value?.didFindNewPeripherals(self.foundPeripheral) }
}
......@@ -358,7 +365,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
peripheral.delegate = self
peripheral.discoverServices([card10ServiceUUID, card10RxTxServiceUUID])
self.connectedPeripheral = peripheral
subscribers.forEach { $0.value?.didConnectToPeripheal(peripheral) }
subscribers.forEach { $0.value?.didConnectToPeripheal(FoundPeripheral(peripheral: peripheral, advertisementName: nil, rssi: nil)) }
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
......
......@@ -81,8 +81,8 @@ extension ScannerTableViewController {
case .unpaired:
let cell = tableView.dequeueReusableCell(withIdentifier: "scannerTableViewCell", for: indexPath)
let peripheralForCell = BluetoothManager.sharedInstance().foundPeripheral[indexPath.row]
cell.textLabel?.text = peripheralForCell.name
cell.detailTextLabel?.text = peripheralForCell.identifier.uuidString
cell.textLabel?.text = "\(peripheralForCell.advertisementName ?? "<nil>")"
cell.detailTextLabel?.text = "\(peripheralForCell.peripheral.identifier.uuidString) - RSSI: \(peripheralForCell.rssi ?? 0)"
return cell
case .paired:
let cell = tableView.dequeueReusableCell(withIdentifier: "scannerTableViewCell", for: indexPath)
......@@ -104,7 +104,7 @@ extension ScannerTableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// tapping a row -> connecting, in didConnectoToPeripheral we try to force pairing by setting the time
let card10 = BluetoothManager.sharedInstance().foundPeripheral[indexPath.row]
BluetoothManager.sharedInstance().connectToCard10Badge(peripheral: card10)
BluetoothManager.sharedInstance().connectToCard10Badge(peripheral: card10.peripheral)
}
}
......@@ -112,13 +112,13 @@ extension ScannerTableViewController {
extension ScannerTableViewController: BluetoothManagerDelegate {
func didFindNewPeripherals(_ peripherals: Array<CBPeripheral>?) {
func didFindNewPeripherals(_ peripherals: Array<FoundPeripheral>?) {
self.tableView.reloadSections(IndexSet(integer: 0), with: .automatic)
}
func didConnectToPeripheal(_ peripheral: CBPeripheral?) {
func didConnectToPeripheal(_ peripheral: FoundPeripheral?) {
// FIXME: UI that indicated that we're connected
print("ScannerTableViewController: connected to card10 \(peripheral?.identifier.uuidString ?? "<nil>") ")
print("ScannerTableViewController: connected to card10 \(peripheral?.peripheral.identifier.uuidString ?? "<nil>") ")
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment