2016-10-08 15 views
2

설명 :백그라운드에서 앱을 수동으로 종료 할 때 BTLE 주변 장치를 검색하는 방법은 무엇입니까?

코어 블루투스 프레임 워크를 사용하여 BTLE 주변 장치에 연결하여 주변 장치 식별자를 데이터베이스에 저장합니다.
동시에 주변 장치와의 재 연결을 처리하기 위해 주변 장치를 배열에 저장합니다.
하지만 사용자가 앱을 수동으로 종료하면 배열이 비게됩니다.
으로 인해 그 주변 장치를 다시 연결할 수 없기 때문입니다.

이제 데이터베이스에 주변기기 ID가 있습니다.
나는 그것에 대해 읽고 발견이 주변을 얻을 수있는 대리자 메서드

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

이지만 아무 것도 반환하지 않습니다 것을.

어떻게 주변 장치를 다시 얻을 수 있습니까?

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

또한 문서의 위 확인. 그 그들은 그냥 모든 주변 장치를 발견 한 후 데이터베이스에 저장된 주변 식별자 발견 된 주변 장치를 비교하는 것입니다 응용 프로그램 시작에 주변

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

'retrievePeripheralsWithIdentifiers'가 주변 장치를 반환하지 않으면 주변 장치를 검색하여 해당 주변 장치를 검색해야합니다. – Paulw11

+0

로컬 데이터베이스에 UUID를 저장하고 백그라운드 스레드에서 검색하는 것이 더 이상하지 않습니다. –

+0

예 Daniel,하지만 어떻게 복구 할 것입니까? 주변기기 검색 방법을 사용하고 식별자를 전달했습니다. 하지만 여전히 그 주변 장치를 얻지 못했습니다. – Nik

답변

0

에 다시 연결하는 방법에 대해 언급했다.

발견 된 주변 장치와 주변 장치를 비교 한 후 원하는 주변 장치에 대한 연결을 만들 수 있습니다.

+0

안녕 Lipponen, 나는 또한 같은 생각입니다. 다른 해결책이 있다면 관심을 가질 것입니다. – Nik

+0

왜 다른 솔루션을 원하십니까? 업데이트 된 순간마다 데이터베이스에 배열을 작성하고 비어 있고 datase가 비어 있지 않으면 데이터베이스에서 배열을 다시 채 웁니다. – Tschallacka

+0

주변 장치 식별자를 데이터베이스에 저장하지만 BLE와의 통신을위한 주변 장치 개체가 없습니다. 따라서 식별자로 주변 장치를 가져 오는 방법이 필요합니다. – Nik