问题
I want to use the historical accelerometer data from the Apple Watch and my accDataList is always nil. I instantiated CMSensorRecorder in the init function of the class. Does someone had this problem before?
func startMovementDetection(){
self.cmSensorRecorder?.recordAccelerometerFor(self.recorderDuration)
self.startDate = NSDate()
}
func extractHistoricalAccelerometerData(){
var accDataList = self.cmSensorRecorder!.accelerometerDataFrom(self.startDate, to: NSDate())
NSLog("AccDataList : \(accDataList)")
if accDataList != nil {
accDataList = accDataList as CMSensorDataList
for accData in accDataList {
self.addHistoricalAccDataToMovementArray((accData as? CMRecordedAccelerometerData)?.acceleration)
}
self.sendDataToIphone()
}
}
Log looks like this:
2015-07-11 22:02:55.279 emmoveDataWatchOS Extension[2158:2428612] Start recording
2015-07-11 22:02:55.279 emmoveDataWatchOS Extension[2158:2428612] AW: Start Motion Updates
2015-07-11 22:02:55.279 emmoveDataWatchOS Extension[2158:2428612] AW EMWatchManager: startMovementDetection
2015-07-11 22:02:55.279 emmoveDataWatchOS Extension[2158:2428612] Optional(CMSensorRecorder: 0x797733d0)
2015-07-11 22:10:51.483 emmoveDataWatchOS Extension[2158:2428612] AW: Stop Motion Updates
2015-07-11 22:10:51.484 emmoveDataWatchOS Extension[2158:2428612] Extract Data from Optional(2015-07-11 20:02:55 +0000) to 2015-07-11 20:10:51 +0000
2015-07-11 22:10:51.484 emmoveDataWatchOS Extension[2158:2428612] Optional(CMSensorRecorder: 0x797733d0)
2015-07-11 22:10:51.486 emmoveDataWatchOS Extension[2158:2428612] AccDataList : nil
回答1:
Maybe you can try to
func startMovementDetection(){
self.startDate = NSDate()
self.cmSensorRecorder?.recordAccelerometerFor(self.recorderDuration)
}
And have a look at Swift watchOS 2 - CMSensorDataList
回答2:
the CMSensorRecorder also have a bug: you should wait for 3 to 5 minutes to restart your Recorder, otherwise the List will always return nil.(you can watch the Apple's video for more details.)
回答3:
I had the same problem, found the solution in this post:
https://forums.developer.apple.com/thread/15470
"It apparently does not like the charger to be plugged in on the watch when starting recroding"
Just need to disconnect the device from its charger. Makes sense, but of course not documented officially anywhere...
来源:https://stackoverflow.com/questions/31184842/watchos2-cmsensorrecorder