简体   繁体   中英

Send String through Bluetooth From Java Server to Android Client

I got a problem regarded with send data from server to client using Bluetooth.

I want to send 360 string from PC server to Android client every 5 second. But i can only send one string especially after '\\n', then server is closed. Could anyone help me to understand this code? actually I just get this code from here .

This is my server code.

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

    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

    **String message = "-0.145 \n";
    byte[] msgBuffer = message.getBytes();
    OutputStream outStream=connection.openOutputStream();
    outStream.write(msgBuffer);**

    //PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
    //pWriter.write("Response String from SPP Server\r\n");
    //pWriter.flush();

    //pWriter.close();
    streamConnNotifier.close();

}

Basically I want to modified the bold part into this one:

Byte [] msgBuffer;
OutputStream outStream;
for(int i=0;i<5;i++)
{
String message = "-0.145" + i;
msgBuffer = message.getBytes();
outStream=connection.openOutputStream();
outStream.write(msgBuffer);
}

But it cannot accept the multiple data. This is my client side:

try {
  inStream = btSocket.getInputStream();
  //outStream = btSocket.getOutputStream();

  BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
  String lineRead = bReader.readLine();

  newNumber = Double.valueOf(lineRead);

  numbertime = (double) 0;

DataPoint v = new DataPoint(numbertime,newNumber);
mlii.appendData(v, true, 1);

System.out.println("data"+v);
  //System.out.println("I got the text from server = "+lineRead);
  //out.append("I got the text from server = "+lineRead);

} catch (IOException e) {
  AlertBox("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}

这个库对蓝牙非常https://github.com/palaima/AndroidSmoothBluetooth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM