Post subject: Connecting to PC from mobile using bluetooth in java
Posted: Fri Jul 25, 2008 6:43 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 1573 Location: Earth Has thanked: 10 time Have thanks: 16 time
I will talk about how to make data transfer from your mobile to your PC .The mobile side will be a j2me application.The server side is J2se application .To make such communication using bluetooth you need to open a virtual COM in your pc and assign this virtual COM to your paired mobile .Paired devices means an same PIN Code entered in both sides (mobile and PC ) .In my case i worked on bluecove external adapter .There is an application for bluecove device that allow me to assign COMs to a specific mobile.In my system i was sending images from my phone to PC server using bluetooth.
In server side : You will need some packages to make the work done .in my case i used two packages bluecove-2.0.2.jar and avetanaObex.jar . Here is code from it
/** * A class that demonstrates Bluetooth communication between server mode PC and * client mode device through serial port profile. * * @see <a href="http://sourceforge.net/projects/bluecove/">BlueCove - JSR-82 * implementation</a> */ public class PCServerCOMM {
/*- * ================ * Bluetooth Server * ================ * * This example application is a straighforward implementation of * a bluetooth server. * * * Usage * ===== * * Start the program. Events are logged by printing them out with standard * output stream. Once the server is up and ready to accept connections, * connect to server with client. * * * How it Works * ============ * * The application starts a loop to wait for a new connection. After a new * connection is reseived the connection is handled. In the handling * operation few tokens and end token is written to connection stream. * Each read token is logged to standard output. After handling session * the loop continues by waiting for a new connection. * */
/*- * * ---- Bluetooth attributes ---- */
// serial port profile protected String UUID = new UUID("1101", true).toString(); protected int discoveryMode = DiscoveryAgent.GIAC; // no paring needed
} if (!falg && (r < s.length)) { // System.out.println("Frame !!!"); f1.flush(); f1.close(); aa = new ImageIcon(path).getImage(); new FrameImage(aa, 1);
/** * A class that demonstrates Bluetooth communication between client mode device * and server mode PC through serial port profile. */ public class DeviceClientCOMM implements DiscoveryListener, CommandListener {
/*- * ================ * Bluetooth Client * ================ * * This example application is a straightforward implementation of * making a connection throught bluetooth link and using it. * * * Usage * ===== * * Once the system is started the information area is filled with logging * information. Wait until combo box is enabled (if there's any bluetooth * device around) and select a device from the combo box. A connection * is constructed to selected device after selection. * * After connection is constructed succesfully the application * operates as an echo client, i.e., everything that is read is written * back to the server. All information is also written into the information * area. * * * How it Works * ============ * * The example consist of three different operations. Operations need to * be executed in the dependency order. * * 1) *Inquiry method* is used to search all available devices. The combo * box's data model is filled with found bluetooth devices. * * 2) *Service search* is used to search serial port profile service from the * selected device. The search is started after the user selects the * device from the combo box. * * 3) *Stream handling* communicates with the server through the bluetooth * link. This example operates in echo loop until stop token is found. * * * Special Debug Mode * ================== * * There's a special debug mode which speeds up development by skipping the * inquiry method to resolve the remote device. In the debug mode the device's * bluetooth address is provided by the developer. */
public void inquiryCompleted(int discType) { log("Inquiry compeleted. Please select device from combo box."); makeDeviceSelectionGUI(); }
/*- * ------- Service search section ------- */ private void startServiceSearch(RemoteDevice device) { try { log("Start search for Serial Port Profile service from " + getDeviceStr(device)); UUID uuids[] = new UUID[]{uuid}; getAgent().searchServices(null, uuids, device, this); } catch (Exception e) { log(e); } }
/** * This method is called when a service(s) are discovered.This method starts * a thread that handles the data exchange with the server. */ public void servicesDiscovered(int transId, ServiceRecord[] records) { log("Service discovered."); for (int i = 0; i < records.length; i++) { ServiceRecord rec = records[i]; String url = rec.getConnectionURL(connectionOptions, false); handleConnection(url); } }
public void serviceSearchCompleted(int transID, int respCode) { String msg = null; switch (respCode) { case SERVICE_SEARCH_COMPLETED: msg = "the service search completed normally"; break; case SERVICE_SEARCH_TERMINATED: msg = "the service search request was cancelled by a call to DiscoveryAgent.cancelServiceSearch()"; break; case SERVICE_SEARCH_ERROR: msg = "an error occurred while processing the request"; break; case SERVICE_SEARCH_NO_RECORDS: msg = "no records were found during the service search"; break; case SERVICE_SEARCH_DEVICE_NOT_REACHABLE: msg = "the device specified in the search request could not be reached or the local device could not establish a connection to the remote device"; break; } log("Service search completed - " + msg); }
/*- * ------- The actual connection handling. ------- */ private void handleConnection(final String url) { Thread echo = new Thread() {
public void run() { StreamConnection stream = null; InputStream in = null; OutputStream out = null;
try { log("Connecting to server by url: " + url); stream = (StreamConnection) Connector.open(url);
log("Bluetooth stream open."); // InputStream in = stream.openInputStream(); out = stream.openOutputStream(); in = stream.openInputStream(); startReadThread(in); // String stringImage = Base64.encode(imag); log("Start echo loop."); out.write(imag); out.flush(); // out.flush();
private void makeDeviceSelectionGUI() { final List devices = new List("Select a device", List.IMPLICIT); for (int i = 0; i < deviceList.size(); i++) { devices.append( getDeviceStr((RemoteDevice) deviceList.elementAt(i)), null); } devices.setCommandListener(new
Post subject: Re: Connecting to PC from mobile using bluetooth in java
Posted: Tue Jan 27, 2009 10:26 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 1573 Location: Earth Has thanked: 10 time Have thanks: 16 time
Hi ,thank you for your reply these lines of codes not related to the connection of bluetooth , it is related to my project function , for example , SIFT is an object recognition algorithm .
_________________ Currenlty programming with : java , html , php , and javascript .
Post subject: Re: Connecting to PC from mobile using bluetooth in java
Posted: Sat Apr 18, 2009 8:59 pm
Joined: Sat Apr 18, 2009 8:07 pm Posts: 4 Has thanked: 0 time Have thanks: 0 time
please help meeeeeeeee im an a graduate student and part of my final project is to transfer a text file from a pc to mobile
if we went to transfer text file the uuid not mention and i not found it although i search a lot on it please help me if you can and the reminder time is very little
Post subject: Re: Connecting to PC from mobile using bluetooth in java
Posted: Mon Apr 27, 2009 8:34 am
Joined: Mon Apr 27, 2009 8:19 am Posts: 1 Has thanked: 0 time Have thanks: 0 time
hi msi_333 ,
Nice post ! I am facing an small problem with a similar code for just text transfer , I have a bluesoliel stack and bluecove library . when i run the application the device gets discovered fromthe client and stack gets initialized and the window pops up asking for permission to open up virtual com ports but the application still ends in a loop waiting for connection , when i use hyperterminal instead of the app i get read n write back in the client .
Post subject: Re: Connecting to PC from mobile using bluetooth in java
Posted: Thu Oct 22, 2009 6:46 pm
Joined: Thu Oct 22, 2009 6:29 pm Posts: 2 Has thanked: 0 time Have thanks: 0 time
hi friend.....can u pls send me the whole program? hope u will grant my wish....i am a student willing to learn the know-hows of bluetooth. [gbrago@yahoo.com]
Post subject: Re: Connecting to PC from mobile using bluetooth in java
Posted: Sat Nov 07, 2009 5:30 am
Joined: Tue Nov 03, 2009 1:00 pm Posts: 3 Has thanked: 2 time Have thanks: 0 time
HI Msi_33
plz you send code of whole program .i am student .i am very much interested in computer program to know some good good program so that my knowlegage became good in computer
so plz you send me this complete code and some of brief description to my email id : boltthepower@gmail.com
Users browsing this forum: Google Adsense [Bot] and 3 guests
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum