0

When I put inLoop() method in a while loop, then the server never runs.

It terminates but I want the server to continuously send bytes to the client again and again.

How do I do that ?

SimpleSPPServer -

public class SimpleSPPServer {
    //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();

        while(true)
        {
            inLoop(connection);
        }
    }

    public void inLoop(StreamConnection connection) throws IOException
    {
        RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
        System.out.println("Remote device address: "+dev.getBluetoothAddress());
        System.out.println("Remote device name: "+dev.getFriendlyName(true));

         //send response to spp client
         OutputStream outStream=connection.openDataOutputStream();

         DataOutputStream mmOutStream = new DataOutputStream(outStream); 

         byte[] bb1 = new byte[] {  
                 (byte)0x7D, (byte)0x43, (byte)0x92, (byte)0x05, (byte)0x0E, 
                 (byte)0x2E, (byte)0x0A };

         System.out.println("Writing Bytes...");

            try 
            {
                //mmOutStream.writeByte(bb[k]);
                mmOutStream.write(bb1, 0, bb1.length);

                Thread.sleep(5000);

                //mmOutStream.write(bb2, 0, bb2.length);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            catch (NullPointerException e) 
            {
            } catch (Exception e) {
                e.printStackTrace();
            }

        PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(mmOutStream));
        try {
            mmOutStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        pWriter.flush();

        pWriter.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());

        SimpleSPPServer sampleSPPServer=new SimpleSPPServer();
        sampleSPPServer.startServer();

    }

Exception -

Exception in thread "main" java.io.IOException: Stream cannot be reopened
    at com.intel.bluetooth.BluetoothRFCommConnection.openOutputStream(BluetoothRFCommConnection.java:190)
    at com.intel.bluetooth.BluetoothRFCommConnection.openDataOutputStream(BluetoothRFCommConnection.java:213)
    at SimpleSPPServer.inLoop(SimpleSPPServer.java:52)
    at SimpleSPPServer.startServer(SimpleSPPServer.java:41)
    at SimpleSPPServer.main(SimpleSPPServer.java:104)
sjain
  • 23,126
  • 28
  • 107
  • 185
  • If the stream cannot be reopened then why are you doing so? – greenapps Dec 05 '14 at 08:06
  • @greenapps - `If the stream cannot be reopened then why are you doing so?` - I want to process same bytes on each iteration. So I am doing like that. Please tell any way to process bytes continuously in a loop without reopen error. – sjain Dec 05 '14 at 08:26
  • Why would one process the `same bytes` more than once? – greenapps Dec 05 '14 at 08:49
  • @greenapps - There is a timely creation of records into the database. So same bytes are processed and when time reaches some threshold, records are created. This is just an idea. Real time logic is even more complex. – sjain Dec 05 '14 at 08:57

1 Answers1

0

Take the opening and closing of the stream out of doLoop(). Open the stream before you start the loop with while. Close the stream never. Or when the loop is done.

greenapps
  • 11,154
  • 2
  • 16
  • 19