Java2 codes,problems ,discussions and solutions are here
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.
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.
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.
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.
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.