Android和java蓝牙客户端服务器应用程序

Android和java蓝牙客户端服务器应用程序,java,android,bluetooth,server,client,Java,Android,Bluetooth,Server,Client,我有一个android应用程序。每次在客户端应用程序中按下按钮时,我想向java服务器发送一个字符串。我可以连接到服务器 但问题是,当我按下按钮时,没有字符串发送到服务器。无论我按了多少次按钮,字符串都不会发送,但当我在智能手机中退出应用程序时会突然发送,这意味着当我销毁活动时,所有字符串会同时传输到服务器 如果我按下按钮100次,只有在我销毁活动时,才会将100个相同的字符串传输到服务器。我希望在触摸按钮时将字符串传输到服务器 请帮我做这个。我是个新手。我的代码如下: public class

我有一个android应用程序。每次在客户端应用程序中按下按钮时,我想向java服务器发送一个字符串。我可以连接到服务器

但问题是,当我按下按钮时,没有字符串发送到服务器。无论我按了多少次按钮,字符串都不会发送,但当我在智能手机中退出应用程序时会突然发送,这意味着当我销毁活动时,所有字符串会同时传输到服务器

如果我按下按钮100次,只有在我销毁活动时,才会将100个相同的字符串传输到服务器。我希望在触摸按钮时将字符串传输到服务器

请帮我做这个。我是个新手。我的代码如下:

public class Joystick extends Activity {
BluetoothAdapter ba=BluetoothAdapter.getDefaultAdapter();
//local bluetooth adapter object created
BluetoothDevice bd;                                                                 //GLOBAL DECLARATION BLOCK
BluetoothSocket bs;
OutputStream os;
InputStream in;
Button up;
private static final UUID MY_UUID =UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.joystick);
    up= (Button) findViewById(R.id.up);

    if(ba.isEnabled()==false)                                                       //BLUETOOTH ADAPTER ENABLED IF WAS DISABLED
        ba.enable();
    final String address = getIntent().getStringExtra("address").trim();
    setResult(100, new Intent());
    bd=ba.getRemoteDevice(address);
    BluetoothConnect.start();


}
@Override
protected void onResume(){
    super.onResume();
    up.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    try {
                        os = bs.getOutputStream();
                        String message = "up";
                        byte[] msgBuffer = message.getBytes();
                        os.write(msgBuffer);



                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    try {
                        os.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;



            }
            return true;
        }
    });


}
Thread BluetoothConnect = new Thread(){
    public void run(){
        try {
            bs=bd.createRfcommSocketToServiceRecord(MY_UUID);
            bs.connect();


        } catch (IOException e) {
            try {
                bs.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }

    }

};
protected void onStop(){
    super.onStop();

    try {
        os.close();
        bs.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@Override
protected void onPause() {
    super.onPause();
    if (os != null) {
        try {
            os.flush();
        } catch (IOException e) {

        }
}
}}
服务器代码为:

public class SimpleSPPServer {
private static Robot robot;
//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=SampleSPPServer";

    //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);
    try{
        robot=new Robot();

    if(lineRead.equalsIgnoreCase("up")){                        
robot.keyPress(KeyEvent.VK_UP);
robot.keyRelease(KeyEvent.VK_UP);
    }       }
    catch(AWTException e){
        System.out.println("exception while creating robot instance");
    }

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

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

}
}
问题解决了。。只需在要传输的字符串末尾添加“\n”