2

I'm building simple java server that communicates with android device (as a client). Currently I'm able to send messages from my phone (client) to my pc (server) via bluetooth. Problem is that I can't send messages from server back to client. I'm using bluecave library. Here's my code

public class MainTest {
    UUID uuid = new UUID("8848",true);
    public static void main(String[] args) {
        LocalDevice local = null;
        try {
            local = LocalDevice.getLocalDevice();
        } catch (BluetoothStateException e) {
            e.printStackTrace();
        }
        System.out.println("Serverted:\n" +local.getBluetoothAddress() +"\n"+local.getFriendlyName());
        MainTest ff = new MainTest();
        while (true) {
            ff.startserver();
        }
    }

    public void startserver() {
        try {
            String url = "btspp://localhost:" + uuid + ";name=File Server";
            StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open( url );

            StreamConnection con = service.acceptAndOpen();
            OutputStream dos = con.openOutputStream();
            InputStream dis = con.openInputStream();

            while (true) {
                byte buffer[] = new byte[1024];
                int bytes_read = dis.read( buffer );
                String received = new String(buffer, 0, bytes_read);
                System.out.println("Message:"+ received);

                if("a".equals(received)) {
                    dos.write("sdfsd".getBytes());
                    dos.flush();
                }
            }
            // con.close();
        } catch ( IOException e ) {
            System.err.print(e.toString());
        }
    }

I also tried to use PrintWriter updated code, but still no response...

public static void startserver() {
        try {
            String url = "btspp://localhost:" + uuid + ";name=TTT";
            StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open( url );

            StreamConnection con = service.acceptAndOpen();
            DataOutputStream dos = con.openDataOutputStream();
            InputStream dis = con.openInputStream();
            PrintWriter pWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(dos)), true);
            while (true) {
                byte buffer[] = new byte[10];
                int bytes_read = dis.read(buffer);
                String received = new String(buffer, 0, bytes_read);
                System.out.println("Message:"+ received);

                pWriter.write("testString");
                pWriter.flush();

            }
           // pWriter.close();
           // con.close();


            // con.close();
        } catch ( IOException e ) {
            System.out.println(e.getMessage());
        }
    }
David
  • 3,055
  • 4
  • 30
  • 73
  • Possible duplicate of [Send text through Bluetooth from Java Server to Android Client](http://stackoverflow.com/questions/10929767/send-text-through-bluetooth-from-java-server-to-android-client) – colin aygalinc Nov 23 '16 at 09:20

1 Answers1

0

The following answer solve the problem to send text message from server to android client : Send text through Bluetooth from Java Server to Android Client

Maybe you can use it in your case.

Community
  • 1
  • 1
colin aygalinc
  • 457
  • 4
  • 13