Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Bluetooth Client

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Den Helder, The Netherlands
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Bluetooth Client

    I am a beginner on java programming and i have a problem.
    I use a bluetooth module (dwengo) in combination with a microcontroller (PIC16F877A)
    I want write a bluetooth client program in java to communicate with the microcontoller
    Examples of clients on internet do not work or give errors I cant'n resolve.
    There is one program ( SampleSPPClient ) that give no errors and shows the bluetooth devices present.
    But when I choose my bluetooth module I got an error (Device does not support Simple SPP Service )
    I think that I have to use an other service of bluetooth but I don't know how to implement
    I would be very greetfull if somebody can help me.

    Here is the program I tried to use

     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.util.Vector;
     
    import javax.bluetooth.DataElement;
    import javax.bluetooth.DeviceClass;
    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.DiscoveryListener;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.RemoteDevice;
    import javax.bluetooth.ServiceRecord;
    import javax.bluetooth.UUID;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
     
    /**
    * A simple SPP client that connects with an SPP server
    */
    public class SampleSPPClient implements DiscoveryListener{
     
    //object used for waiting
    private static Object lock=new Object();
     
    //vector containing the devices discovered
    private static Vector vecDevices=new Vector();
     
    private static String connectionURL=null;
     
    public static void main(String[] args) throws IOException {
     
    SampleSPPClient client=new SampleSPPClient();
     
    //display local device address and name
    LocalDevice localDevice = LocalDevice.getLocalDevice();
    System.out.println("Address: "+localDevice.getBluetoothAddress());
    System.out.println("Name: "+localDevice.getFriendlyName());
     
    //find devices
    DiscoveryAgent agent = localDevice.getDiscoveryAgent();
     
    System.out.println("Starting device inquiry...");
    agent.startInquiry(DiscoveryAgent.GIAC, client);
     
    try {
    synchronized(lock){
    lock.wait();
    }
    }
    catch (InterruptedException e) {
    e.printStackTrace();
    }
     
    System.out.println("Device Inquiry Completed. ");
     
    //print all devices in vecDevices
    int deviceCount=vecDevices.size();
     
    if(deviceCount <= 0){
    System.out.println("No Devices Found .");
    System.exit(0);
    }
    else{
    //print bluetooth device addresses and names in the format [ No. address (name) ]
    System.out.println("Bluetooth Devices: ");
    for (int i = 0; i <deviceCount; i++) {
    RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i) ;
    System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(true)+")");
    }
    }
     
    System.out.print("Choose Device index: ");
    BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
     
    String chosenIndex=bReader.readLine();
    int index=Integer.parseInt(chosenIndex.trim());
     
    //check for spp service
    RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(in dex-1);
    UUID[] uuidSet = new UUID[1];
    uuidSet[0]=new UUID("446118f08b1e11e29e960800200c9a66", false);
     
    System.out.println("\nSearching for service...");
    agent.searchServices(null,uuidSet,remoteDevice,cli ent);
    try {
    synchronized(lock){
    lock.wait();
    }
    }
    catch (InterruptedException e) {
    e.printStackTrace();
    }
     
    if(connectionURL==null){
    System.out.println("Device does not support Simple SPP Service.");
    System.exit(0);
    }
     
    //connect to the server and send a line of text
    StreamConnection streamConnection=(StreamConnection)Connector.open( connectionURL);
     
    //send string
    OutputStream outStream=streamConnection.openOutputStream();
    PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
    pWriter.write("Test String from SPP Client\r\n");
    pWriter.flush();
     
     
    //read response
    InputStream inStream=streamConnection.openInputStream();
    BufferedReader bReader2=new BufferedReader(new InputStreamReader(inStream));
    String lineRead=bReader2.readLine();
    System.out.println(lineRead);
    }//main
     
    //methods of DiscoveryListener
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
    //add the device to the vector
    if(!vecDevices.contains(btDevice)){
    vecDevices.addElement(btDevice);
    }
    }
     
    //implement this method since services are not being discovered
    public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
    if(servRecord!=null && servRecord.length>0){
    connectionURL=servRecord[0].getConnectionURL(0,false);
    }
    synchronized(lock){
    lock.notify();
    }
    }
     
    //implement this method since services are not being discovered
    public void serviceSearchCompleted(int transID, int respCode) {
    synchronized(lock){
    lock.notify();
    }
    }
     
    public void inquiryCompleted(int discType) {
    synchronized(lock){
    lock.notify();
    }
     
    }//end method
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Bluetooth Client

    Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Den Helder, The Netherlands
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Bluetooth Client

    Sorry, is there a way to correct this or do I have to make a new Thread ?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Bluetooth Client

    Do you see an "Edit Post" label at the bottom of the post you'd like to change? If so, press that and edit away.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Location
    Den Helder, The Netherlands
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Bluetooth Client

    Sorry, this is my first experience with a forum. I have at the beginning the javacode [highlight=Java] and [highlight] at the end inserted but I see no change.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Bluetooth Client

    Very close. The ending tag is [/highlight], with the '/' at the beginning.

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Location
    Den Helder, The Netherlands
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Bluetooth Client

    Thanks

Similar Threads

  1. Bluetooth Client & Server
    By SarahVeni in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 5th, 2013, 03:47 AM
  2. how to pair bluetooth devices....
    By kumar.dinesh7 in forum Java SE APIs
    Replies: 0
    Last Post: March 11th, 2013, 02:39 AM
  3. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  4. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  5. Bluetooth support on JAVA SE.
    By atar in forum Java Theory & Questions
    Replies: 2
    Last Post: April 3rd, 2012, 04:08 PM