This discussion is archived
0 Replies Latest reply: Jan 26, 2010 10:51 PM by 843851 RSS

Bluetooth problems using J2ME for voting system

843851 Newbie
Currently Being Moderated
Hi...

I am now on a project developing an application for mobile devices. A java based voting system through bluetooth. So, i am fully utilized the J2ME and the bluetooth technology. Both Server and Clients are using mobile devices - j2me.

Upon developing the system, i faced some problems that urgently need to be solved faster. The problems are:

1)As a SERVER, it can only RECEIVE...and as a CLIENT, it can only SENDING. How I can make the SERVER to RECEIVE (eg:receive voters' database, voters' voting) and also SEND(eg: send questions and other notifications to clients)..?? And same goes to CLIENT. How I can make it to be able to SEND (database and votes) and RECEIVE(questions)..??

2)How I can make the Server to find the list of Clients first before they can start to communicate (sending+receiving)..??

3)As for current system, I can only make the Admin to be Server/Client.. How I can make voters (other than log in as Admin) to be Client..??

4) When in Synchronize page, after select "OK" for the first time (Bluetooth connection on and server+client communicate), then the application hang and i have to restart it back. How it can be solved...???

5)Before the Server and Client can start to communicate, how I can improve on the security part by providing password before it starts communicate?

Really need helps..
These are the parts that involved the Bluetooth connection:
import java.io.DataInputStream;
import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.midlet.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

//currently doing - profile page
public class BTVoter extends MIDlet implements CommandListener, Runnable
{
     private RecordStore voterRecords, issueRecords;
     private RecordInterface voters = new RecordInterface(voterRecords, "Voters");
     private RecordInterface issue = new RecordInterface(issueRecords, "Issue");
     
     private boolean endNow = false;
     private Thread server = null;
     private LocalDevice localDevice = null;
     private final UUID uuid = new UUID("BAE0D0C0B0A000955570605040302010", false);
     private StreamConnectionNotifier scn = null;
     private Client client = Client.GetInstance();
     ...
        ...
        ...
     public void run() {
          StreamConnection conn = null;
          try {
               localDevice = LocalDevice.getLocalDevice();
               localDevice.setDiscoverable(DiscoveryAgent.GIAC);
               strStatus.setText(localDevice.getFriendlyName() + " is " + strStatus.getText());     
               String url = "btspp://localhost:" + uuid.toString() + ";name=btvoter;authorize=false";;
               scn = (StreamConnectionNotifier)Connector.open(url.toString());               
          } catch (BluetoothStateException e) { System.out.println("Error in run"); 
          } catch (IOException e) { System.out.println("Problem with StreamConnectionNotifier");}     
          
          while (!endNow)
          {
               conn = null;
               try {
                    conn = scn.acceptAndOpen();
               } catch (IOException e) { continue; }
               
               if (conn != null) processRequest(conn);
          }
     }
import java.io.DataOutputStream;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public final class Client implements Runnable
{
     private int mode;
     private RecordInterface voters = null, issue = null;
     
     private Thread clientThread = null;
     private static Client inst = new Client();
     private UUID uuid = new UUID("BAE0D0C0B0A000955570605040302010", false);
     private boolean endNow = false;
     private String service = null;
     private DiscoveryAgent discoveryAgent = null;
        ....
        ....
        ....
        public void run() {
          try {
               discoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
          } catch (BluetoothStateException e) { System.err.println("Error at client - run"); }
          StreamConnection conn = null;
          DataOutputStream dos = null;
          
          while (!endNow)
          {
               try
               {
                    service = discoveryAgent.selectService(uuid, ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                    if (service != null)
                    {
                         conn = (StreamConnection)Connector.open(service);
                         dos = conn.openDataOutputStream();
                         
                         //if you need to send the Issue file...
                         if (mode != 2)
                         {
                              Issue tempIssue = new Issue(issue.Get());
                              //send the issue only, no results
                              if ((mode == 0) || (mode == 3))
                              {
                                   int[] reset = new int[tempIssue.GetOptQty()];
                                   for(int i=0; i<reset.length; i++) reset[i] = 0;
                                   tempIssue.SetVotes(reset);
                              }
                              byte[] sendBytes = tempIssue.ToByte();
                              //first, we need to send info on the length of data we're going to send
                              dos.writeInt(sendBytes.length);
                              dos.write(sendBytes, 0, sendBytes.length);
                         }
                         
                         //now, if you need to send the Voters database...
                         if (mode > 1)
                         {
                              //first, read all the voters into an array
                              Voter[] allVoters = new Voter[voters.GetQty()];
                              for(int i=0; i<allVoters.length; i++) allVoters[i] = new Voter(voters.Get(i+1));
                              
                              //next, send the number of voters to prepare the server
                              dos.writeInt(allVoters.length);
                              //then we send the voters data                              
                              for(int i=0; i<allVoters.length; i++)
                              {
                                   byte[] sendBytes = allVoters.ToByte();
                                   dos.writeInt(sendBytes.length);
                                   dos.write(sendBytes, 0, sendBytes.length);
                              }
                         }
                         
                         dos.flush();
                         dos.close();
                    }
               } catch (Exception e) { System.err.println("Error in client - run 2"); }
          }