Samsung Gear using AngularJS

Oct 10, 2015

7 min read

smartwatch

HTML

AngularJs

wearables

Author picture

by syed shanu

Contributor

30k Views

Introduction

Article image

What is Gear Odd Image Out Game?

In this game app, a total of five questions will be asked. In each question, I will display four sets of Images in that one Image will be different from the other three. User has to pick the odd image from the four images. If user picks the correct odd image from the question, he will get 10 points. If he select the wrong answer, then he will get -5 points. After all the five questions have been answered by the user, the result will be calculated and if the users' points are 50, then he wins the game and if the user get points less than 50, then he loses the game.

What you should know to create this Odd image Out App for Samsung Gear smartwatch?

You need to have basic knowledge of the following:

  • TIZEN IDE Web project Development
  • AngularJS Development

Prerequisites Needed to Install for Developing the Samsung Gear App

To start developing our first app for Samsung Gear, we need the following software to be installed.

  • Tizen SDK (Tizen consists of set of tools for developing Tizen web and native applications. It contains IDE for developing our applications and Emulator to view our output)
  • SDK Image

Note: If you are new to Samsung Gear App development using TIZEN IDE, then kindly view my previous article which explains in detail about how to install Tizen SDK and create your first "Hello world" program using Tizen IDE.

AngularJS

We might be familiar with what the Model, View, View Model (MVVM) and what Model, View and Controller (MVC) are. AngularJS is a JavaScript framework that is purely based on HTML, CSS and JavaScript.

The AngularJS Model View Whatever (MVW) pattern is similar to the MVC and MVVM patterns. In our example, I have used Model, View and Service. In the code part, let's see how to install and create AngularJS in our MVC application.

If you are interested in reading more about AngularJS, then kindly go through the following link:

How I Developed this Odd Image Out Game Gear App?

First, collect all the images needed to be displayed for the question. Each question carries four sets of images and in that one image should be as different. So collect as many images as possible to make more questions. I will display the questions randomly from the set of questions to avoid repeat question display. So first, we need to make as many questions as possible for the users to not get bored of the game. I have downloaded all the free images from the Icon Finder, since we are creating app for Smartwatch. Here, I have downloaded 48*48 size images.

Article image

After downloading all the Images and giving the proper name to each Image and in Image name itself, I have given as answer as O for Ok and N for Wrong Image. For the sample of this demo, I have created a total of 15 Questions with 15 * 4 images total. You can add more questions to make the app more interesting.

Using the Code

As I have already mentioned, if you are new to Tizen IDE development, kindly view my first article which explains in detail about how to download, install and get started with Tizen IDE to create your app for Samsung Gear.

Click -> Start programs -> Click Tizen IDE.

For the first time, it will select your Workspace. You can browse and select folder to create all your projects under the folder. Click OK to start your new project.

Article image

Once you are done, click on File -> New -> Tizen Web Project.

Article image

Creating our First Wearable UI Template: We can see a window like below. First, we start with Wearable UI Template. Select the Template and Wearable UI and click on Basic. Give name for your project and click Finish.

Article image

Once you have created, you can see your Project in the left side of Project Explorer. Create new folder inside your newly created project for adding all the AngularJS reference files. I have created the folder name as “MyAngular”. I have copied all AngularJS and JQuery related reference files to this folder and also created a new JavaScript file as “controller.js” for AngularJS controller creation.

Article image

Steps to Create AngularJS controller.js File

Step 1

All our Business logic will be written in AngularJS Controller.js file. As you can see in project “MyAngular” folder, I have created the “controller.js” file to write all our AngularJS code.

In the Controller.JS JavaScript file, we will create for AngularJS Model and Controller like below:

First, we add the reference and create Model for our AngularJS controller.

JSCRIPT
/// <reference path="angular.js" />
/// <reference path="angular.min.js" />
/// <reference path="angular-animate.js" />
/// <reference path="angular-animate.min.js" />

In controller, I have declared all the variables that need to be used and display the result in our HTML page.

Step 2: Declaration of Local Variables in Controller

Here, I have commented for each variable so that the user can easily understand it.

JSCRIPT
app.controller("AngularJs_Controller",

Step 3: Display New Game Message

Article image

When user clicks on the New Game Button, I will call the FirstQuestion() method and display and hide the relevant table row for displaying the question. And call the displayQuestion() method to display the first question.

Controller Method

JSCRIPT
$scope.FirstQuestion = 

HTML Code

<table>
          <tr  ng-show="showGameStart">
                     <td align="center">
                         <span style="color:#FFFFFF;font-size:x-large">
                           SHANU - Odd Image Out  </span><br><br>
      <input type="submit" value="New Game"
      style="background-color:#3f9835;color:#FFFFFF;border-color:#f6f803;
      border-style:dashed;" ng-click="FirstQuestion()" />
                           <p style="color:#FFFFFF;font-size:xx-small;">
            Find the Odd Image from the Given four Images.You will be having 5 questions.<br>
                           Answer all the 5 Question .
                           <br>Wrong Answer will get -5 points and 
                           correct answer will get 10 Points.<br>
                           If user answer all the 5 Questions correctly then they the winner.
                           </p>
                           </td>
                           </tr>

Step 4: Display New Game Question

Article image

When user clicks on New Game, I will call the displayQuestion() method to selected the random question from the set of questions which we have created and display Each question Image in HTML page.

Controller Method

JSCRIPT
$scope.displayQuestion = 

HTML Code

HTML
<tr 

Step 5: Find Answer

Now we have displayed the question so what is next. Yes in each question image click, we need to find the correct answers. In each image click event, I call the findAnswer(1) method to check for each question answer and calculate the result and display the points to the user and then to display the next question. In this method, I will pass the argument as 1,2,3,4 for each question clicked by order in each question answer I will be storing the answers in Number I will compare both the User Clicked image number with each questions answer. If both match, then I will add the Points as 10 and if the answer is wrong, then I will -5 with the total points. Finally, I will display the next question for the user to play.

Controller Method

JSCRIPT
// to find the Answer
    $scope.findAnswer = 

Here, we can see that I will calculate the answer and display the next question to user with total points he earned.

Article image

In the mean time, I will check for questions counter value. If the user answers total five questions, then I will call the displayResult() method to display the final results to the user.

Step 5: Display Final Result

In this method, I will check for the total points of the user and display the result.

Won the Game

If the points are greater than or equal to 50, then I will display the message as user won the game.

Article image

Lose the Game

If the points are lesser than 50, then I will display the message as user lose the game.

Article image

Controller Method

JSCRIPT
$scope.displayResult = 

HTML Code

HTML
<tr 

Step 5: Start New Game

After displaying the result, user can start the new game by clicking on Home Button.

Controller Method

JSCRIPT
$scope.NewQuestion = 

Step 6: View Output

Output with Simulator: To view the output with simulator, right click our project from Tizen Ide and select Run AS and click on “Tizen Web Simulator Application”

Article image

Once we clicked, we can see the output in simulator.

Article image

Output with Emulator

To view our result in Emulator, first we need to run the Emulator. In Connection Explorer, click on Emulator Manager.

Article image

Here, I have selected the emulator type as Circle and click on GearCircle play to open the emulator.

Article image

Once the emulator is opened, we need to select our Project and select RUN AS and click on “Tizen Web Application”.

Article image

Here, we can see our Odd Image Out app will be running in Samsung Gear Emulator.

Article image

Points of Interest

Hope you like this and now you might be having a clearer idea to start working with Samsung gear App development for Smartwatch. Have fun!

History

  • 10th October, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)