2017-05-12 29 views
0

Core Bluetoothに問題があり、特定のCBUUIDをスキャン中に周辺機器を見つけられず、アプリケーションをフォアグラウンドとバックグラウンドで実行したいしかし、それは、CBUUID "manager.scanForPeripherals(withServices:nil、options:nil)"を使用して周辺のスキャンをすべて行ったことです。背景に取り組んでいますか? "私はbluetooth-centralを有効にしました"と私はバックグラウンドでやりたいことがありますか?Core Bluetoothが特定のCBUUIDをスキャン中に周辺機器(フォアグラウンドとバックグラウンド)を検出できない

var manager:CBCentralManager! 
var peripheralCB:CBPeripheral! 
var peripherals = [CBPeripheral]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

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

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
    func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) { 
      if let peripheralsObject = dict[CBCentralManagerRestoredStatePeripheralsKey] { 
       // 2 
       let peripherals = peripheralsObject as! Array<CBPeripheral> 
       // 3 
       if peripherals.count > 0 { 
        // 4 
        peripheralCB = peripherals[0] 
        // 5 
        peripheralCB?.delegate = self 
       } 
      } 
     } 

func centralManagerDidUpdateState(_ central: CBCentralManager) { 
    var consolMessages = "" 

    switch central.state 
    { 
    case .poweredOff: 
     consolMessages = "BLE is powered off" 

    case.poweredOn: 
     consolMessages = "BLE is powered on" 
     let serviceUUIDs = [CBUUID(string: "B0702880-A295-A8AB-F734-031A98A512DE") as AnyObject] 
     let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : false] 
     manager.scanForPeripherals(withServices: serviceUUIDs as? [CBUUID], options:dictionaryOfOptions) 
     // manager.scanForPeripherals(withServices: nil, options:nil) 

    case.resetting: 
     consolMessages = "BLE is resetting" 

    case.unauthorized: 
     consolMessages = "BLE is unauthorized" 

    case.unknown: 
     consolMessages = "BLE is unknown" 

    case.unsupported: 
     consolMessages = "unsupported" 

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

    print("peripheral \(peripheral)") 
    print("peripheral Name \(peripheral.name)") 

    if #available(iOS 9.0, *) { 
      peripheral.accessibilityAssistiveTechnologyFocusedIdentifiers() 
    } else { 
     // Fallback on earlier versions 
    } 
    print("peripheral Name \(peripheral.name)") 
    peripherals.append(peripheral) 
    manager.connect(peripheral, options: nil) 

    let AdvertsatingData = advertisementData[CBAdvertisementDataManufacturerDataKey] 

    print("AdvertsatingData\(AdvertsatingData)") 


} 

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 

    print("peripheral Connected") 
    print("peripheral didConnect \(peripheral)") 
    print("Connected peripheral Name \(peripheral.name)") 


} 

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { 

    let alert = UIAlertController(title: "Alert", message: "didFailToConnect", preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 
    self.present(alert, animated: true, completion: nil) 
} 

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { 

    print("peripheral Disconnectd") 

    print("Disconnect peripheral Name \(peripheral.name)")  
} 
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 

    if error != nil{ 

    } 
    else { 
     print("didDiscoverCharacteristicsFor") 

    } 
}[![This is the Beacon Simulator https://i.stack.imgur.com/diyWA.png ][1]][1] 

答えて

0

これが動作しないいくつかの理由:

  • MactsAsBeaconは、BLEメーカー広告ですiBeacon広告を送り出すとBluetoothサービスのUUIDを持っていないではありません。 iBeacon ProximityUUIDとCoreBluetooth Bluetoothでのスキャンに使用するサービスUUIDを混同しないでください。彼らは根本的に異なるものです。

  • サービスUUIDが提供されていない限り、CoreBluetoothはバックグラウンドコールバックを取得しません。それはAppleが課した制限です。

  • CoreBluetoothは、フォアグラウンドでも、iBeaconパケットのデータからビーコン識別子を読み取ることができません。 Appleは、iOSで受信したiBeaconパケットについてこのデータをクリアします。

iOSでビーコンパケットを読み取るには、CoreLocation APIを使用する必要があります。他の方法はありません。

オフショアのiOSビーコン検出器を使用して、MactsAsBeaconからの送信を確実に検出できるように注意してください。 MacプログラムはMacOSのいくつかのフレーバーでは送信しません。

+0

返信いただきありがとうございますdavidgyoung。私は既に、CoreLocationでうまく動作しています。 CoreLocationでは "advertisingData"は使用できません。なぜ "CoreBluetooth"スキャンペリフェラルを使用して特定のUUIDがフォアグラウンドで動作していないのですか。あなたのコードで何が間違っているのか、何を追加したいのか説明してください。 –

+0

この場合、私はあなたがadvertisingDataから得ようとしていることを理解していません。 iBeacon広告の場合、データにはproximityUUID、メジャーおよびマイナーが含まれ、これらのすべてがCoreLocationによって返されるCLBeaconオブジェクトに存在します。広告データからさらに何を得ることを期待していますか? CoreBluetoothはiBeaconパケットのadvertisingDataへのアクセスをブロックするため、何でも、取得できません。 – davidgyoung

関連する問題