Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > 2 ways communication for Bluetooth connection..

Reply
Thread Tools

2 ways communication for Bluetooth connection..

 
 
izzahmeor izzahmeor is offline
Junior Member
Join Date: Jan 2010
Posts: 3
 
      02-03-2010
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"); }
		}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bluetooth communication arthurclucas@gmail.com C++ 1 04-29-2008 10:36 PM
how to add a bluetooth device on a bluetooth laptop =?Utf-8?B?ZmlmbzYxMQ==?= Wireless Networking 2 07-05-2006 08:53 PM
Bluetooth-Communication J2SE <--> J2ME Rainer Java 0 06-11-2006 08:54 AM
Bluetake BT009Si Bluetooth USB Adapter and BT400G5 Bluetooth Silverstrand Front Page News 0 09-28-2005 08:25 PM
Logitech Bluetooth headset says I have wrong Bluetooth Stack =?Utf-8?B?U3Byb3h0b24=?= Wireless Networking 0 09-04-2005 10:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57