Google

Monday, January 19, 2009

Bluetooth on J2SE

http://bluecove.sourceforge.net/

Quick Tutorials

How to quickly install and test your bluetooth
J2SE capability.

1. Download bluecove-2.1.0.jar from http://code.google.com/p/bluecove/

2. Open Netbeans, goto Libraries Menu and add new library.
Navigate to the bluecone-2.1.0.jar and add it as a jar library.
Do not uncompress it. Call it Bluecove in the textfield descriptor.


3. Create a new Java application project and paste this code in:

/**
* Minimal Device Discovery example.
*
* http://bluecove.sourceforge.net/apidocs/overview-summary.html#DeviceDiscovery
*/
package bluetoothremotedevicediscovery;

import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.*;

public class RemoteDeviceDiscovery {

public static final Vector/**/ devicesDiscovered = new Vector();

public static void main(String[] args) throws IOException, InterruptedException {

final Object inquiryCompletedEvent = new Object();

devicesDiscovered.clear();

DiscoveryListener listener = new DiscoveryListener() {

public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
devicesDiscovered.addElement(btDevice);
try {
System.out.println(" name " + btDevice.getFriendlyName(false));
} catch (IOException cantGetDeviceName) {
}
}

public void inquiryCompleted(int discType) {
System.out.println("Device Inquiry completed!");
synchronized (inquiryCompletedEvent) {
inquiryCompletedEvent.notifyAll();
}
}

public void serviceSearchCompleted(int transID, int respCode) {
}

public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
}
};

synchronized (inquiryCompletedEvent) {
boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
if (started) {
System.out.println("wait for device inquiry to complete...");
inquiryCompletedEvent.wait();
System.out.println(devicesDiscovered.size() + " device(s) found");
}
}
}
}


4. Edit your Project properties Select the Library and then
select the Compile Tab and click on Add Library. Select the Bluecove
library.

5. Just run it and watch the output window. Make sure your bluetooth
is turned on and also your Mobile phone is turned on. It will be
able to detect your mobile phone.


Bluetooth JSR82 Samples:
http://www.jsr82.com/