A Guide to Develop a Fall Detection Application in Apple Watch

Apple Watch Fall Detection Image

On 24th April, 2015, Apple released the Apple Watch Series 1. They followed it up with three major OS updates and a product update. New features included taking all incoming notifications, alerts, messages, and channeling them into the watch face. Now, watchOS 2 allowed developers to build watch native applications that run directly on the watch.

I started developing a watch companion for one of our iOS apps. Here we tried to send an alert to the server if the watch fell or the man wearing the watch fell. The steps I followed are as follows:

1.Set up the environment for the current iOS application to include a watch app.

2.Set up the connectivity between the watch app and the iOS app.

3.Sensors in Apple watch.

4.Detecting a fall in watch.

Step 1. Setting up the environment

To add a watch target to your iOS app, select File → New → Target in Xcode tool.TargetNow select watchOS tab from the menu and click “WatchKit App” and hit the “Next” button.

WatchOS Tab

  • Next, you need to add product related information and click the “Finish” button.
  • As you hit “Finish”, you will see that two groups in your project navigator have been added to your project bundle with name as “ProductName watch” and “WatchKit extension”. Two groups are created, as the code of watch app runs as an extension to the watch app. The figure below illustrates the relationship between the watch app, WatchKit extension and the iOS app.

Relationship between iOS app and Watch app

Figure: Relationship between Watch app, WatchKit extension and iOS app.

(Image Source: developer.apple.com)

Now select the watch scheme from the scheme options and build. You will see that the watch app has been added successfully to your selected device.

Step 2. Setting up connectivity between the watch app and the iOS app

Once watch app is added to your bundle, we needed to set up connectivity between the iOS app and the watch app. That is, we had to try transfer data between the parent (iOS) and the watch application.

As seen in above Figure 1, the WatchConnectivity framework links the iOS app and watch app. So, we need to use the WatchConnectivity framework from Xcode libraries and frameworks. This provides us with various methods. Each method has its own use, depending on the situation to share data between the two apps.

Prior to that, we had to create and activate a communication-facilitating WCSession instance. Transfers occur between the two sides, only when the sessions go active on both sides. Data can still be send to the other side, even if only one session is active. But the transfer may happen opportunistically, in the background.

(a).updateApplicationContext :error:  This can be used to share small data chunks to update the counterpart state. The data you send is volatile, as it gets replaced on each successive call.

(b).transferUserInfo: Used to share data even if the counterpart app is not running and data gets transferred and queued for delivery.

(c).sendMessage:replyHandler:errorHandler: This is used when the counterpart app is also running and data gets transferred immediately. It does not work when either of the apps is in inactive mode.

Apart from these data transferring methods, one can also share files between the apps. We have used the transferUserInfo: api in our case, as we need to transfer the data to server (in any case) if a fall is detected. You can use any other depending on the severity of transfer.

Step 3. Sensors in Apple Watch

Apple Watch consist of motion sensors and a custom sensor used to measure the heart rate. Motion sensors are placed include the accelerometer, gyroscope and magnetometer. The heart rate sensor uses visible light and infrared light along with photodiodes.

Accelerometer helps in detecting the movement of the device in any direction. Three accelerometers power Apple Watch. Each identifies the change in velocity in linear path along the three axes (x-axis, y-axis, z-axis). The data from the three accelerometers is combined and provided to us in the form of a single acceleration. Gyroscope helps in identifying the rate of rotations around the three axes. Magnetometer helps in identifying the direction in which the device motion is pointing, relative to the horizon or heading relative to north.

Data from the three motion sensors is collected and used to provide a struct called device motion. To view what the WatchKit provides, we can use a simple code snippet as follows:

CMMotionManager *motionMgr = [[CMMotionManager alloc] init];
if([motionMgr isAccelerometerAvailable])
NSlog(@”Accelerometer is available”);
if([motionMgr isGyroAvailable])
NSlog(@”Gyroscope is available”);
if([motionMgr isMagnetometerAvailable])
NSlog(@”Magnetometer is available”);
if([motionMgr isDeviceMotionAvailable])
NSlog(@”Device motion identification is available”);

Step 4. Fall Detection in Apple Watch

A fall is the condition when the total acceleration applied on the object equals the value of acceleration of gravity. We now know the accelerometer that provides Apple Watch the acceleration by which the device moves along the linear path. So, we will use this value to detect a fall in device.

So, I took the total acceleration, i.e. user acceleration and gravity along an axis, squared it up and then added the values along all three axes. Lastly, I took the square root of the entire equation which gives the absolute value of acceleration along all the axis. When this value equals the gravitational force value for an adequate amount of time, it implies a fallen device. It consequently raises an alert to the server. A code snippet (using objective C) is listed below:

 //Getting acceleration along all three axis
double accelerationX = motion.gravity.x + motion.userAcceleration.x;
double  accelerationY = motion.gravity.y + motion.userAcceleration.y;
double  accelerationZ = motion.gravity.z + motion.userAcceleration.z;

 //squaring and adding the acceleration along all three axis and again taking square root of total value
double totalAcceleration = sqrt((accelerationX * accelerationX) + (accelerationY * accelerationY) + (accelerationZ * accelerationZ));

 // checking if value is near to acceleration of gravity (9.8 m/s2)
if ( totalAcceleration >  9.0) {
NSLog(@”Raise an alert of fall to server”);

You can further enhance the code by identifying the direction of motion and then raising the alert. I hope this guide helps you in developing creative watch applications. Please share your suggestions or questions at mayur.sojrani@cuelogic.com.