Swift watchOS 2 - CMSensorDataList
//First make the extension tu use enumerate in the for-in loop
extension CMSensorDataList: SequenceType {
public func generate() -> NSFastGenerator {
return NSFastGenerator(self)
}
}
//Now you can query the recorded data
func printData(){
let date = NSDate()
let recorder = CMSensorRecorder()
let sensorData: CMSensorDataList = recorder.accelerometerDataFromDate(initialDate!, toDate: date)!
for (index, data) in sensorData.enumerate() {
print(index, data)
}
}
watchOS2 - CMSensorRecorder
Maybe you can try to
func startMovementDetection(){
self.startDate = NSDate()
self.cmSensorRecorder?.recordAccelerometerFor(self.recorderDuration)
}
And have a look at Swift watchOS 2 - CMSensorDataList
Swift - Unable to retrieve CMSensorDataList records
I ran your code and did not get the "Response invalid" or "Error occurred". I did get the main thread warnings. So I changed to a background thread and it works fine.
Also, I don't think you need to wait six minutes. I changed it to one minute.
I hope this helps.
let recorder = CMSensorRecorder()
@IBAction func recordAcceleration() {
if CMSensorRecorder.isAccelerometerRecordingAvailable() {
print("recorder started")
DispatchQueue.global(qos: .background).async {
self.recorder.recordAccelerometer(forDuration: 30)
}
perform(#selector(callback), with: nil, afterDelay: 1 * 60)
}
}
@objc func callback(){
DispatchQueue.global(qos: .background).async { self.getData() }
}
func getData(){
print("getData started")
if let list = recorder.accelerometerData(from: Date(timeIntervalSinceNow: -60), to: Date()) {
print("listing data")
for data in list{
if let accData = data as? CMRecordedAccelerometerData{
let accX = accData.acceleration.x
let timestamp = accData.startDate
//Do something here.
print(accX)
print(timestamp)
}
}
}
}
CMSensorRecorder is not authorised, but I am never prompted to give authorisation?
Found THIS thread for that and there is one answer for that which says:
I found that the
CMSensorRecorder.isAuthorizedForRecording()
returns
true
only after your app is authorized inPrivacy/Motion
&Fitness
(on
the iPhone). Then to make the app authorized forMotion
&Fitenss
I
had to access one of the core motion function (like
startActivityUpdatesToQueue
or evenrecordAccelerometerForDuration
).
After that you just need to confirm on the iPhone and from now on
CMSensorRecorder.isAuthorizedForRecording()
returnstrue
.Still, I can't get any data from the
CMSensorRecroding
. In my case the
accelerometerDataFromDate
function does not return any data - the
returned value is always nil. Because it is said elsewhere that it can
take up to 3mins for the data to become available, I am doing the
following scenario:
I am starting the recoding session with
recordAccelerometerForDuration(30)
. Here I record the current date:
recordingStartDate = NSDate()
.I wait more than
3min30s
(keeping the app on the watch active) and after this time I call:
accelerometerDataFromDate(NSDate(timeInterval: 10, sinceDate: recordingStartDate), toDate: NSDate(timeInterval: 20, sinceDate: recordingStartDate)
As you can see, I making a 10s window within the requested 30s recording frame.
I get
nil
.
I also tried shorter timeouts, accessing the data immediately, and
even activating the accelerometer before calling
ecordAccelerometerForDuration. Nothing helps, I still get nil back
fromaccelerometerDataFromDate
.I really wonder how you guys are able to get any readings back from
the sensor recorder...Maybe things will get better after September 9.
So for authorization
I tried startActivityUpdates
like shown below
self.activityManager.startActivityUpdates(to: OperationQueue.main, withHandler: {(data: CMMotionActivity!) -> Void in
})
and you need to declare let activityManager = CMMotionActivityManager()
And once user allow it if CMSensorRecorder.authorizationStatus() == .authorized {
will be true
.
NSFastEnumeration in Swift
After a while poking around the swift framework files, I finally found this nice class called NSFastGenerator
. NSSet
and friends seem to be using the same Generator
.
For ZBarSymbolSet
, here's how you'd extend it to support for-in
loops:
extension ZBarSymbolSet: SequenceType {
public func generate() -> NSFastGenerator {
return NSFastGenerator(self)
}
}
Update: Looks like Swift 2.0's protocol extensions fixed this for us!
Related Topics
In Xcode 6.1. 'Uiimage' Does Not Have a Member Named 'Size' Error
How to Stop Timer in Text View
Navigationview Pops Back to Root, Omitting Intermediate View
How to Make the Scroll of a Tableview Inside Scrollview Behave Naturally
Dispatchsourcetimer and Swift 3.0
Upvote/Downvote System Within Swift via Firebase
Override Func Error in Swift 2
Building Pure Swift Cocoa Touch Framework
Swift - Unit Testing Private Variables and Methods
How to Scale Text to Fit Parent View with Swiftui
How to Set the Blurradius of Uiblureffectstyle.Light
Open Uidatepicker Programmatically in iOS 14
Can Associated Values and Raw Values Coexist in Swift Enumeration
Hide Navigation Bar Without Losing Swipe Back Gesture in Swiftui
Ios13 Navigation Bar Large Titles Not Covering Status Bar
Determining If Swift Dictionary Contains Key and Obtaining Any of Its Values