Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
218 lines (134 sloc) 11.8 KB

Salesforce Wear Pack for Nymi

The Nymi, by Bionym, is a wearable device that acts as a universal secure identifier that is locked to a single person through their unique electrical heart signature, or ECG. The Nymi wrist band features two electrodes, one that touches your wrist and the other that you touch with a finger on the opposite hand. This creates a circuit through your heart and allows your ECG to be measured and streamed over Bluetooth.

In this repository you'll find a sample application that demonstrates a fictional application used by the Sassy Springs Hotel. In it, we'll register and communicate with hotel VIPs as they interact with staff and features of the hotel.

Nymi Video

First time setup

You can try out this sample application yourself by following these steps.

  1. Register for a Developer Edition environment and create a Connected App in Salesforce. Follow the instructions under Create a Connected App in Salesforce.

  2. Log into your Developer Edition environment and create a custom field called ProvisionId (API name as ProvisionId__c) on the Contacts object.

  3. Clone the repository: git clone git@github.com:developerforce/WearablePack-Nymi.git

  4. Register for Bionym's developer program. You can find more information on the Bionym website.

  5. Place the Nymi library libNCLiOSHardAndSim.a in the project's /lib folder.

  6. Open /samples/HotelVIP/iOSNymi.xcodeproj.

  7. Update the RemoteAccessConsumerKey variable in AppDelegate.m with the Consumer Key defined in your Connected App from step #1.

  8. Update the clientId and clientSecret values in the getETToken method of the GlobalMethods.m class with your ExactTarget credentials. If you would like to signup for ExactTarget, please contact a sales representative. You can configure ExactTarget using the getting started guide, message send guide, and these two resources: message contact send, and creating a new message in mobile connect.

  9. Confirm that the project builds.

  10. For the sample application to connect to an actual Nymi device you will need to change the build target to an actual iOS device.

Run and enjoy!

Code Walkthrough

This sample application interacts with numerous services and devices. Here is a little background on some of these integrations.

Nymi Device

The sample application interacts with the Nymi device using the core Bluetooth stack. Ensure that you have CoreBluetooth.framework added to your project frameworks. You will also have to add the libNCLiOSHardAndSim.a library provided by the Bionym developer program; the reference exists in the project but the file itself is not in the repository.

For details on using the Nymi SDK be sure to review the SDK documentation.

The important thing to not about interacting with the Nymi device is that it all occurs through asynchronous callbacks. These callbacks are registered during the viewDidLoad event. The first time you communicate with the Nymi you'll initialize the device ...

sharedManager.nclInit = nclInit(
  discoverCallback,
  (__bridge void*)self,
  "iOS Base",
  NCL_FALSE,
  nil
  );

... whereas subsequent interactions will simply add a callback ...

nclAddCallback(registerCallback, (__bridge void*)self, NCL_EVENT_PROVISION, sharedManager.nymiHandle);

After the callback is registered, you can then call an event like nclStartDiscovery(); to interact with the device. Once it completes successfully, it will hit the callback registered in the class and perform the appropriate opperations.

Salesforce

Salesforce Mobile SDK for iOS

The project itself is based on the Salesforce Mobile SDK for iOS toolkit. If you want to start from scratch, you'll have to do the following:

  1. Install the forceios tools: sudo npm install forceios -g

  2. Run the forceios command: forceios create --apptype="native" --appname="iOSNymi" --companyid="com.salesforce.iOSNymi" --organization="salesforce.com" --packagename="com.salesforce.iOSNymi"

  3. Open the new Xcode project.

  4. Add the following two frameworks: CoreBluetooth.framework, CoreGraphics.framework

  5. Add the libNCLiOSHardAndSim.a library from the Nymi SDK.

  6. Fix the build. From Build Settings -> Search Paths -> Library Search Paths change /Users/username/src/appname/lib to "/Users/username/src/appname/lib" to fix the build. (Should be fixed in a later release of the iOS toolkit.)

At this point you can start using the Nymi SDK.

API Integrations

The iOS application periodically calls the Salesforce APIs. You can find detailed documentation online, but here are a few examples of how it works:

SFRestRequest *request =
  [SFRestRequest
    requestWithMethod:SFRestMethodPOST
    path:[NSString stringWithFormat:@"/v29.0/sobjects/Contact"]
    queryParams: (NSDictionary *)[SFJsonUtils objectFromJSONString:body]];

[[SFRestAPI sharedInstance] send:request delegate:self];

You'll want to register a callback so that you can handle the response back from Salesforce. Here's a look at that code:

- (void)request:(SFRestRequest *)request didLoadResponse:(id)dataResponse {
    NSString *contactId = [dataResponse objectForKey:@"id"];
    ... do something ...        
}

The benefit of using the iOS toolkit is that all the authentication and management of tokens is handled by the toolkits.

Exact Target

To get started with Exact Target you will need an account, and the best way to get one is by contacting a sales representative.

The sample application has three interactions with Exact Target: requesting an access token, sending an email, and sending an SMS.

Requesting an Access Token

To request a token you'll use your clientId and clientSecret. Simply construct a JSON request and post the the https://auth.exacttargetapis.com/v1/requestToken endpoint and extract the accessToken from the response.

+ (NSString*)getAccessToken
{
    NSString *jsonRequest = @"{\"clientId\":\"YOURCLIENTID\",\"clientSecret\":\"YOURCLIENTSECRET\"}";
    
    NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://auth.exacttargetapis.com/v1/requestToken"]];
    
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];
    
    NSURLResponse* response;
    NSError* error = nil;
    
    NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: result options:0 error:nil];
    return [jsonArray valueForKey:@"accessToken"];
}

You can find this method in the ExactTarget.m file.

Sending an Email

To send an email, you'll have to first setup Exact Target. Take a look at the respourses listed above under Getting Started.

Once configured it is a two step process: attach your access token to the request header, and post a JSON message with details to the messageDefinitionSends endpoint:

+ (void)sendEmail:(NSString*)accessToken :(NSString*)emailAddress :(NSString*)firstName :(NSString*)lastName
{
    NSString *jsonRequest =
    [NSString stringWithFormat:
     @"{\"To\": {\"Address\": \"%@\",\"SubscriberKey\": \"%@\",\"ContactAttributes\": {\"SubscriberAttributes\": {\"Full_Name\": \"%@ %@\"}}}}",
     emailAddress,
     emailAddress,
     firstName,
     lastName];
    
    NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends/key:Nymi_Triggered_Send/send"]];
    
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    
    NSString *bearer = [NSString stringWithFormat:
                        @"Bearer %@",
                        accessToken];
    
    [request setValue:bearer forHTTPHeaderField:@"Authorization"];
    [request setHTTPBody: requestData];
    
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}

You can find this method in the ExactTarget.m file.

Sending an SMS

To send an SMS, you'll have to first setup Exact Target. Take a look at the respourses listed above under Getting Started.

Once configured it is a two step process: attach your access token to the request header, and post a JSON message with details to the send endpoint:

+ (void)sendSMS:(NSString*)accessToken :(NSString*)phoneNumber
{
    NSString *jsonRequest =
    [NSString stringWithFormat:
     @"{\"MobileNumbers\": [\"%@\"],\"subscribe\" : true,\"resubscribe\" :true,\"keyword\" : \"NYMISUB\"}",
     phoneNumber];
    
    NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.exacttargetapis.com/sms/v1/messageContact/MTM6Nzg6MA/send"]];
    
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    
    NSString *bearer = [NSString stringWithFormat:
                        @"Bearer %@",
                        accessToken];
    
    [request setValue:bearer forHTTPHeaderField:@"Authorization"];
    [request setHTTPBody: requestData];
    
    NSURLResponse* response;
    NSError* error = nil;
    
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
}

You can find this method in the ExactTarget.m file.

Contributing to the Repository

If you find any issues or opportunities for improving this respository, fix them! Feel free to contribute to this project by forking this repository and make changes to the content. Once you've made your changes, share them back with the community by sending a pull request. Please see How to send pull requests for more information about contributing to Github projects.

Reporting Issues

If you find any issues with this demo that you can't fix, feel free to report them in the issues section of this repository.