How to persist filtered event on disconnect

edited April 2015 in iOS
I have setup the metawear to log a filtered event and I am trying to figure out how to retrieve those logs after a disconnect.  The event documentation says to use retrieveEventWithIdentifier but when I look at the retrieveEventWithIdentifier documentation it says it is depreciated and to use setConfiguration instead.  It isn't clear to me how you would use that instead.  In particular, the documentation says that setConfiguration will delete all non-volatile memory when called.  Seems like that would delete the logged info that I would want to retrieve.  

Can anyone explain the proper way to setup logging and then retrieve the logged data after a disconnect?


  • A very good question, I wrote a blog post for the benefit of all.  Please let me know if you have further questions or feedback.
  • Thanks Steven.  That was very helpful.  I am working in swift and relatively new to iOS programming.   I have one question about this block: (the forum is limiting my response so will split it up).

  • [self.device setConfiguration:[[DeviceConfiguration alloc] init] handler:^(NSError *error) {

    if (!error) {

    // Programming successful!



    What are you passing as the first argument to setConfiguration?  It looks like a new DeviceConfiguration object, but it doesn't have the required coder parameter so I am a little confused.

  • xcode was complaining when I tried to create an instance without passing an argument to init so I added an init method that didn't take args.  That fixed the compiler issue but now when I reconnect, the results of decodeObjectForKey are always nil.  Here is my code -  What I am missing?
  • Unfortunately we haven't played much with Swift, so I can't comment on the differences you are seeing with regards to compiler errors.  However, what you have looks correct.

    Can you confirm that after you connect to the MetaWear and call "self.device.setConfiguration", it returns without error?  Then disconnect and confirm "aCoder.encodeObject(self.pulseWidthEvent, forKey: "pulseWidthEvent")" is called and has a valid object?

    Then kill the app and reload (this will make sure we have to reload the object with NSCoding), and see if "coder.decodeObjectForKey" works correctly? Maybe get rid of the cast and check if any object is there?
  • setConfiguration is definately returning without error.  I am able to download the logs as expected if I don't disconnect.

    When you say, "Then disconnect and confirm..."  How have you been picturing the disconnect to happen?  I have just been killing the app.  Is that the issue?  Do I need to manually disconnect from the device when the app is closed?
  • I added some logging and this is what I learned:

    On first run:
    1. [Connect to device]
    2. It tries to decode and finds a nil object
    3. [SetConfiguration called]
    4. encodes nil object
    5. [runOnDeviceBoot completes]
    6. encodes event object
    7. [setConfiguration completes]
    8. [disconnect]
    9. encodes event object
    [kill app and run again]
    1. [Connect to device]
    2. decode event object
    3. [setConfiguration called]
    4. encodes nil object
    5. [runOnDeviceBoot completes]
    6. encodes event object
    7. [setConfiguration completes]
    So it looks to me like the issue is caused by step #4.  It seems that whenever setConfiguration is called it creates a new event.  Am I suppose to only ever call setConfiguration once even between app runs?  If so, would I do that by using remember and then only calling setConfiguration if connecting to a device that was not remembered?  
  • setConfiguration should only be called once.  Think of it as installing a program on your computer.  Your app should have some notion of a setup phase where you scan for MetaWear's and then choose the one you want to use.  At this point you connect and call setConfiguration, effectively installing your custom events.

    Subsequent connects to that device will remember what was installed, and you can access them by using the configuration property and casting the result to a MetawearConfg.  Then access the pulseWidthEvent property.
  • Got it.  I have updated my gist to reflect that working for other people who might read this thread -

    You might want to update the blog post to clarify what you just said and perhaps include an example of checking whether setConfiguration needs to be run.

    Thanks for your help. 
  • Thanks for the feedback, I'll update accordingly!
This discussion has been closed.