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 4 of 4

Thread: Bluetooth Client & Server

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Bluetooth Client & Server

    I've got 2 classes. A bluetooth client class which will be run on my laptop and a bluetooth server class which will be run on a pc. Apparently, I keep having this error and I could use some help...

    Exception in thread "main" java.io.IOException: Can't query remote device
    BlueCove stack shutdown completed
    at com.intel.bluetooth.RemoteDeviceHelper.getFriendly Name(RemoteDeviceHelper.java:404)
    at javax.bluetooth.RemoteDevice.getFriendlyName(Remot eDevice.java:130)
    at com.main.BluetoothClient.main(BluetoothClient.java :75)
    package com.main;
     
    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 BluetoothClient 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 {
     
        	BluetoothClient client=new BluetoothClient();
     
            //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(index-1);
            UUID[] uuidSet = new UUID[1];
            uuidSet[0]=new UUID("1101",true);
            int[] attrSet={0x1101};
     
            System.out.println("\nSearching for service...");
            agent.searchServices(attrSet,uuidSet,remoteDevice,client);
     
            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
            try{
            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);
            }catch(Exception e){
            	//e.printStackTrace();
            }
     
     
        }//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) {
            System.out.println("Inside Service Discovered");
            if(servRecord!=null && servRecord.length>0){
                connectionURL=servRecord[0].getConnectionURL(0,false);
                for (int i = 0; i < servRecord.length; i++) {
     
                	DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
                	System.out.println("service " + serviceName.getValue() + " found ");
                }
    /*
            	for (int i = 0; i < servRecord.length; i++) {
                    String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                   // System.out.println("Connection URL="+url);
                    if (url == null) {
                        continue;
                    }
                   // serviceFound.add(url);
                    DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
                    if (serviceName != null) {
    					System.out.println("service " + serviceName.getValue() + " found " + url);
    	               // if(serviceName.getValue().equals("OBEX Object Push")){
    	                   // sendMessageToDevice(url);                
    	                //}
     
                    } else {
                        System.out.println("service found " + url);
                    }
                }*/
            }
            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
     
     
     
    }

    package com.main;
     
    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 javax.bluetooth.*;
    import javax.microedition.io.*;
     
    /**
    * Class that implements an SPP Server which accepts single line of
    * message from an SPP client and sends a single line of response to the client.
    */
    public class BluetoothServer {
     
        //start server
        private void startServer() throws IOException{
     
            //Create a UUID for SPP
            UUID uuid = new UUID("1101", true);
            //Create the servicve url
            String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
     
            //open server url
            StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
     
            //Wait for client connection
            System.out.println("\nServer Started. Waiting for clients to connect...");
            StreamConnection connection=streamConnNotifier.acceptAndOpen();
     
            RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
            System.out.println("Remote device address: "+dev.getBluetoothAddress());
            System.out.println("Remote device name: "+dev.getFriendlyName(true));
     
            //read string from spp client
            InputStream inStream=connection.openInputStream();
            BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
            String lineRead=bReader.readLine();
            System.out.println(lineRead);
     
            //send response to spp client
            OutputStream outStream=connection.openOutputStream();
            PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
            pWriter.write("Response String from SPP Server\r\n");
            pWriter.flush();
     
            pWriter.close();
            streamConnNotifier.close();
     
        }
     
     
        public static void main(String[] args) throws IOException {
     
            //display local device address and name
            LocalDevice localDevice = LocalDevice.getLocalDevice();
            System.out.println("Address: "+localDevice.getBluetoothAddress());
            System.out.println("Name: "+localDevice.getFriendlyName());
     
            BluetoothServer sampleSPPServer=new BluetoothServer();
            sampleSPPServer.startServer();
     
        }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bluetooth Client & Server

    Hi ,
    it may be the device is incompatible with your server. try to test it with the some device. hope this help you

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Bluetooth Client & Server

    @oumaya, please note the date on this thread - its over 2 years old. Please avoid resurrecting posts that haven't seen activity in years - there are plenty of more current and active topics.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bluetooth Client & Server

    ok , sorry .

Similar Threads

  1. server client upercase
    By Hokorippoi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2011, 04:45 PM
  2. client/server socket
    By Java_dude_101 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 18th, 2011, 01:16 AM
  3. client server communication
    By Brt93yoda in forum Java Theory & Questions
    Replies: 4
    Last Post: September 2nd, 2010, 04:49 PM
  4. Client/Server
    By Dillz in forum Paid Java Projects
    Replies: 2
    Last Post: June 2nd, 2010, 05:19 AM
  5. [SOLVED] The concept of Server and Client
    By rendy.dev in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2010, 04:13 AM

Tags for this Thread