Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Creating a Patient-Doctor Monitoring System for Asha Devices

From Wiki
Jump to: navigation, search

This article explains how to create a patient-doctor communications system for Asha Devices, using the Notifications API.

Note.pngNote: This is an entry in the Nokia Asha Wiki Competition 2013H2.

Article Metadata
Code Example
Source file:
Tested with
Devices(s): Nokia Asha 501
Created: arunkam (15 Dec 2013)
Last edited: arunkam (21 Dec 2013)



This article considers the scenario of a hospital with many patients and few doctors. Doctors may want to check on the status of a particular patient from any location. Likewise, a patient may need to contact their doctor. While there are many possible alternative implementations to set up set up communication between the two parties; this one uses the Nokia Notifications API.

The core of this architecture is a server-side database which stores all the details about the patients and doctors (this is written in PHP and MYSQL for this example, but could be done using any language). The stored information includes the notification session IDs for the doctor/patient client apps (obtained by the client app when it registers with the Notification API server, and passed to the patient/doctor server). The patient/doctor server can use these IDs, via the Notification API server, to send messages to all registered devices.

The basic operation of the system is simple:

  1. Patients and doctors enter their details into the client app(s) and they get stored in the patient/doctor database. In this case a different table is used for each type of user.
  2. The client apps register with the Nokia Notification server, receive a Notification ID, and send this to the patient/doctor server to be stored too.
  3. If a patient needs to send a message to their doctor they can do so through the patient/doctor server, which knows the doctor's notification ID (and visa versa).

Server side implementation

The core of this system is the backend. This is where majority of the work takes place. The database name in this example is articledb and the table names are Patient and Doctor.

The Patient table has 5 columns: ID, Name, History, Medications and NID. The Doctor table has 2 columns: ID and NID.

The ID is a unique identifier used to identify each patient and doctor. The History column contains record of all the patient's medical history and the Medications column contains data about the medications consumed by the patient. The NID column contains the Notification IDs of the patients and doctors respectively.

//Connecting to database
$connection = mysql_connect("localhost", "username", "password");
echo mysql_error();
$db_select = mysql_selectdb("articledb", $connection);
// Getting the patient details
$name = $_REQUEST['name'];
$history = $_REQUEST['history'];
$medicines = $_REQUEST['medicines'];
//Storing patient details
$query = "INSERT INTO patient (name,history,medicines) VALUES ('". $name . "','" . $history . "','" . $medicines . "')";
//Sending false in case of failure.Else the unique ID of the patient is returned.
$result = mysql_query($query);
echo mysql_error();
if (mysql_errno() != 0) {
echo mysql_error();
echo "false";
echo mysql_insert_id();

The above code is used to put the data into the database. We obtain the unique ID generated for each user and then use it to login the user.

//Connecting to database
$connection = mysql_connect("localhost", "username", "password");
echo mysql_error();
$db_select = mysql_selectdb("articledb", $connection);
//Getting patient ID.
$val = $_GET['val'];
if ($val == null)
$query = "SELECT * FROM patient";
$query = "SELECT * FROM patient WHERE id=" . $val;
//Getting patient details
$result = mysql_query($query);
$output_array = array();
$i = 0;
while($row = mysql_fetch_array($result))
$output_array[$i]['name'] = $row['name'];
$output_array[$i]['history'] = $row['history'];
$output_array[$i]['medicines'] = $row['medicines'];
$output_array['count'] = $i-1;
echo mysql_error();
// Sending the patient details in the form of an array.
echo json_encode($output_array);

The code below is used to get the details about the patients. We enter the patient ID and it will query the database and return the information in the form of a JSON response.

//Connecting to database
$connection = mysql_connect("localhost", "username", "password");
echo mysql_error();
$db_select = mysql_selectdb("articledb", $connection);
//Getting doctor ID and the message to be sent
$val = $_REQUEST['val'];
$message = $_REQUEST['mess'];
$query = "SELECT * FROM doctor WHERE id=" . $val;
//Getting the Notification ID of the specified doctor.
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$nid = $row['NID'];
//Sending the notification
echo sendNotification("",Service Secret", $nid, $message);
echo mysql_error();
// Function to send the notification using CURL.
function sendNotification($serviceId, $serviceSecret, $notificationId, $message) {
// the host must be used for testing in the sandbox environment
$url = "" . urlencode($notificationId) . "/";
// format the POST parameters
$args = 'payload=' . urlencode($message) .
'&ctype=' . urlencode("text/plain");
// initialize the curl session
$session = curl_init($url);
// set HTTP POST method
curl_setopt($session, CURLOPT_POST, true);
// set the POST parameters
curl_setopt($session, CURLOPT_POSTFIELDS, $args);
// return the HTTP request's response as a string instead of outputting it directly
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// set the authentication method as requested by the Nokia Notification Server
// set the authentication data: service ID and service secret
curl_setopt($session, CURLOPT_USERPWD, $serviceId . ':' . $serviceSecret);
// execute the HTTP request
$output = curl_exec($session);
// get the response HTTP code
$responseCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
$success = ($responseCode == 201);
return $success;

This code is responsible for sending a message to the doctor from the patient. We will first find out the NID (Notification ID) of the doctor by querying the database. The we will use curl and send a notification to the doctor who is registered with that particular NID.

The remaining server side code is similar to the code above. The rest of the code described shows how to send a message from the doctor to the patient, how to register for NID for the doctor and patient etc.

Client-side implementation

We have two different client-side MIDlets - one for the doctor and another for the patients. {{Note|For this simple example, a single MIDlet for both doctor and patients would be reasonable. In a real-world implementation though the information entered would be different so separate midlets have been created.

Patients app

The Patients app is quite simple.It has the two features.One is to register oneself as a new Patient. This gives back a new ID for the patient to identify himself with.The second is the login page.This allows the patient to see his present medications, his history etc.

At the same time, the app registers itself to listen to notifications and also send its Notification ID (NID) to our server so that we can store that in our database.

The implementation is done using LWUIT.

  public void sendRegData(String name, String history, String medicine)
final String post_data = "name=" + name + "&history=" + history + "&medicines=" + medicine;
NetworkManager networkManager = NetworkManager.getInstance();
networkManager.addErrorListener(new ActionListener()
public void actionPerformed(ActionEvent evt)
NetworkEvent n = (NetworkEvent) evt;
System.out.println("Error : " + n.getError().toString());
ConnectionRequest cr;
cr = new ConnectionRequest()
protected void buildRequestBody(OutputStream os) throws IOException
protected void readResponse(InputStream input) throws IOException
System.out.println("Reading data");
String output;
int ch;
StringBuffer sb = new StringBuffer();
while ((ch = != -1)
sb.append((char) ch);
output = sb.toString();
System.out.println("Output :" + output);
if (!output.equals("false"))
if ("Success", "Your ID is :" + output, "Ok", null))
} else
{"Error", "Error occured", "Ok", null);
String url = "http://localhost/register.php";

The above code is used to register the patient with the service. We are sending the details about the user via post. If all the details are entered correctly, the PHP script will put the entries into the database and return the ID of the new patient.

public void processPatientData(Hashtable ht, final String id)
details = new Form("Details");
loggged_in_ID = id;
Label l = new Label("Name");
TextArea ta1 = new TextArea((String) ht.get("name"));
Label l2 = new Label("History");
TextArea ta2 = new TextArea((String) ht.get("history"));
Label l3 = new Label("Medicines");
TextArea ta3 = new TextArea((String) ht.get("medicines"));
System.out.println("Done till here");
Command calldoc = new Command("Call Doctor");
final Command back = new Command("Back");
details.addCommandListener(new ActionListener()
public void actionPerformed(ActionEvent evt)
if (evt.getCommand() == back)
} else
} catch (Exception e)
System.out.println("Error here");

Once we are successfully logged in, the above method is used to process a particular patients details from the JSON and display it. We also register for Notifications for this particular patient who has logged in.

Doctor app

The Doctor's app is similar to the patient's app, except that it can access any patient's record and need not login. This is again accomplished by the use of JSON. The list of patients available are shown in the form of a list. When the ID of any patient is clicked, the details about that patient are shown.

As soon as the doctor's app is opened, it is also registered for notifications and the notification ID of the doctor is sent to the database as well.

Dead Man's Switch

The useful feature of this system is that the patient can contact the doctor directly when they are in crisis. If the patient is starting to feel the symptoms of a heart attack, they can quickly click on the contact doctor command. It will automatically send a message to the doctor, a warning message along with the patient ID. The system can also be tweaked to send this message to all the doctors in the hospital. This way immediate assistance can be given to the patient.

Scope for improvements

The system can be improved upon in many ways

  1. Adding all members of staff - The system describes only a doctor-patient interaction. This can be easily extended to more staff members like Nurse and other hospital fraternity. This can be implemented by creating another table about the other hospital staff and storing their NID as well.
  2. Adding scheduled messages from the doctor's end to the patient. One can easily schedule messages to be delivered to the patient everyday. A likely scenario where this would be very helpful is to create a medication reminder. The advantage of this approach is that the medication reminder need not be implemented in the Client side. One can also schedule messages to be delivered to a patient in the server end itself.
  3. Keeping track of payments, food etc. We can extend this facility to keep track of various other factors of the patient. Notification about due payments, food etc can also be easily added.


This article demonstrates how to create a Patient- Doctor Management System. It also shows how to make sure that the patient can quickly reach out to the doctor in times of danger.

This page was last modified on 21 December 2013, at 18:36.
468 page views in the last 30 days.