Reputation: 21
I have an android app. I want to send a string to java server when I touch the button each time in my client app.I am able to connect to server.
But the problem is when I touch the button, no string is sending to server. No matter how many times I touch the button the string is not sending but suddenly when I exit the app in my smartphone, means when I destroy the activity, all the strings then transfers to the server at the same time.
If I touch the button 100 times, 100 same strings are transferred to the server only when I destroy activity. I want the string to be transferred to server at the time when I touch the button.
Please help me with this. I am a newbie. My code is below:
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) {
}
}
}}
the server code is:
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();
}
}
Upvotes: 1
Views: 705
Reputation: 21
Problem solved.. Just add "\n" at the end of string you want to transfer.
Upvotes: 1