Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Metawear: example of how to persisting event after disconnects
import Foundation
class MetawearConfig:NSObject, MBLRestorable {
var pulseWidthEvent:MBLEvent!
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.pulseWidthEvent, forKey: "pulseWidthEvent")
required init(coder: NSCoder) {
self.pulseWidthEvent = coder.decodeObjectForKey("pulseWidthEvent") as? MBLEvent
override init () {
func runOnDeviceBoot(device: MBLMetaWear) {
// Log some Accelerometer data
device.accelerometer.fullScaleRange = MBLAccelerometerRange.Range2G
device.accelerometer.filterCutoffFreq = 0
device.accelerometer.highPassFilter = true;
var periodicEvent = device.accelerometer.rmsDataReadyEvent.periodicSampleOfEvent(1000)
self.pulseWidthEvent = periodicEvent.pulseDetectorOfEventWithThreshold(30, width: 3, output: MBLPulseOutput.Width)
println("Logging Started")
// Config code
self.device.connectWithHandler({ error in
let configuration : MetawearConfig = self.device.configuration as MetawearConfig
if (configuration.pulseWidthEvent == nil) {
self.device.setConfiguration(MetawearConfig()) { error in
if (error != nil) {
println("setConfiguration error: \(error.localizedDescription)")
println("set Configuration successful")

This comment has been minimized.

Copy link

typarker commented Feb 22, 2016

Thanks for this example, I'm also having issues messing with Metawear with Swift. Do you have any idea how to erase all commands for all events? In the Android documentation there's something called removeMacros() but it seems like eraseCommandsToRunOnEventAsync has to be attached to a specific event to remove it.

I'm trying to let the user set a threshold level on the fly and have it update the Metawear to alert them with vibration whenever the threshold gets crossed, even when the device is offline. Right now it seems like it's just stacking multiple commands on top of each other. My email is if you have any suggestions.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.