之前介紹過如何搜尋藍芽裝置 (請參考搜尋藍芽裝置)
接下來我們使用 Dictionary 來記錄搜尋到的裝置
Step 1: 新增Class BtDevice 來存放裝置資訊
BtDevice.swift1 2 3 4 5 6
| import Foundation
struct BtDevice { var uuid: String!; var name: String?; }
|
Step 2: Create Dictionary
使用 Bluetooth UUID 當作 Key
1 2
| var mFoundDevices : Dictionary<String , BtDevice>!; mFoundDevices = [String : BtDevice]();
|
Step 3: 將發現的裝置存放在Dictionary
1 2 3 4 5 6 7 8
| func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { print("Device: \(peripheral.name) : \(peripheral.identifier.UUIDString)"); let btUuid = peripheral.identifier.UUIDString; let btName = peripheral.name let btDevice = BtDevice(uuid: btUuid, name: btName); mFoundDevices[btUuid] = btDevice; }
|
Step 4: 過濾已發現的裝置
由於搜尋藍芽裝置時,可能會搜尋到重複的裝置,所以我們需要先過濾此裝置是否已被搜尋過. 若是還未搜尋到的裝置再加入Dictionary中.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { print("Device: \(peripheral.name) : \(peripheral.identifier.UUIDString)"); if (!isAlreadyFound(peripheral.identifier.UUIDString)) { insertFoundDevice(peripheral.identifier.UUIDString, newName: peripheral.name); } else { print("This Device is discovered: \(peripheral.identifier.UUIDString)"); } }
internal func isAlreadyFound(uuid : String!) -> Bool { return mFoundDevices[uuid] != nil; }
internal func insertFoundDevice(newUuid: String!, newName: String?) { let btDevice = BtDevice(uuid: newUuid, name: newName); mFoundDevices[newUuid] = btDevice; }
|
Step 5: 清空 Dictionary
在搜尋前, 我們也必須先清空搜尋過的資料.
1
| mFoundDevices.removeAll();
|
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import Foundation import CoreBluetooth
class BtManager : NSObject { var mCBCentralManager: CBCentralManager! var mFoundDevices : Dictionary<String , BtDevice>!; override init() { super.init(); initCBCentralManager(); mFoundDevices = [String : BtDevice](); }
internal func initCBCentralManager() { mCBCentralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: true]); }
func isScanning() -> Bool { return mCBCentralManager.isScanning; }
func startToScan() { if (mCBCentralManager.state == CBCentralManagerState.PoweredOff) { print("state is off"); initCBCentralManager(); } else { if (isScanning()) { print("state is scanning"); } else { clearAllFoundDevices(); mCBCentralManager.scanForPeripheralsWithServices(nil, options: nil); print("start to scan: \(isScanning())"); } } }
func stopScanning() { mCBCentralManager.stopScan(); } }
extension BtManager : CBCentralManagerDelegate { func centralManagerDidUpdateState(central: CBCentralManager) { switch (central.state) { case CBCentralManagerState.PoweredOn: print("state On"); case CBCentralManagerState.PoweredOff: print("state Off"); case CBCentralManagerState.Unknown: fallthrough; default: print("state Unknow"); } }
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { print("Device: \(peripheral.name) : \(peripheral.identifier.UUIDString)");
if (!isAlreadyFound(peripheral.identifier.UUIDString)) { insertFoundDevice(peripheral.identifier.UUIDString, newName: peripheral.name); } else { print("This Device is discovered: \(peripheral.identifier.UUIDString)"); } }
internal func isAlreadyFound(uuid : String!) -> Bool { return mFoundDevices[uuid] != nil; } internal func insertFoundDevice(newUuid: String!, newName: String?) { let btDevice = BtDevice(uuid: newUuid, name: newName); mFoundDevices[newUuid] = btDevice; }
internal func clearAllFoundDevices() { mFoundDevices.removeAll(); } }
|