0

BLE デバイスで何かが起こったときに通知を受け取りたい。BLE デバイスが何らかのデータ/コマンドをアプリに渡す場合、アプリ内の AdvertisingData は変更されません。しかし、アンドロイドでできることと同じことが完璧に機能しています。広告データが変更されたときのような機能を実装したい通知を取得したい。これを実装するのを手伝ってください。以下は私の AppDelegate.swift クラスです。

private var centralManager : CBCentralManager!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)

    return true
}

func applicationWillEnterForeground(_ application: UIApplication) {

    print("entering foreground...")
}

func applicationDidEnterBackground(_ application: UIApplication) {
    print("entered background...")

    centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)
}


func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state == .poweredOn {
        print("Bluetooth is On")

        let kTrackStandardDeviceInfoServiceUUID  = CBUUID(string: "180A")

        let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : true]
        let arrayOfServices: [CBUUID] = [kTrackStandardDeviceInfoServiceUUID]

        centralManager.scanForPeripherals(withServices: arrayOfServices, options: dictionaryOfOptions)
    } else {
        print(central.state)
    }
}

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

    print("\nName   : \(peripheral.name ?? "(No name)")")
    print("RSSI   : \(RSSI)")

    let name = peripheral.name ?? ""
    if name.contains("ETGuardian") {

        let DetailData = advertisementData["kCBAdvDataManufacturerData"]
        let DiscoveredData = String(describing: DetailData)

        print(DiscoveredData)

        for ad in advertisementData {
            print("AD Data: \(ad)")
        }
    }
}
4

1 に答える 1