Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Java Program for communicate PC and Mobile

Wed Dec 07, 2011 5:34 am

Hello Friends,

I'm working on writing a Java program for communicate a PC and Mobile device via Bluetooth port.
I used JSR082 APIs for java and Netbeans 6.7 IDE. The server code was written using Java SE project and following is the code I have written.



import javax.microedition.io.*;
import javax.bluetooth.*;

public class ServiceBrowser implements DiscoveryListener {
static int SERVICE_NAME_ATTRID = 0x0100 ;
RemoteDevice discovered[] = new RemoteDevice[255];
int num_discovered ;

public void deviceDiscovered( RemoteDevice rd , DeviceClass cod ) {
String addr = rd.getBluetoothAddress();
String name = " ";
try {
name = rd.getFriendlyName(true );
} catch( java.io.IOException e ) {}

this.discovered[this.num_discovered] = rd ;
this.num_discovered++;
System.out.println( "discovered " + addr + " - " + name );
}

public void inquiryCompleted(int status){
System.out.println( "device inquiry complete" ) ;
synchronized(this) {
try {
this.notifyAll ();
} catch(Exception e) {}
}
}

public void servicesDiscovered(int transID , ServiceRecord[] rec ){
for( int i=0; i<rec.length ; i++ ) {
DataElement d = rec[i].getAttributeValue(SERVICE_NAME_ATTRID);
if (d != null) System.out.println((String)d.getValue());
else System.out.println( "unnamed service" ) ;
System.out.println(rec[i].getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT, false));
}
}

public void serviceSearchCompleted( int transID , int respCode ) {
System.out.println( "service search complete" );
synchronized(this) {
try {this.notifyAll();}
catch(Exception e) {}
}
}

public static void main(String argv[ ] ) {
try {
ServiceBrowser sb = new ServiceBrowser();
LocalDevice ld = LocalDevice.getLocalDevice();
DiscoveryAgent da = ld.getDiscoveryAgent();
sb.num_discovered = 0;
da.startInquiry(DiscoveryAgent.GIAC, sb);
synchronized(sb) {
try {sb.wait(); }
catch(Exception e) {}
}
// service search
UUID uuids[] = new UUID[1];
uuids[0] = new UUID(0x1002);
int attridset[] = new int[1];
attridset[0] = SERVICE_NAME_ATTRID;
for( int i=0; i<sb.num_discovered ; i++ ) {
RemoteDevice rd = sb.discovered[i];
da.searchServices(attridset , uuids , rd , sb ) ;
synchronized(sb) {
try { sb.wait();}
catch( Exception e ) {}
}
}
} catch( BluetoothStateException e ) {
System . out . print(e . toString ( ) ) ;
}
}
}



When compiling the above code I got a exception error at

LocalDevice ld = LocalDevice.getLocalDevice();

Error: javax.bluetooth.BluetoothStateException

I'm rather new to Java and J2ME programming so it's great help for me if anybody can help this regards.


ArunaR.



Re: Java Program for communicate PC and Mobile

Wed Dec 07, 2011 7:08 pm

The BluetoothStateException is thrown when a request is made to the Bluetooth system that the system cannot support in its present state. If, however, the Bluetooth system was not in this state, it could support this operation. For example, some Bluetooth systems do not allow the device to go into inquiry mode if a connection is established. This exception would be thrown if startInquiry() were called.

can you please put more log for your exception ?

Are you sure that you have Bluetooth driver in your PC.

Re: Java Program for communicate PC and Mobile

Thu Dec 08, 2011 5:15 am

Hi,

Following are some more exceptions that also relevant to same but it's not for above program.
Actually it's your program that was given in thread "Connecting to PC from mobile using bluetooth in java"

javax.bluetooth.BluetoothStateException
at com.sun.kvem.jsr082.bluetooth.LocalDeviceImpl.<init>(LocalDeviceImpl.java:185)
at javax.bluetooth.LocalDevice.getLocalDevice(LocalDevice.java:94)
at mscproject.PCServerComm.<init>(PCServerComm.java:46)
at mscproject.MscProject.main(MscProject.java:24)


I have taken above error from DELL latitude D620 which is having Bluetooth functionality. I was able to communicate my mobile phone via the same Bluetooth in this machine.

Best regards,
Aruna.

Re: Java Program for communicate PC and Mobile

Tue Dec 13, 2011 11:40 am

Hi,

I was able to solve the above problem with bluecove-2.1.0 library.
Previously I have user bluecove-2.0.0 library which gave the above errors.

Best Regards,
Aruna.

Post a reply
  Related Posts  to : Java Program for communicate PC and Mobile
 mobile to pc in bluetooth in java     -  
 java mobile apps     -  
 Connecting to PC from mobile using bluetooth in java     -  
 connecting to pc from mobile using bluetooth in java     -  
 how to develop a mobile application for nokia help of java     -  
 connecting PC with mobile using bluetooth code in java     -  
 java program     -  
 java program     -  
 Executing a Java Program     -  
 Elements of a Java Program     -