2016-11-28 10 views
0

BlueFruit BLE SPIモジュールを使用しているArduinoプロジェクトに接続しようとしています。 iOSアプリを使用して接続しようとすると問題が発生します。私はデバイスに接続しようとしたが、状態は接続状態= 1になっている。これは、サービスの検索から私を防ぎ、このような「接続」状態が...。ここ を達成したコードスニップがあるされていないため、Bluetooth周辺機器が「接続中」状態でiOSに接続しています

//check state of the bluetooth on phone 
func centralManagerDidUpdateState(_ central: CBCentralManager) { 
    if central.state == .poweredOff{ 
     //TODO: ADD SAVE DATA TO REALM BEFORE DISSMISSING 
     errorView.isHidden = false 
    } 
    if central.state == .poweredOn{ 
     errorView.isHidden = true 
     //scan for peripherals with the service i created 
     central.scanForPeripherals(withServices: nil, options: nil) 
    } 
} 

//devices found(should only be ours because we will create Unique serviceID) 
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
    // get advertisement data and check to make sure the name is matching. set it as the peripheral then make connection 
    if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String { 
     print("NEXT PERIPHERAL NAME: \(peripheralName)") 
     print("NEXT PERIPHERAL UUID: \(peripheral.identifier.uuidString)") 

    if peripheralName == nameID{ 
     manager.stopScan() 
     self.peripheralHalo = peripheral 
     peripheralHalo!.delegate = self 
     manager.connect(peripheral, options: nil) 

     while(peripheralHalo?.state.rawValue == 1) 
     { 
      if(manager.retrieveConnectedPeripherals(withServices: [serviceID]).count > 0){ 
       print("\(manager.retrieveConnectedPeripherals(withServices: [serviceID]))") 
      } 
     } 
    } 
     print("Connected!!") 
    } 

私は呼んでmanager.connect(周辺機器、オプション:ゼロ)、ペリフェラルは接続しようとします。テストのために以下のwhileループを追加し、常に状態を「接続」として表示します。 LightBlue iOSアプリを試してみましたが、特性値の変更の通知を適切に接続して受け取ることができるので、Arduinoのファームウェアはすごくいいはずです。ご参考までに!

答えて

1

ループしたくないです。whileループです。これにより、コアBluetoothデリゲートスレッドがブロックされます。 connectを発行した後、didConnectCBCentralManagerDelegateメソッドに電話がかかります。ペリフェラルが接続されたら、ペリフェラルにdiscoverServicesを呼び出す必要があります。peripheral:didDiscoverServices:ペリフェラルデリゲートメソッドへのコールバックが行われます。同様の方法で特性を発見することができます。あなたがに接続したい周辺機器を識別する何かを保存しようとしている場合

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
// get advertisement data and check to make sure the name is matching. set it as the peripheral then make connection 
    if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String { 
     print("NEXT PERIPHERAL NAME: \(peripheralName)") 
     print("NEXT PERIPHERAL UUID: \(peripheral.identifier.uuidString)") 

     if peripheralName == nameID { 
      self.peripheralHalo = peripheral 
      central.stopScan() 
      central.connect(peripheral, options: nil) 
     } 
    } 
} 

func centralManager(_ central: CBCentralManager, 
       didConnect peripheral: CBPeripheral) { 
    print("Connected!!") 
    peripheralHalo!.delegate = self 
    peripheral.discoverServices([serviceID) 
} 

はまた、私はあなたが識別子を使用していない名前のように名前を変更することができます示唆しています。

+0

ありがとうございました。私はwhileループなしでそれを試していますし、「didconnect」も「didfailconnect」も呼び出されません。 – StoNeD510

+0

ありがとうポールそれをした!私はすべてを試したと思って、何時間も時間を費やしてそれを見つめていました。私があなたの投稿したコードから切り替えたのは、周辺機器への私のハードリファレンスであり、代理人はdidDiscoverで作られなければなりませんでした。そうでなければ接続を落としていた – StoNeD510

+0

ええ、私はそれについてはわからなかった。私はちょうどテストするためのコードを書いていました。 – Paulw11

関連する問題