Skip to content

Instantly share code, notes, and snippets.

@devxoul
Created February 13, 2018 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devxoul/1171d0172142b41599371e52cfdc7fd3 to your computer and use it in GitHub Desktop.
Save devxoul/1171d0172142b41599371e52cfdc7fd3 to your computer and use it in GitHub Desktop.
import CoreBluetooth
let serviceUUID = CBUUID(string: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
let service = CBMutableService(type: serviceUUID, primary: true)
/// 1. `CBCentralManager`를 초기화하고,
self.central = CBCentralManager(delegate: self, queue: nil)
/// 2. 사용가능한 상태가 되면 특정 UUID를 가진 서비스를 스캔합니다.
func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == .PoweredOn {
// 이미 한 번 스캔된 정보라도 계속 스캔합니다.
let options = [CBCentralManagerScanOptionAllowDuplicatesKey: true]
self.central.scanForPeripheralsWithServices([serviceUUID], options: options)
}
}
/// 3. Peripheral이 스캔되면 이 메서드가 호출됩니다.
func centralManager(central: CBCentralManager,
didDiscoverPeripheral peripheral: CBPeripheral,
advertisementData: [String : AnyObject],
RSSI: NSNumber) {
print(advertisementData[CBAdvertisementDataLocalNameKey]) // "전수열"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment