0

CoreBluetoothをCBPeripheralManagerとして使用してBluetoothアクセサリとしてMacをセットアップしました。 Macは設定された特性CBUUIDで広告を出していて、加入者がいればボタンをクリックしてUTF-8でエンコードされたタイムスタンプを半分ごとにストリーミングする。iOS BluetoothLE:CBCentralManagerの購読を取り消しました

iPhoneは、適切な特性に登録されているCBCentralManagerとして設定されています。デコードされたタイムスタンプでiPhoneのUIを更新し、データを受信するたびにアプリがアクティブになる。 bluetooth-centralバックグラウンドモードは.plistファイルに設定されています。

iPhoneは引き続き印刷をデバッグし、タイムスタンプのUIを約25秒間更新してから停止します。これは、アプリケーションがフォアグラウンドでもバックグラウンドでも発生します。 編集:CBPeripheralManagerは現在didUnsubscribeFrom characteristicコールバックを受信して​​います。私は理由が何も表示されませんdidUnsubscribeFrom、なぜそれが常に25秒後のアイデアと呼ばれるだろう。

CBPeripheralManagerは依然としてタイムスタンプを送信し続けます。 CBPeripheralManagerupdateData(_:for:onSubscribedCentrals:)呼び出しからの戻り値は常に真であり、キューが一杯にならないことを示します。

ここには、最も関連性の高いコードがたくさんあります。 CBPeripheralManagerアプリから

func centralManagerDidUpdateState(_ central: CBCentralManager) { 
    print(central.state.rawValue) 
    centralManager.scanForPeripherals(withServices: [timeUUID], options: nil) 
    print("scanning") 
} 

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
    peripheral.delegate = self 
    peripheral.discoverServices(nil) 

    print("connected") 
} 

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
    print(peripheral.name as Any) 
    print(advertisementData[CBAdvertisementDataServiceUUIDsKey] as! Array<CBUUID>) 
    self.peripheralController = peripheral 

    self.centralManager.connect(peripheral, options: nil) 
} 

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 

    if service.uuid.uuidString == timeUUID.uuidString { 
     peripheralController.setNotifyValue(true, for: service.characteristics!.first!) 
     peripheralController.readValue(for: service.characteristics!.first!) // EDIT: This was the offending line 
    } 
} 

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { 

    if characteristic.uuid.uuidString == timeUUID.uuidString { 
     if let valueFrom = characteristic.value { 
      if let this = String(data: valueFrom, encoding: .utf8) { 
       if UIApplication.shared.applicationState == .active { 
        label.text = this 
        print("ACTIVE \(this)") 
       } else if UIApplication.shared.applicationState == .background { 
        print("BACKGROUND \(this)") 
       } else if UIApplication.shared.applicationState == .inactive { 
        print("INACTIVE \(this)") 
       } 
      } 
     } 
    } 
} 

CBCentralManagerアプリから

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { 
    myCharacteristic = CBMutableCharacteristic(type: myServiceUUID, properties: [CBCharacteristicProperties.read, CBCharacteristicProperties.notify], value: nil, permissions: CBAttributePermissions.readable) 
    let myService = CBMutableService(type: myServiceUUID, primary: true) 
    myService.characteristics = [myCharacteristic] 
    bluetoothController.add(myService) 
} 

func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) { 
    print(characteristic.uuid) 
    subscriber = central 
} 

func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) { 
    print(characteristic) 
    print("unsubscribe") 
} 

func repeatAdvertisement() { 
    timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [unowned self] (timerRef) in 
     guard let maybeTimer = self.timer, maybeTimer.isValid else { return } 
     let datum = Date() 
     let stringFromDate = self.dateFormatter.string(from: datum) 
     let data = stringFromDate.data(using: .utf8) 
     print(data!.count) 
     //  myCharacteristic.value = data 
     let myService = CBMutableService(type: self.myServiceUUID, primary: true) 
     myService.characteristics = [self.myCharacteristic] 
     let did = self.bluetoothController.updateValue(data!, for: self.myCharacteristic as! CBMutableCharacteristic, onSubscribedCentrals: [self.subscriber]) 
     print("timed \(stringFromDate) \(did)") 
    } 


} 


func advertise() { 
    if timer == nil { 
     repeatAdvertisement() 
    } else { 
     timer?.invalidate() 
     timer = nil 
    } 
} 

は、私はあなたが必要とする何かを知ってみましょう。

答えて

0

いいえ、天国です。問題は、peripheralController.readValue(for: service.characteristics!.first!)という行でした。サンプルコードに基づいてアプリ内にその行がありましたが、それは必要ありませんでした。

明らかにreadValue(for:)を呼び出すと、何らかの種類のタイムアウトが発生します。私はアプリからその行を編集し、それは幸せに更新されます。

誰かがいつでも同じことに直面する場合に備えて、質問を残してこの回答を追加してください。

関連する問題