Total members 11894 |It is currently Fri Nov 22, 2024 12:46 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.




Author:
Newbie
User avatar Posts: 6
Have thanks: 1 time

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.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

For this message the author DrRakha has received gratitude : ArunaR

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.


Author:
Newbie
User avatar Posts: 6
Have thanks: 1 time

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.


Author:
Newbie
User avatar Posts: 6
Have thanks: 1 time

For this message the author ArunaR has received gratitude : DrRakha
Post new topic Reply to topic  [ 4 posts ] 

  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     -  
 connecting PC with mobile using bluetooth code in java     -  
 how to develop a mobile application for nokia help of java     -  
 java program     -  
 java program     -  
 email through JAVA program     -  
 java chat program.     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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