Swift Watchos 2 - Cmsensordatalist

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 in Privacy/Motion & Fitness (on
the iPhone). Then to make the app authorized for Motion & Fitenss I
had to access one of the core motion function (like
startActivityUpdatesToQueue or even recordAccelerometerForDuration).
After that you just need to confirm on the iPhone and from now on
CMSensorRecorder.isAuthorizedForRecording() returns true.

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:

  1. I am starting the recoding session with recordAccelerometerForDuration(30). Here I record the current date:
    recordingStartDate = NSDate().

  2. 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.

  3. 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
from accelerometerDataFromDate.

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



Leave a reply



Submit