2016-10-08 4 views
2

Explication:comment récupérer les périphériques BTLE lorsque l'utilisateur quitte manuellement l'application à partir de l'arrière-plan?

Je me connecte au périphérique BTLE à l'aide de la structure de base bluetooth, en enregistrant l'identificateur de périphérique dans une base de données.
En même temps, je suis en train d'enregistrer les périphériques dans un tableau pour gérer la reconnexion avec les périphériques.
Mais, lorsque l'utilisateur quitte manuellement l'application, mon tableau devient vide.
en raison de laquelle je ne serai pas en mesure de reconnecter ces périphériques à nouveau.

Maintenant, j'ai des identifiants de périphériques dans ma base de données.
J'ai lu à ce sujet et a constaté qu'il existe une méthode déléguée

- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers NS_AVAILABLE(NA, 7_0) 

Pour les périphériques, mais il ne retourne rien.

Comment puis-je récupérer les périphériques?

https://developer.apple.com/library/content/documentation//NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithA

Je vérifie également document ci-dessus. Dans ce qu'ils ont mentionné sur les méthodes de se reconnecter aux périphériques

Retrieving a List of Known Peripherals 

The first time you discover a peripheral, the system generates an identifier (a UUID, represented by an NSUUID object) to identify the peripheral. You can then store this identifier (using, for instance, the resources of the NSUserDefaults class), and later use it to try to reconnect to the peripheral using the retrievePeripheralsWithIdentifiers: method of the CBCentralManager class. The following describes one way to use this method to reconnect to a peripheral you’ve previously connected to. 

When your app launches, call the retrievePeripheralsWithIdentifiers: method, passing in an array containing the identifiers of the peripherals you’ve previously discovered and connected to (and whose identifiers you have saved), like this: 

    knownPeripherals = 
     [myCentralManager retrievePeripheralsWithIdentifiers:savedIdentifiers]; 
The central manager tries to match the identifiers you provided to the identifiers of previously discovered peripherals and returns the results as an array of CBPeripheral objects. If no matches are found, the array is empty and you should try one of the other two reconnection options. If the array is not empty, let the user select (in the UI) which peripheral to try to reconnect to. 

When the user selects a peripheral, try to connect to it by calling the connectPeripheral:options: method of the CBCentralManager class. If the peripheral device is still available to be connected to, the central manager calls the centralManager:didConnectPeripheral: method of its delegate object and the peripheral device is successfully reconnected. 

Note: A peripheral device may not be available to be connected to for a few reasons. For instance, the device may not be in the vicinity of the central. In addition, some Bluetooth low energy devices use a random device address that changes periodically. Therefore, even if the device is nearby, the address of the device may have changed since the last time it was discovered by the system, in which case the CBPeripheral object you are trying to connect to doesn’t correspond to the actual peripheral device. If you cannot reconnect to the peripheral because its address has changed, you must rediscover it using the scanForPeripheralsWithServices:options: method. 
+0

Si 'retrievePeripheralsWithIdentifiers' ne renvoie pas de périphérique, vous devrez découvrir le périphérique en recherchant sa publicité. – Paulw11

+0

Cela n'a pas plus de sens de stocker l'UUID sur une base de données locale et de l'extraire de votre thread d'arrière-plan –

+0

Oui Daniel, mais comment vous allez les récupérer. comme j'ai utilisé la méthode périphérique retrive et lui ai passé les identifiants. mais je n'ai toujours pas eu ces périphériques. – Nik

Répondre

0

Au démarrage de l'application, je voudrais juste découvrir tous les périphériques, puis comparer les périphériques découverts avec les identifiants périphériques stockés dans la base de données. Après la comparaison des périphériques et des périphériques découverts, les connexions pour les périphériques souhaités peuvent être effectuées.

+0

Salut Lipponen, je pense aussi pareil. mais sera intéressé s'il y a une autre solution. – Nik

+0

Pourquoi voudriez-vous une autre solution? Il suffit d'écrire votre tableau dans la base de données à chaque moment où il est mis à jour, et de repeupler votre tableau depuis la base de données lorsqu'il est vide et que la base de données n'est pas vide. – Tschallacka

+0

Nous enregistrons des identifiants de périphériques dans une base de données, mais nous n'avons pas l'objet périphérique pour la communication avec BLE. Il faut donc un moyen d'obtenir les périphériques des identifiants. – Nik