// withServiceUUIDs:需要搜索的设备`CBUUID`数组,默认为nil,则搜索附近所有的蓝牙设备,浪费时间,所以最好传入`[CBUUIDConvertible]`// timeoutAfter 搜索时长SwiftyBluetooth.scanForPeripherals(withServiceUUIDs:nil,timeoutAfter:15){scanResultinswitchscanResult{case.scanStarted:// The scan started meaning CBCentralManager scanForPeripherals(...) was called case.scanResult(letperipheral,letadvertisementData,letRSSI):// 解析`advertisementData`,找到要连接的设备,然后连接设备 self.peripheral=peripheralcase.scanStopped(leterror):// The scan stopped, an error is passed if the scan stopped unexpectedly}}
peripheral.discoverServices(withUUIDs:nil){resultinswitchresult{case.success(letservices):break// An array containing all the services requestedcase.failure(leterror):break// A connection error or an array containing the UUIDs of the services that we're not found}}peripheral.discoverCharacteristics(withUUIDs:nil,ofServiceWithUUID:"180A"){resultin// The characteristics discovered or an error if something went wrong.switchresult{case.success(letservices):break// An array containing all the characs requested.case.failure(leterror):break// A connection error or an array containing the UUIDs of the charac/services that we're not found.}}