2017-09-22 27 views
2

J'essaie de découvrir les services d'un périphérique BLE auquel je me suis connecté depuis mon application iOS, mais lorsque ma fonction didDiscoverServices est appelée, la matrice des services périphériques est vide.Swift 3 Core Bluetooth ne découvrant pas les services

L'application se connecte au périphérique, exécute périphérique.discoverServices (nil), puis la fonction didDiscoverServices est appelée, mais aucun service n'est renvoyé. J'ai lu un certain nombre de réponses connexes bluetooth et d'autres exemples en ligne et le mieux que je puisse dire, mon code n'est pas différent de ce qu'il devrait être, sauf qu'il ne fonctionne pas. J'ai l'impression d'avoir manqué quelque chose quelque part mais je ne sais pas quoi.

J'ai ajouté mon journal de console ci-dessous pour ce que je reçois lors de l'exécution du code, et le code bluetooth en bas pour référence.

Bluetooth initialised 
BLE is powered on 
Optional("Nordic_Blinky") found at -72 
Scanning stopped 
Connect request sent 
Connected to <CBPeripheral: 0x1c0301a70, identifier = 0887CF7F-98C8-3FCF-2D10-873FFFFB2B65, name = Nordic_Blinky, state = connected> 
Discovering services 
Services -- Optional([]) and Error -- nil 

code de classe Mon BluetoothHandler est inférieure à

class BluetoothHandler : NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { 

// MARK: Properties 
var manager: CBCentralManager! 
var targetPeripheral: CBPeripheral! 


// MARK: Shared Instance 
static let sharedInstance = BluetoothHandler() 
private override init() { 
    super.init() 
    self.startManager() 
    print("Bluetooth initialised") 
} 

// MARK: Functions 

func startManager() { 
    manager = CBCentralManager(delegate: self, queue: nil) 
} 

func centralManagerDidUpdateState(_ central: CBCentralManager) { 

    var consoleMessage = "" 
    switch (central.state) { 

    case.poweredOn: 
     consoleMessage = "BLE is powered on" 
     manager.scanForPeripherals(withServices: nil, options: nil) 

    case.poweredOff: 
     consoleMessage = "BLE is powered off" 

    case.resetting: 
     consoleMessage = "BLE Resetting" 

    case.unknown: 
     consoleMessage = "BLE state unknown" 

    case.unsupported: 
     consoleMessage = "Device not supported by BLE" 

    case.unauthorized: 
     consoleMessage = "BLE not authorised" 
    } 
    print("\(consoleMessage)") 

} 

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

    if (peripheral.name == "Nordic_Blinky") { 

     print("\(String(describing: peripheral.name)) found at \(RSSI)") 
     self.stopScan() 

     if targetPeripheral != peripheral { 

      targetPeripheral = peripheral 
      targetPeripheral!.delegate = self 

      manager.connect(targetPeripheral, options: nil) 
      print("Connect request sent") 
     } 

    } 

    else if (peripheral.name != nil) { 
     print("\(String(describing: peripheral.name))") 
    } 
} 

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
    print("Connected to \(peripheral)") 
    peripheral.delegate = self 
    peripheral.discoverServices(nil) 
    print("Discovering services") 
} 

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Any) { 
    print("Connection failed", error) 
} 

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

    if self.targetPeripheral != nil { 
     self.targetPeripheral!.delegate = nil 
     self.targetPeripheral = nil 
    } 
    print("Connection disconnected") 
    self.startManager() 
} 

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
    print("Services -- \(peripheral.services) and Error -- \(error)") 

    if let services = peripheral.services { 
     for service in services { 
      peripheral.discoverCharacteristics(nil, for: service) 
     } 
    } 
} 
+0

Une application comme BlueLight.app a-t-elle trouvé des services? – Larme

+0

@Larme oui une application Android s'est connectée et obtient tous les services de l'appareil – AdamDWalker

+0

'BlueLight.app' sur iOS aussi? – Larme

Répondre

0

Essayez ceci: 3

rapides

Si votre périphérique (périphérique BLE) ont des services puis imprimer dans le journal.

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

    if let services = peripheral.services as [CBService]!{ 

     for service in services{ 

      peripheral.discoverCharacteristics(nil, for: service) 
     } 
    } 
    print("==>",peripheral.services!) 
} 

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

    for newChar: CBCharacteristic in service.characteristics!{ 

     print(newChar) 
    } 
} 
+0

Je suis déjà en train d'essayer d'imprimer les services à journaliser, mais il imprime uniquement un tableau vide car il n'y a pas de services (voir le journal du code et de la console) – AdamDWalker