×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    Question Bluetooth problems using J2ME for voting system

    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...???

    Really need helps..
    These are the parts that involved the Bluetooth connection:


    Code:
    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);
    		}
    	}

    Code:
    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[i].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"); }
    		}

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    Does this help?

    To be honest, I do not understand one of your questions. Perhaps this happens, because you use terms which I match to other subjects. Instead of explaining me your problem in more detail, I recommend to start with a book or tutorials rather than code examples as you will have to debug a lot on your own. With Bluetooth, there are a lot of bugs to overcome, tweaks must be applied and lessons have to be learnt. I do not have a good J2ME book at hand right now, however, start with the references of this one for JSR-82 (perhaps your library has its previous edition) or read at least the PDF coming with the specification. If you like to start with tutorials, continue here…

Similar Threads

  1. Bluetooth streaming connection in J2ME
    By Chathurika_PK in forum Bluetooth Technology
    Replies: 2
    Last Post: 2009-03-26, 12:58
  2. Can discover device on J2ME via bluetooth?
    By kevin_ca in forum Bluetooth Technology
    Replies: 1
    Last Post: 2009-02-18, 10:49
  3. Problems about maemo 4.0.1 SDK installation.
    By fgg_maemo in forum General Development Questions
    Replies: 3
    Last Post: 2008-10-07, 14:26
  4. Bluesoleil Software
    By kencooper in forum Bluetooth Technology
    Replies: 9
    Last Post: 2008-08-29, 19:46
  5. 6131 bluetooth to pc problems
    By Ghost_of_Carl in forum Bluetooth Technology
    Replies: 2
    Last Post: 2007-08-21, 16:46

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×