Anyone?
The approach I'm taking is
(1) To only scan for devices I need
// https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.asp
let BATTERY_SERVICE_UUID = "180F"
let DEVICE_INFO_SERVICE_UUID = "180A"
let HRM_HEART_RATE_SERVICE_UUID = "180D"
func centralManagerDidUpdateState(central: CBCentralManager) {
switch (central.state) {
case .Unsupported: print("CoreBluetooth BLE hardware is unsupported on this platform")
case .Unauthorized: print("CoreBluetooth BLE state is unauthorized")
case .Unknown: print("CoreBluetooth BLE state is unknown")
case .Resetting: print("CoreBluetooth BLE is resetting")
case .PoweredOff: print("CoreBluetooth BLE hardware is powered off")
case .PoweredOn:
print("CoreBluetooth BLE hardware is powered on and ready")
print("Scanning perifierals for services that I want")
let services = [CBUUID(string: BATTERY_SERVICE_UUID),
CBUUID(string: DEVICE_INFO_SERVICE_UUID),
CBUUID(string: HRM_HEART_RATE_SERVICE_UUID)]
central.scanForPeripheralsWithServices(services, options: nil)
}
}
(2) Then find the first valid device that contains a local name, stop scanning and use it
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
// Check to make sure that the device has a non-empty local name //
if let localName = advertisementData[CBAdvertisementDataLocalNameKey] {
print("Found Heart rate Monitor: \(localName)")
self.centralManager.stopScan() // Stop Scanning
self.hrmPeripheral = peripheral; // Store the peripheral
peripheral.delegate = self // Add the delegate
self.centralManager.connectPeripheral(peripheral, options: nil) // Connect to the peripheral
}
}
But perhaps I should scan for a few seconds and place all peripherals into an array along with their RSSI. Then after I stop scanning, loop through that array to find the one with the highest RSSI and set that periferial as the hrmPeripheral?
Does anyone do it this way?