Quantcast
Channel: Reality matters
Viewing all articles
Browse latest Browse all 184

How to build beaconified Apple Watch apps using Estimote's SDK & Nearables Simulator

$
0
0

Apple Watch’s debut is just around the corner but WatchKit, Apple’s framework for building Watch extensions for iPhone apps, has already been out for quite some time. We know that thousands of you have been eagerly waiting to start working on integrating iBeacon and nearables with the most exciting wearable of the season. So here it is: Estimote WatchKit SDK, consisting of an open-source demo app and a beta build of Estimote SDK 3.0, which includes tools to smoothen the Watch app ⇔ iOS app communication, and Nearables Simulator to go along with the Apple Watch simulator already available in Xcode.

image

How to build your first WatchKit app with stickers?

A quick recap on the architecture of the WatchKit app. We have the WatchKit app itself, it runs on the Watch and it’s only about the presentation layer. We have the WatchKit extension which resides on the phone and runs all the Watch-related code. This code is supposed to be lightweight, so it’s not a good candidate for beacon/stickers ranging and monitoring. And finally, we have the “parent” iOS app, also running on the phone, in a separate process — and that’s our perfect spot for any long-running, background tasks like monitoring.

There’s the catch: the WatchKit extension and the “parent” app live in separate processes and sandboxes, so if we’re to pass beacons or stickers ranging/monitoring results from the app into the extension, we need to employ some inter-process communication. Apple has naturally given developers all the necessary tools to do it, but at Estimote we went one step further to make sure that you can have your beacon-enabled Watch application up and running in no time. So let’s talk about ESTNotificationTransporter, the latest addition to the 3.0 beta of the Estimote SDK. Shameless plug: there’s a lot of other changes coming in 3.0, and since it’s still in the beta phase, we would love to hear your feedback about it.

image

The way you’d usually go about communicating beacon and stickers ranging results from the iOS app to the WatchKit extension:

  1. [iOS app] Receive the ranging results in your didRange delegate.
  2. [iOS app] Serialize them and put them into a “shared storage” — the App Group.
  3. [iOS app] Notify the WatchKit extension that new ranging results are available.
  4. [WatchKit extension] Receive the notification from the iOS app.
  5. [WatchKit extension] Read and unserialize data from the App Group storage.
  6. [WatchKit extension] Act on the newest data.

ESTNotificationTransporter takes care of all of that for you.

First, in your Project Settings, on the Capabilities tab, enable the App Group capability and add an App Group, e.g. group.com.example.helloWatch. In both your iOS app and WatchKit extension’s code, obtain an instance of the ESTNotificationTransporter and set it up with this App Group identifier:

self.transporter = [ESTNotificationTransporter sharedTransporter];
[self.transporter setAppGroupIdentifier:@"group.com.example.helloWatch"];

Then, set your iOS app with beacon or nearable manager as usual — let’s assume it’s the nearable manager. In the didRangeNearable delegate add:

- (void)nearableManager:(ESTNearableManager *)manager 
       didRangeNearable:(ESTNearable *)nearable {
    [self.transporter saveNearable:nearable];
}

In your WatchKit extension’s setup, subscribe to appropriate notification:

[self.transporter addObserver:self
                     selector:@selector(didRangeNearableViaNotification:)
              forNotification:ESTNotificationDidSaveNearable];

…and add the code to handle it:

- (void)didRangeNearableViaNotification:(NSNotification *)notification {
    ESTNearable *nearable = [self.transporter readNearable];
    // TODO: add your Watch app response to the ranging results here
}

That’s it for ranging nearables, but the ESTNotificationTransporter can also handle communication around enter and exit events, proximity zone changes etc. Well, let’s go through one more example then — enter the range of a nearable. In the didEnterIdentifierRegion delegate add:

-  (void)nearableManager:(ESTNearableManager *)manager 
didEnterIdentifierRegion:(NSString *)identifier {
    [self.transporter notifyDidEnterIdentifierRegion:identifier];
}

In the WatchKit extension’s setup:

[self.transporter addObserver:self
                     selector:@selector(didEnterNearableRange)
              forNotification:ESTNotificationDidNearableEnterRegion];

And the handler method:

- (void)didEnterNearableRange {
    NSString *id = [self.transporter readIdentifierForMonitoringEvents];
}

We’ve prepared an example Sneaker Seeker app which illustrates the whole concept. It’s very similar to the Proximity Demo from the regular Estimote SDK, so those of you familiar with it should already know the drill. It uses ranging to approximate the distance from an Estimote Sticker, and determines in which proximity zone the user is. As the user moves between zones, the Watch app reacts accordingly, updating content in real time. Sneaker Seeker is part of the Estimote WatchKit SDK, so feel free to check it out today.

Nearables simulator

image

You know how to code up the communication between the app that’s on your iPhone and scanning for beacons or stickers, and its WatchKit extension. The question is: how do you go about testing it? You can only run your Watch app in a simulator at this time, and the simulator doesn’t exactly work with beacons or stickers…

You could mock it all up, but it’s time consuming, so… we’ve done it for you! Meet another addition to the Estimote SDK 3.0 — ESTSimulatedNearableManager:

self.simulator = [[ESTSimulatedNearableManager alloc]
                  initWithDelegate:self
                  identifier:@"0a1b2c3d4e5f6a7b"
                  zone:ESTNearableZoneFar];

This instantiates the simulator with a single, simulated nearable with the specified ID and in “far” distance to the phone. While at this time proximity zone is the only property that’s being stubbed out by the simulator, we’ll be adding more of them soon, so that you can also simulate your nearables moving, flipping around, getting cold etc. These leverage our built-in accelerometer and temperature sensors, which give incredible context to objects!

The simulator works just like a regular ESTNearableManager, so we can now start ranging for our simulated nearable:

[self.simulator startRangingForIdentifier:@"0a1b2c3d4e5f6a7b"];

…and receive appropriate events (didRange, didEnter, didExit) to our ESTNearableManagerDelegate like usual — only it will actually work in the iOS simulator, providing a “virtual” ESTNearable object:

- (void)nearableManager:(ESTNearableManager *)manager
       didRangeNearable:(ESTNearable *)nearable {
    // assert(nearable.identifier == @"0a1b2c3d4e5f6a7b");
    // assert(nearable.zone == ESTNearableZoneFar);
    // ...
}

Naturally, the simulator wouldn’t be much useful if we couldn’t dynamically control the properties of the simulated nearable and trigger enters or exits on demand:

[self.simulator simulateZoneForNearable:ESTNearableZoneNear];

Monitoring works, too!

[self.simulator startMonitoringForIdentifier:self.nearable.identifier];

.

[self.simulator simulateDidEnterRegionForNearable:self.nearable];

...

-  (void)nearableManager:(ESTNearableManager *)manager
didEnterIdentifierRegion:(NSString *)identifier {
    // assert(identifier == self.nearable.identifier);
    // ...
}

.

[self.simulator simulateDidExitRegionForNearable:self.nearable];

...

- (void)nearableManager:(ESTNearableManager *)manager   
didExitIdentifierRegion:(NSString *)identifier {
    // assert(identifier == self.nearable.identifier);
    // ...
}

Finally, once you’re past the development and testing stages, and ready to go live, all it takes is to replace the ESTSimulatedNearableManager with a regular ESTNearableManager. You’ll also need to remove any simulator-only methods from your code.

Coming soon: more Apple Watch goodies

We’re just getting started with Apple Watch support. Expect additional examples joining the Sneaker Seeker app soon, to demonstrate how beacons and stickers can interact with glances, notifications and more. We’ll be also adding an ESTSimulatedBeaconManager, and expanding the capabilities of the nearables simulator. And because we’re so excited about how wearables and nearables will work together, in the coming weeks you’ll see a lot of content related to this subject on our blog, Community Portal, and in other places — tutorials, inspirations, anything to help you build the “killer app” for Apple Watch.

Now it’s time for you to let us know how you like the new update, and what cool ideas for context-aware applications you can think of in regard to wearables. We’ve just recently opened new Estimote Community forums, so it’s a great place to start the discussion on beacons, nearables and Apple Watch. You can of course also reach us on Twitter and in the old fashioned way.

Piotr Krawiec, Technology Evangelist at Estimote
Wojtek Borowicz, Community Evangelist at Estimote


Viewing all articles
Browse latest Browse all 184

Trending Articles