2017-09-11 2 views
0

J'ai lu le tutoriel pour utiliser le nRF8001 d'Adafruit et le connecter à un appareil iOS via Arduino. Jusqu'à présent, j'ai tout configuré correctement et cela fonctionne très bien dans leur application. J'essaie d'écrire ma propre application qui (pour l'instant) fait exactement la même chose, j'ai lu leur tutoriel et de ce que je comprends, j'ai copié le code aussi près que possible, jusqu'à présent, je peux obtenir la connexion et la plupart les choses semblent fonctionner (UI-sage), semblent cependant je ne peux pas faire quoi que ce soit passé se connecter à l'appareil:
Voici mon code APRÈS connexion:Swift BLE "didDiscoverServices" ne s'exécutant pas. Est-ce que je manque quelque chose?

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
     //What to do when it discovers a peripheral, add it to the array list 
     print("Peripheral found: " + (peripheral.name ?? "Unknown Name")) 
     peripheralsFoundNames.append((peripheral.name ?? "Unknown Name")) 
     peripheralsFoundData.append((advertisementData.description)) 
     peripheralsFoundCB.append(peripheral) 
     peripheralsFoundRSSIs.append(RSSI) 
    } 

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
     print("Connected to device!") 
     displayStatusAlert(localmsg: "Connection Succesful!") 
     NotificationCenter.default.post(name: Notification.Name(rawValue: DEVICE_READY_KEY), object: self) 
     data?.length = 0 //clear any data that might be stored 
     peripheral.discoverServices([BLETemperatureService]) 
     print("Here at didConnect, connected to:" + peripheral.name!) 
     // Here needs to add code to check if it's a single or multi-channel device via the advertisement data or some other constant, maybe the name? 
    } 

Comme vous pouvez le voir, je fais appel explicitement périphériques.discoverServices, puis j'ai une instruction d'impression qui s'exécute. Ensuite, je donne les résultats suivants (NOTE AUCUNE DES LIGNES CI SEMBLE EXECUTE À TOUT MOMENT (AU MOINS PAS LES ÉTATS D'IMPRESSION).

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
    print("here at diddisoverservices") 
     if ((error) != nil){ 
      displayStatusAlert(localmsg: "Error: \n" + (error?.localizedDescription ?? "Error Unknown")) 
     } 

     guard let services = peripheral.services 
      else{ 
       return 
     } 

     for service in services { 
      peripheral.discoverCharacteristics(nil, for: service) 

     } 
     print ("Discovered!") 
    } 


    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 
     if ((error) != nil){ 
      displayStatusAlert(localmsg: "Error: \n" + (error?.localizedDescription ?? "Error Unknown")) 
     } 

     guard let characteristics = service.characteristics 
      else{ 
       return 
     } 
     for characteristic in characteristics { 
      //looks for the right characteristic 
      print("looking for characteristic") 


      if characteristic.uuid.isEqual(BLERXCharacteristic) { 
       deviceConnectedRXChar = characteristic 

       //Once found, subscribe to the this particular characteristic 
       peripheral.setNotifyValue(true, for: deviceConnectedRXChar!) 

       peripheral.readValue(for: characteristic) 
       print("Rx Characteristic: \(characteristic.uuid)") 
      } 
      if characteristic.uuid.isEqual(BLETXCharacteristic){ 
       deviceConnectedTXChar = characteristic 
       print("Tx Characteristic: \(characteristic.uuid)") 
      } 
      peripheral.discoverDescriptors(for: characteristic) 
     } 

     print ("Characteristic discovered") 
    } 

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

     if characteristic == deviceConnectedRXChar { 
      if let ASCIIstring = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue) { 
       receivedDataString = ASCIIstring 
       print("Value Recieved: \((receivedDataString as String))") 
       NotificationCenter.default.post(name: Notification.Name(rawValue: DEVICE_SENT_DATA), object: nil) 

      } 
     } 
+0

Première:. Pourriez-vous montrer la 'didDiscoverPeripheral' et que vous avez une forte référence sur périphérique? Deuxièmement: Si vous faites 'peripherique.discoverServices (nil)', ça marche? – Larme

+0

Ajouté didDiscoverPeripheral code, en outre, essayé avec (nil) et toujours obtenir les mêmes résultats (l'impression "ici à diddiscoverservices" ne fonctionne pas) –

+0

Après 'peripheralsFoundCB.append (périphérique)', vous n'avez jamais appelé 'central.connect (périphérique, options: []) 'Il l'a découvert, mais il n'a pas démarré de connexion. Un périphérique est limité de la connexion simultanée, et si vous êtes dans l'environnement avec des centaines de périphériques, j'espère que c'est comme ça. – Larme

Répondre

0

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) est une

méthode CBPeripheralDelegate

donc ce que vous manque est . définissant l'objet CBPeripheraldelegate

Alors juste avant de faire peripheral.discoverServices([BLETemperatureService]), vous devez faire peripheral.delegate = self