2017-05-04 3 views
0

私はこの問題を非常に混乱させています。私は問題が何であるか分かりません。このアプリケーションを約95%実行すると、正しく接続され、アラート特性にアクセスできますが、didUpdateValueForはトリガーされません。ときどき、私は変更するか、またはアプリケーションを再インストールするが正しく動作し、トリガします。他のアプリでテストしたところ、周辺機器の値が正しく更新されていることがわかりました。私はiPhone 6で作業していて、何度も再起動しました。私はそれが何らかの理由で動作してデバッグできなかったのかどうか不明です。どのようなアイデアや方法で問題を見つけることができますか?iOS Bluetoothキャッシングエラー?

import UIKit 
import CoreBluetooth 

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { 
    var manager: CBCentralManager! 
    var peripheral: CBPeripheral! 
    var devices: [CBPeripheral] = [] 

    var closestRSSI = 0 
    var alertCharacteristic: CBCharacteristic? = nil 

    let SERVICE_UUID = CBUUID(string: "06758213-258D-44B2-A577-8AE43E9DF674") 
    let ALERT_UUID = CBUUID(string: "B9FBC271-666B-4CA7-8F9C-F8E9C0223D20") 
    let BATTERY_UUID = CBUUID(string: "4D757E85-6A28-48CB-9CF5-299CB72D5AB2") 
    let FIRMWARE_UUID = CBUUID(string: "64CD5AF5-B6EE-4D46-A164-246BB197F5DA") 
    let CLIENT_CONFIG_UUID = CBUUID(string: "00002902-0000-1000-8000-00805F9B34FB") 
    var characteristicUUIDArray: [CBUUID] = [] 

    @IBOutlet weak var connectButton: UIButton! 
    @IBOutlet weak var infoLabel: UILabel! 
    @IBOutlet weak var headLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     manager = CBCentralManager(delegate: self, queue: nil) 
     infoLabel.text = "0" 
     characteristicUUIDArray = [ALERT_UUID, BATTERY_UUID, FIRMWARE_UUID] 
    } 

    @IBAction func connectButtonPressed(_ sender: Any) { 

    } 

    func centralManagerDidUpdateState(_ central: CBCentralManager) { 
     switch central.state{ 
     case .unauthorized: 
      print("Unauthorized to use BLE") 
      break 
     case .poweredOff: 
      print("Bluetooth is currently powered off") 
      break 
     case .poweredOn: 
      print("Bluetooth is ready to use") 
      manager.scanForPeripherals(withServices: [SERVICE_UUID], options: nil) 
      break 
     case .resetting: 
      print("Blueooth is resetting") 
      break 
     case .unknown: 
      print("Bluetooth is unknown") 
      break 
     case .unsupported: 
      print("Bluetooth is unsupported") 
      break 
     } 
    } 

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

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
     print(peripheral.name ?? "Null") 
     if peripheral.name != nil{ 
      if peripheral.name! == "Watch" { 
       self.manager.stopScan() 
       self.peripheral = peripheral 
       self.peripheral.delegate = self 
       manager.connect(peripheral, options: nil) 
      } 
     } 
    } 

    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { 
     print("Disconnected") 
     manager.cancelPeripheralConnection(peripheral) 
     manager.scanForPeripherals(withServices: [SERVICE_UUID], options: nil) 
    } 

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
     print("\nDid Connect To \(peripheral.name!)") 
     peripheral.discoverServices([SERVICE_UUID]) 
    } 

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
     print("Did Discover Services") 
     for service in peripheral.services!{ 
      let thisService = service 

      if thisService.uuid == SERVICE_UUID{ 
       peripheral.discoverCharacteristics(characteristicUUIDArray, for: thisService) 
      } 
     } 
    } 

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 
     print("Did Discover Characteristics") 
     for charateristic in service.characteristics! { 
      let thisCharacter = charateristic 

      if thisCharacter.uuid == ALERT_UUID{ 
       print("Alert") 
       peripheral.setNotifyValue(true, for: thisCharacter) 
      } 

//   if thisCharacter.uuid == BATTERY_UUID{ 
//    print("Battery") 
//    self.peripheral.setNotifyValue(true, for: thisCharacter) 
//   } 
     } 
    } 

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { 
     print("Did update") 
     if(characteristic.uuid == ALERT_UUID){ 
      print("Did Update Value For Alert") 
      let content = String(data: characteristic.value!, encoding: String.Encoding.utf8) 
      let value = content?.components(separatedBy: ",") 
      let warning = Int(value![0]) 
      infoLabel.text = String(warning!) 
     } 

     if(characteristic.uuid == BATTERY_UUID){ 
      print("Did Update Value For Battery") 
     } 
    } 
} 
+0

私は何かに夢中です。私が警告のためにdidDiscovercharacteristicにブレークポイントを追加し、それが瞬間を待つようにすると、毎回動作するようです。通知する特性を設定する際にタイミングの問題がありますか? – Ubarjohade

+0

私がthisCharacter.uuid == ALERT_UUIDにブレークポイントを持っている限り、私は再開を押した後に更新されます。これは、iOSに新しい人にとっては厄介な問題です – Ubarjohade

答えて

0

私はそれが競合状態のエラーであると信じています。遅延で各特性をラップすることで問題は解決されました。ここに私が変えたものがあります。だから私はそれが競合状態のエラーだと信じています。遅延で各特性をラップすることで問題は解決されました。ここに私が変えたものがあります。私はこれがなぜそれを修正するのか不明ですが、誰かが何らかのアイデアを持っているなら、なぜこれが聞こえて楽しむでしょうか

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 
    print("Did Discover Characteristics") 
    for charateristic in service.characteristics! { 
     let thisCharacter = charateristic 

     if thisCharacter.uuid == ALERT_UUID{ 
      print("Alert") 
      DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3), execute: { 
       peripheral.setNotifyValue(true, for: thisCharacter) 
      }) 
     } 

     if thisCharacter.uuid == BATTERY_UUID{ 
      print("Battery") 
      DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3), execute: { 
       peripheral.setNotifyValue(true, for: thisCharacter) 
      }) 
     } 
    } 
} 
関連する問題