2 ways communication for Bluetooth connection..

Discussion in 'Java' started by izzahmeor, Feb 3, 2010.

  1. izzahmeor

    izzahmeor

    Joined:
    Jan 27, 2010
    Messages:
    3
    Hi..

    I am now on a final year project to develop an application for mobile devices. My project is “Java based e-voting system through Bluetooth (Mobile-to-Mobile)”. So, I am fully utilized the J2ME and the Bluetooth technology. As for Bluetooth technology, I need at least one Server and several Clients in order to make my program works. For this project, both Server and Clients are using mobile devices.

    As for now, I faced some problems on the Bluetooth connection:

    1) To be a Server or a Client, I need to log in myself as an Admin first, then only I can set myself either to be a Server or a Client. If I log in myself as another person (NOT Admin), I can't even be a Client. How I can make another person (NOT Admin) to be a Client..??

    2) I can be either a Server or a Client when I logged in as an Admin. So, what I did was, in one mobile phone, I logged in as an Admin and set myself as a Server. Another mobile phone, I logged in as an Admin and set myself as a Client. So now, I have a Server and a Client. BUT, this Server and Client can only make a one way communication, which means, Server can only RECEIVE and Client can only SEND.
    How I can make this Server and Client communicate in a TWO ways communication..?? Means that, a Server can SEND & RECEIVE..and same goes to Client which can SEND & RECEIVE too..

    Really needs help from others..
    These are the parts in my project 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 tem****ue = new Issue(issue.Get());
    						//send the issue only, no results
    						if ((mode == 0) || (mode == 3))
    						{
    							int[] reset = new int[tem****ue.GetOptQty()];
    							for(int i=0; i<reset.length; i++) reset[i] = 0;
    							tem****ue.SetVotes(reset);
    						}
    						byte[] sendBytes = tem****ue.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"); }
    		}
     
    izzahmeor, Feb 3, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. barak

    Bluetooth connection closed

    barak, Nov 2, 2004, in forum: Java
    Replies:
    0
    Views:
    347
    barak
    Nov 2, 2004
  2. barak

    Bluetooth connection closed

    barak, Nov 2, 2004, in forum: Java
    Replies:
    0
    Views:
    338
    barak
    Nov 2, 2004
  3. Rainer
    Replies:
    0
    Views:
    6,081
    Rainer
    Jun 11, 2006
  4. arthurclucas@gmail.com

    Bluetooth communication

    arthurclucas@gmail.com, Apr 29, 2008, in forum: C++
    Replies:
    1
    Views:
    401
    Ben Voigt [C++ MVP]
    Apr 29, 2008
  5. izzahmeor
    Replies:
    0
    Views:
    795
    izzahmeor
    Feb 3, 2010
Loading...

Share This Page