This discussion is archived
7 Replies Latest reply: Jan 10, 2013 11:37 AM by rukbat RSS

connecting bluetooth device to java

984040 Newbie
Currently Being Moderated
hi i am a final year student and for my project i need to connect this bluetooth device to java so it can read and write the data. with this data i then have to visualise it by using GUI. can someone please help me as i cant seem to find code which will enable me to connect the bluetooth device to java and then read and write the data on screen. if someone could help i would very much appreciate it. supplying code will be helpful as i dont have much time and not a good programmer.
  • 1. Re: connecting bluetooth device to java
    DarrylBurke Guru Moderator
    Currently Being Moderated
    981037 wrote:
    hi i am a final year student and for my project i need to connect this bluetooth device to java so it can read and write the data. with this data i then have to visualise it by using GUI. can someone please help me as i cant seem to find code which will enable me to connect the bluetooth device to java and then read and write the data on screen. if someone could help i would very much appreciate it. supplying code will be helpful as i dont have much time and not a good programmer.
    You need a Bluetooth API (JSR-82) implementation for your particular OS and BT hardware. One free download I'm aware of is BlueCove.

    And this is a forum not a free code service. YOU have to write the code; if you face any specific problems feel free to ask a specific question and you'll get help with that.

    I'm moving this thread to New to Java.

    db
  • 2. Re: connecting bluetooth device to java
    984040 Newbie
    Currently Being Moderated
    i have managed to find this piece of code but do not have the bluetooth device to currently check whether it works..does it look correct:


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

    /**
    * Minimal Device Discovery example.
    */
    public class RemoteDeviceDiscovery {

    public static final Vector/*<RemoteDevice>*/ 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");
    }
    }
    }

    }
  • 3. Re: connecting bluetooth device to java
    Kayaman Guru
    Currently Being Moderated
    981037 wrote:
    i have managed to find this piece of code but do not have the bluetooth device to currently check whether it works..does it look correct:
    It looks like you're going to fail your project.
  • 4. Re: connecting bluetooth device to java
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Cross posted?
    http://stackoverflow.com/questions/14234064/connecting-bluetooth-device-to-my-java-code

    db
  • 5. Re: connecting bluetooth device to java
    DarrylBurke Guru Moderator
    Currently Being Moderated
    981037 wrote:
    i have managed to find this piece of code but do not have the bluetooth device to currently check whether it works..does it look correct:


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

    /**
    * Minimal Device Discovery example.
    */
    public class RemoteDeviceDiscovery {

    public static final Vector/*<RemoteDevice>*/ 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");
    }
    }
    }

    }
    Announcements at the top of the forum listings are meant to be read. Clearly, you didn't bother.

    db
  • 6. Re: connecting bluetooth device to java
    984040 Newbie
    Currently Being Moderated
    And the point of that comment was? I will not fail my project and i was asking for help, so why don't you go and act like a girl somewhere else or do something productive. You should be banned from this forum, most idiotic response ive ever seen on a forum. terrible.
  • 7. Re: connecting bluetooth device to java
    rukbat Guru Moderator
    Currently Being Moderated
    Moderator Action:
    Thread locked.

    You were supposed to realize you were being pointed to where you could learn how to post to the forum, including how to format your code. You responded with invectives, instead.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points