I am trying to read core motion data (accel, gyro) and store them using my iWatch 6 for an extended amount of time (about 14 days). So, I realize that my application becomes unresponsive. I looked into it and I think that the watchdog kills my application because I am constantly writing stuff in OperationQueue.current! .To extract data from this queue, I need to get the foreground. So, I thought that my best bet would be to schedule a task and call it every ~30 min to do that for me. But when I schedule a task, it keeps happening for the first couple of times and then becomes very sporadic (maybe every 10 hours instead of 30 min). So, I am just looking for a way to do such an EASY task.
Since I was not able to use the scheduleBackgroundRefresh, I am using the locationmanager as an alternative (just like a timer) as it can run in the background. I know that it is not the best practice but I could not find any other way around this. To save the battery, I used the reduced accuracy which collects the location every 15 minutes or so. This runs great up until 8 hours. Then it becomes sporadic again! I am not sure what to do with that.
//
// ExtensionDelegate.swift
// STMITest WatchKit Extension
import WatchKit
import CoreMotion
class ExtensionDelegate: NSObject, WKExtensionDelegate, CLLocationManagerDelegate {
let motionManagerDelegate = CMMotionManager()
let locationManager:CLLocationManager = CLLocationManager()
var sensorTime=[Double]()
var sensorRx=[Double]()
var sensorRy=[Double]()
var sensorRz=[Double]()
var offsetTime=0.0
var offsetTimeFlag=true
var lastSyncedDate=Date()
var locationStartDate=Date()
func applicationDidFinishLaunching() {
print("Delegate:App finished lunching")
locationConfig()
locationStarter()
lastSyncedDate=Date()
locationStartDate=Date()
}
func locationConfig(){
print("LocationConfig Func")
locationManager.delegate=self
locationManager.desiredAccuracy = kCLLocationAccuracyReduced
locationManager.allowsBackgroundLocationUpdates=true
locationManager.activityType = .fitness
}
func locationStarter(){
if locationManager.authorizationStatus == .authorizedWhenInUse || locationManager.authorizationStatus == .authorizedAlways{
print("-------------initiation-authority is ok")
locationManager.startUpdatingLocation()
}
else{
print("-------------initiation-authority is changing")
locationManager.requestWhenInUseAuthorization()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.first != nil {
print("location:: \(locations)")
}
startQueuedMotionUpdates()
var timeInterval=Date().timeIntervalSince(lastSyncedDate)
if timeInterval > 1 * 60{
binaryWriter("GPSSuccess")
lastSyncedDate=Date()
}
timeInterval=Date().timeIntervalSince(locationStartDate)
if timeInterval > 3 * 60 * 60{
locationManager.stopUpdatingLocation()
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error ) {
print("Error in location manager")
startQueuedMotionUpdates()
var timeInterval=Date().timeIntervalSince(lastSyncedDate)
if timeInterval > 1 * 60{
binaryWriter("GPSError")
lastSyncedDate=Date()
}
timeInterval=Date().timeIntervalSince(locationStartDate)
if timeInterval > 3 * 60 * 60{
locationManager.stopUpdatingLocation()
locationManager.startUpdatingLocation()
}
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
print("authority should change",self.locationManager.authorizationStatus)
switch manager.authorizationStatus {
case .restricted, .denied, .notDetermined:
print("restricted, denied or notDetermined")
locationManager.requestWhenInUseAuthorization()
break
case .authorizedAlways:
print("always on")
break
case .authorizedWhenInUse:
print("only when it is in use")
locationManager.startUpdatingLocation()
}
}
// func taskScheduler(){
// let fireDate=Date(timeIntervalSinceNow: 60*60)
// let wkExt = WKExtension.shared()
// let info: NSDictionary = ["Caller": "to update motion"]
// wkExt.scheduleBackgroundRefresh(withPreferredDate: fireDate,userInfo:info) { (error: Error?) in if (error != nil) {
// print("background refresh could not be scheduled.")
// }
// else{
// print("successfully scheduled")
// }
// }
//
// }
func startQueuedMotionUpdates() {
if self.motionManagerDelegate.isDeviceMotionAvailable {
if !motionManagerDelegate.isDeviceMotionActive{
motionManagerDelegate.deviceMotionUpdateInterval = 30.0 / 60.0
motionManagerDelegate.showsDeviceMovementDisplay = true
}
motionManagerDelegate.startDeviceMotionUpdates(using: .xMagneticNorthZVertical, to: OperationQueue.current!, withHandler: { (data, error) in
if let validData = data {
let rX = validData.rotationRate.x
let rY = validData.rotationRate.y
let rZ = validData.rotationRate.z
let aX = validData.userAcceleration.x
let aY = validData.userAcceleration.y
let aZ = validData.userAcceleration.z
let timeStamp=validData.timestamp
if self.offsetTimeFlag{
self.offsetTime = 0.0-timeStamp
self.offsetTimeFlag=false
}
// self.sensorTime.append(contentsOf: [timeStamp+self.offsetTime])
self.sensorTime.append(contentsOf: [timeStamp+self.offsetTime,rX,rY,rZ,aX,aY,aZ])
}
})
}
else {
fatalError("The motion data is not avaialable")
}
}
func binaryWriter(_ extensionName: String) {
var myDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-HH:mm:ss"
var currentTime = dateFormatter.string(from: myDate)
var fileName = currentTime+extensionName
print("Writing Start========================",fileName)
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
do {
fileName = currentTime+extensionName+"Time"
var fileURL = dir.appendingPathComponent(fileName)
try (self.sensorTime as NSArray).write(to: fileURL,atomically: true)
self.sensorTime.removeAll()
}
catch{
print("Error in the binaryWriter Function")
}
}
myDate = Date()
currentTime = dateFormatter.string(from: myDate)
print("Writing End========================",currentTime)
}
}
Post
Replies
Boosts
Views
Activity
Hi
I am pretty new to ios development and I am trying to write a code to store the acceleration and gyro data into files using iwatch se (OS 7.5), iPhone XSMax (14.7.1) and XCode 12.5.1. I wrote most of my application in the ExtensionDelegate which I put below. The application gets killed after a couple of hours. Any ideas why? I think the watchdog kills it. I know this is not the most efficient way to write it but I was unable to make the timer thing working in the background! :(
import WatchKit
import CoreMotion
class ExtensionDelegate: NSObject, WKExtensionDelegate {
let motionManagerDelegate = CMMotionManager()
var sensorDataDelegate=[Double]()
var offsetTime=0.0
var offsetTimeFlag=true
func applicationDidFinishLaunching() {
print("Delegate:App finished lunching")
self.startQueuedMotionUpdates()
}
func applicationDidBecomeActive() {
print("Delegate:App active")
// self.startQueuedMotionUpdates()
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillResignActive() {
print("Delegate:App will be active")
// self.startQueuedMotionUpdates()
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, etc.
}
func applicationDidEnterBackground() {
print("Delegate:App is background")
// self.startQueuedMotionUpdates()
self.binaryWriter()
}
func applicationWillEnterForeground() {
print("Delegate:App will be foreground")
// self.startQueuedMotionUpdates()
}
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
// Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
self.startQueuedMotionUpdates()
self.binaryWriter()
for task in backgroundTasks {
// Use a switch statement to check the task type
switch task {
case let backgroundTask as WKApplicationRefreshBackgroundTask:
// Be sure to complete the background task once you’re done.
backgroundTask.setTaskCompletedWithSnapshot(false)
case let snapshotTask as WKSnapshotRefreshBackgroundTask:
// Snapshot tasks have a unique completion call, make sure to set your expiration date
snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
// Be sure to complete the connectivity task once you’re done.
connectivityTask.setTaskCompletedWithSnapshot(false)
case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
// Be sure to complete the URL session task once you’re done.
urlSessionTask.setTaskCompletedWithSnapshot(false)
case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:
// Be sure to complete the relevant-shortcut task once you're done.
relevantShortcutTask.setTaskCompletedWithSnapshot(false)
case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:
// Be sure to complete the intent-did-run task once you're done.
intentDidRunTask.setTaskCompletedWithSnapshot(false)
default:
// make sure to complete unhandled task types
task.setTaskCompletedWithSnapshot(false)
}
}
}
func startQueuedMotionUpdates() {
var myDate = Date()
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd-HH:mm:ss"
var currentTime = dateFormatter.string(from: myDate)
print("sensor function starts==============================",currentTime)
if self.motionManagerDelegate.isDeviceMotionAvailable {
if !self.motionManagerDelegate.isDeviceMotionActive{
self.motionManagerDelegate.deviceMotionUpdateInterval = 30 / 60.0
self.motionManagerDelegate.showsDeviceMovementDisplay = true
}
self.motionManagerDelegate.startDeviceMotionUpdates(using: .xMagneticNorthZVertical, to: OperationQueue.current!, withHandler: { (data, error) in
if let validData = data {
let rX = validData.rotationRate.x
let rY = validData.rotationRate.y
let rZ = validData.rotationRate.z
let timeStamp=validData.timestamp
if self.offsetTimeFlag{
// let currentTime=Date().timeIntervalSince1970
// self.offsetTime=currentTime-timeStamp
self.offsetTime = 0.0-timeStamp
self.offsetTimeFlag=false
}
self.sensorDataDelegate.append(contentsOf: [timeStamp+self.offsetTime])
}
})
}
else {
fatalError("The motion data is not avaialable")
}
myDate = Date()
currentTime = dateFormatter.string(from: myDate)
print("sensor function ends==============================",currentTime)
}
func binaryWriter() {
var tempVal = self.sensorDataDelegate
var myDate = Date()
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-HH:mm:ss"
var fileName = dateFormatter.string(from: myDate)
print("Writing Start========================",fileName)
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent(fileName)
do {
try (tempVal as NSArray).write(to: fileURL,atomically: true)
}
catch {/* error handling here */}
}
myDate = Date()
var currentTime = dateFormatter.string(from: myDate)
print("Writing End========================",currentTime)
self.sensorDataDelegate.removeAll()
myDate = Date()
currentTime = dateFormatter.string(from: myDate)
print("Data is cleaned========================",currentTime)
}
}
Hi
I am having an issue accessing gyroscope and magnometer data through CoreMotion. I have an apple watch 6 (os 7.6), iPhone XS max (ios 14.7) and XCode 12.5.1. I did write a simple application to print the accel, gyro and magno data. It works on the phone but not on the watch. On the watch, it only gives me accel data and when I do isGyroAvailable or isMagnoAvailable I get False, while isAccelAvailable gives me True. Should I turn on something? Maybe an authentication flag? Any help will be greatly appreciated.