withingssyncmanager
This class manages a service which allows background data synchronisation between your Withings devices and the Withings cloud.
API
Requirements
Background modes
This service requires the following background mode capabilities on your app:
- Uses Bluetooth LE accessories
- Acts as a Bluetooth LE accessory
Device tokens
This service requires you to provide one or more Withings device tokens. There are two ways to get a device token:
- After installing a Withings device with the SDK, you will receive a WithingsWebViewNotification
Installation success (1)
orInstallation from settings success (4)
with a parameteradvertise_key
which contains your device token. - Through the Withings API User v2 - Getdevice. This API lists your devices. The device token is stored with the
advertise_key
key.
Both methods must be implemented to ensure the proper functioning of the background sync service.
Start service
public class func start(with deviceKeys: [String])
When you start this service, your application will wake up in the background when a Withings device is within BLE range. The synchronisation process will then start in the background.
This function should be called if you want to support Bluetooth synchronisation:
- For Wi-Fi / BLE Devices: required when end user selects "I don't have a Wi-Fi option". Optional if user selects Wi-Fi sync in the setup process.
- For Bluetooth LE only trackers: required to support Bluetooth sync.
Stop service
public class func stop()
This method stops the background sync service.
Notifications
The SDK will post a notification to the NotificationCenter when a background sync event occurs for a device, such as:
- synchronisation started
- synchronisation succeeded
- synchronisation failed
The notification name is WithingsSyncStatus.notificationName
and it contains an enum value WithingsSyncStatus
within the userInfo dictionary (key = WithingsSyncStatus.notificationStatusKey
). This enum value describes the event.
Here is a sample code to listen for Background sync notifications:
NotificationCenter.default.addObserver(forName: WithingsSyncStatus.notificationName, object: nil, queue: nil) { [weak self] (notification) in
guard let status = notification.userInfo?[WithingsSyncStatus.notificationStatusKey] as? WithingsSyncStatus else { return }
switch status {
case .started(let peripheral: WithingsPeripheral):
print("Sync started for device token \(peripheral.deviceKey)")
case .succeeded(let peripheral: WithingsPeripheral):
print("Sync succeeded for device token \(peripheral.deviceKey)")
case .failed(let peripheral: WithingsPeripheral):
print("Sync failed for device token \(peripheral.deviceKey)")
}
}