How to distinguish peripherals

I'm just starting out playing with Bluetooth. I have been able to identify my iMac using


func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

print("Peripheral: \(peripheral)")

}


My Bluetooth mouse and keyboard are listed in the preferences as being discoverable as "my iMac". How, though, do I distinguish what is my iMac, mouse and keyboard? I see three unique identifiers, which should be them, but how do I tell which is which?


Peripheral: <CBPeripheral: 0x282fb8000, identifier = 71F3009D-565A-451D-B8BD-C0B6543487BC, name = Jonathan’s iMac, state = disconnected>

Peripheral: <CBPeripheral: 0x282fa4000, identifier = 71F3009D-565A-451D-B8BD-C0B6543487BC, name = Jonathan’s iMac, state = disconnected>

Peripheral: <CBPeripheral: 0x282fa4000, identifier = 71F3009D-565A-451D-B8BD-C0B6543487BC, name = Jonathan’s iMac, state = disconnected>

Peripheral: <CBPeripheral: 0x282fa01e0, identifier = 71F3009D-565A-451D-B8BD-C0B6543487BC, name = Jonathan’s iMac, state = disconnected>

Replies

Did you try:

func retrievePeripherals(withIdentifiers identifiers: [UUID]) -> [CBPeripheral]


for each CBPeripheral ask its name


let uuid = UUID("71F3009D-565A-451D-B8BD-C0B6543487BC")
let cb = retrievePeripherals(withIdentifiers identifiers: [uuid])[0]
let name = cb.name


Repeat with all identifiers

Is there anything special abut the UUID that you used above? Also, doesn't printing out the variable peripheral give me the name anyway? It still does not distinguish between my mouse and keyboard.

what do you get when you print name ?

I'm still learning Swift. The code below does not allow me to get past assigning "cb"

I am not sure if I am putting the retrievePeripherals in the correct place. Can you help me?




import UIKit

import CoreBluetooth


class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {


var manager:CBCentralManager!

var cb:CBPeripheral!



override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

manager = CBCentralManager(delegate: self, queue: nil)

let uuid = UUID(uuidString:"71F3009D-565A-451D-B8BD-C0B6543487BC")

let cb = manager.retrievePeripherals(withIdentifiers: uuid) //gives error: Cannot convert value of type 'UUID?' to expected argument type '[UUID]'

}


func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

print("Peripheral: \(peripheral) and strength: \(RSSI)")

}




func centralManagerDidUpdateState(_ central: CBCentralManager) {

var consoleMsg = ""

switch (central.state) {

case .poweredOn:

consoleMsg = "BLE is On"

manager.scanForPeripherals(withServices: nil, options: nil)

case .poweredOff:

consoleMsg = "BLE is Off"

case .resetting:

consoleMsg = "BLE is resetting"

case .unauthorized:

consoleMsg = "BLE is unauthorized"

case .unknown:

consoleMsg = "BLE is unknow"

case .unsupported:

consoleMsg = "BLE is unsupported"

}

print ("\(consoleMsg)")

}


override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

}

I don't know if that will be enough, but you have several errors.


class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
 
    var manager:CBCentralManager!
    var cb:CBPeripheral!
 
 
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        manager = CBCentralManager(delegate: self,  queue:  nil)
     
        let uuid = UUID(uuidString:"71F3009D-565A-451D-B8BD-C0B6543487BC")
        let cb = manager.retrievePeripherals(withIdentifiers: uuid)      //gives error: Cannot convert value of type 'UUID?' to expected argument type '[UUID]'
    }


Line 13: you redeclare a cb with the let statement. Doing so, the cb defined as var will not be changed, remain nil, and you will crash later.

Line 13: what is expected is an array as [uuid]

Lines 3 and 4: do not use unwrap optionals, it is risky if ever they become nil later.

Line 1: you should give a more explicit name to your class


So try with those changes and report:

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
 
    var manager:CBCentralManager?
    var cb:CBPeripheral?
 
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        manager = CBCentralManager(delegate: self,  queue:  nil)
     
        let uuid = UUID(uuidString:"71F3009D-565A-451D-B8BD-C0B6543487BC")
        cb = manager.retrievePeripherals(withIdentifiers: [uuid])      //gives error: Cannot convert value of type 'UUID?' to expected argument type '[UUID]'
    }

It prints the name "Jonathan's iMac" which is what was printed when I dumped the value of "peripheral" to the debugger. My array only has one peripheral. I am guessing that the iMac takes control of the mouse and keyboard and considers them all part of "Jonathan's iMac"


func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

print("Peripheral: \(peripheral)")

}

Peripheral: <CBPeripheral: 0x282fb8000, identifier = 71F3009D-565A-451D-B8BD-C0B6543487BC, name = Jonathan’s iMac, state = disconnected>