/wp-content/uploads/2015/07/sap_logo_752822.png

Appendix H:  Non Kapsel Plugins

The following examples demonstrate how to include and use some of the native functionality that Cordova plugins provide.

Taking, Storing and Retrieving a Picture
Acquiring the Device Location and Displaying it on a Map

Using the PhoneGap Push Notifications Plugin

Taking, Storing and Retrieving a Picture

The following steps demonstrate how to take a picture using the Cordova camera pluginand how to store and retrieve previously taken images.

  • Create a new project named CameraDemo and add Android or iOS or both.
    cordova create C:\Kapsel_Projects\CameraDemo com.mycompany.camera CameraDemo
    cd CameraDemo
    cordova platform add android
    
    cordova create ~/Documents/Kapsel_Projects/CameraDemo com.mycompany.camera CameraDemo
    cd ~/Documents/Kapsel_Projects/CameraDemo
    cordova platform add ios
  • Add the Kapsel Encrypted Storage plugin and the Cordova Camera plugin.
    cordova plugin add cordova-plugin-camera
    
    cordova plugin add kapsel-plugin-encryptedstorage --searchpath %KAPSEL_HOME%/plugins
    or
    cordova plugin add kapsel-plugin-encryptedstorage --searchpath $KAPSEL_HOME/plugins
    
  • Replace the contents of  C:\Kapsel_Projects\CameraDemo\www\index.html with index.html.
  • Prepare, build and deploy the app with the following commands.
    cordova run android
    or
    cordova run ios

    image1.PNG

Acquiring the Device Location and Displaying it on a Map

The following steps demonstrate how to get a location and then use Google services to translate a location into an address and to display a map showing the address.

  • Create a new project named LocationDemo and add Android or iOS or both.
    cordova create C:\Kapsel_Projects\LocationDemo com.mycompany.location LocationDemo
    cd LocationDemo
    cordova platform add android
    
    cordova create ~/Documents/Kapsel_Projects/LocationDemo com.mycompany.location LocationDemo
    cd ~/Documents/Kapsel_Projects/LocationDemo
    cordova platform add ios
  • Add the InAppBrowser plugin to the project to enable showing the Google Map in a separate window that will have a close button.
    cordova plugin add cordova-plugin-inappbrowser
  • Note, that on some devices it may be required to add the geolocation plugin using the following command.
    cordova plugin add cordova-plugin-geolocation
  • Replace the contents of  C:\Kapsel_Projects\LocationDemo\www\index.html with index.html.
  • Add the below permissions if using Android to the AndroidManifest.xml
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  • Prepare, build and deploy the app with the following commands.
    cordova run android
    or
    cordova run ios

      If using an Android emulator, the emulator’s location will need to be passed to it.  To do this, switch to the DDMS view, select the emulator under devices, select Emulator Control, press the Send button.  Then press the Get Location button on the emulator.
    On an iOS simulator the location can be changed under Debug > Location.
    image2.PNGimage3.PNG

  • Depending on the device, the location may not be returned before the timeout occurs.
    On Android, ensure that the Location is enabled under Settings > Location > On.  The Mode can also be set.  Finally under recent location requests should appear LocationDemo if you have previously pressed the Get Location button.
    On iOS, ensure that Location is enabled under Settings > Privacy > Location > Location Service > On

Using the PhoneGap Push Notifications Plugin

The following steps demonstrate how to use the PhoneGap Push plugin.  Note, Kapsel also provides a similar pushplugin.

  • Create a new Android project named PushDemo2.
    cordova create C:\Kapsel_Projects\PushDemo2 com.mycompany.push2 PushDemo2
    cd PushDemo2
    cordova platform add android
    
  • Add the push, media, and console plugins to the project.
    cordova plugin add phonegap-plugin-push 
    cordova plugin add cordova-plugin-media
    cordova plugin add cordova-plugin-console
  • Replace the contents of  C:\Kapsel_Projects\PushDemo2\www\index.html with index.html.
  • Follow the instructions at Getting Started with GCM to create an API project, receive a project number (sender ID), and enable the GCM service for the project and to obtain an API key.
  • Add your senderID (received from Google to the register method in index.html on line 7.
  • Provide a wav file to be played when a notification is received while the app is in the foreground.  For example
    copy "C:\Windows\Media\Heritage\Windows Notify.wav" C:\Kapsel_Projects\PushDemo2\www\beep.wav
  • Copy the contents of the www folder to the platform specific www folder of the project using prepare.
    cordova prepare
    
  • Deploy the Kapsel app to an Android emulator based on the Google APIs or an Android device.  On startup the app will register with Google and receive a registration ID.  This registration ID can be used to send the app notifications.
    image4.PNG
  • Install node-gcm
     C:\Kapsel_Projects\PushDemo2\npm install node-gcm
  • Copy notify.js to C:\Kapsel_Projects\PushDemo2\notify.js.
    Edit notify.js and fill in the registration ID on line 20.  The registration ID can be copied from the Android logcat or the Web Inspector console by searching for registrationId and then selecting the line containing it and pressing Ctrl C.
    Also provide the API key on line 5.
  • Press the home button to place the app in the background.  Type
    node notify.js 

    to send a notification.  Notice that a notification appears on the device. 
    image5.PNG

    Open the notifications and then click on the notification.
    image6.PNG

    Note, try also sending a notification while the app is displayed (foreground notification) and after closing the app via the back button (cold start notification)

  • See also Tutorial: Implement Push Notifications in your PhoneGap Application

Back to Getting Started With Kapsel

To report this post you need to login first.

13 Comments

You must be Logged on to comment or reply to a post.

  1. Daniel Van Leeuwen Post author

    Changed the lockTimeout setting on the passcode policy of the camera example from 0 to 300 to prevent an error that could occur when attempting to save the picture.  The data vault was not being unlocked in time before the call to store the image using the encrypted storage plugin.

    (0) 
  2. David Fish

    Hi Daniel,

    I am getting an error when trying to save the image to the encrypted store. The message says: Failed because: 4. This message appears twice.

    I am running this on an iPhone 6+. Any idea what that error message means and why it is appearing twice?

    Thanks,
    Dave

    (0) 
    1. Daniel Van Leeuwen Post author

      I just tried the example and did not see an error.

      The error codes are described in the C:\SAP\MobileSDK3\KapselSDK\plugins\encryptedstorage\www\encryptedstorage.js

      /**

      * This error indicates that the datavault managed by the Logon plugin is locked.

      * the Logon plugin must be in an unlocked state to use the EncryptedStorage plugin.

      * @memberof sap.EncryptedStorage

      * @name sap.EncryptedStorage#ERROR_DATAVAULT_LOCKED

      * @constant

      */

      EncryptedStorage.prototype.ERROR_DATAVAULT_LOCKED = 4;

      I would double check that the init method is being called and specifically that the sap.Logon.initPasscodeManager’s successCallback is being called before you try to save or retrieve something from the encrypted storage.

      Hope that helps,

      Dan van Leeuwen

      (0) 
      1. David Fish

        Hi Dan,

        I didn’t realize that the passcode was required and I had disabled it. Once I ran the app and entered the passcode the encrypted storage plugin worked properly.

        Thanks,

        Dave

        (0) 
        1. Daniel Van Leeuwen Post author

          As far as I know the passcode is not required.  I tried it on iOS and Android by pressing the Dissable Passcode button in the example included in this guide.

          Regards,

          Dan van Leeuwen

          (0) 
          1. David Fish

            I am using an iPhone 6+ (iOS 9.2). I had a typo in the appId which I fixed. I’m not sure the previous version of the app was cleared off the phone when I tested it. I just tried it again with the passcode disabled and it worked. So the problem was the appId typo.

            Thanks,

            Dave

            (0) 
    1. Daniel Van Leeuwen Post author

      I assumed that on most devices this plugin is not needed.  I will include a link to it.  Did adding it make a difference for you?  What device did you use?

      Note that

      GitHub – apache/cordova-plugin-geolocation: Mirror of Apache Cordova Plugin geolocation

      says

      This API is based on the W3C Geolocation API Specification, and only executes on devices that don’t already provide an implementation.


      Thanks again for the feedback,


      Dan van Leeuwen

      (0) 

Leave a Reply