2 ways communication for Bluetooth connection..


Joined
Jan 27, 2010
Messages
3
Reaction score
0
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"); }
		}
 
Ad

Advertisements


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top