I have used non-buffered stream also changed the same in server side code. it got worked. Thanks Smith..
receive commands uing Bluetoothsocket connection
-
30-09-2022 - |
Pregunta
I am trying to open a connection with my laptop which has bluetooth from my phone. Server program is running in my laptop. My intention was to establish a connection with my laptop to send some inputs continuously on the same created socket. Connection was successfully established and a byte was sent to the server running in my laptop but after pressing send button nothing was sent.
The below code works fine for the first attempt. When i am trying to resend using the same connection byte value was not sent. When i launching my application for second time my phone gets hanged. I am looking for memory issues but i couldn't find any memory issues. Did i miss something ? Do i need to do something.
public class BluetoothActivity extends Activity {
private TextView myLabel;
private EditText myTextbox;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
private OutputStream mmOutputStream;
private BufferedOutputStream mbuffStream;
private ProgressDialog progressBar;
private int progressBarStatus;
private Context mContext;
private TextView devicesLabel;
private TextView devices;
private final static int MAX_VALUE = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
mContext = this;
Button openButton = (Button) findViewById(R.id.open);
Button sendButton = (Button) findViewById(R.id.send);
Button closeButton = (Button) findViewById(R.id.close);
myLabel = (TextView) findViewById(R.id.label);
devicesLabel = (TextView) findViewById(R.id.devicelabel);
devices = (TextView) findViewById(R.id.devices);
myTextbox = (EditText) findViewById(R.id.entry);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Open Button
openButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
openBTConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
});
// Send Button
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
sendData();
} catch (IOException ex) {
}
}
});
// Close button
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
closeBT();
}
});
}
private void openBTConnection() throws IOException {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // Standard
// SerialPortService
// ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
System.out.println("socket " + mmSocket.toString());
if (mmSocket != null) {
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mbuffStream = new BufferedOutputStream(mmOutputStream, 1);
myLabel.setText("Bluetooth Opened");
} else {
myLabel.setText("No bluetooth device found");
}
}
void sendData() throws IOException {
String msg = myTextbox.getText().toString();
mbuffStream.write(msg.getBytes(), 0, msg.getBytes().length);
mbuffStream.flush();
myLabel.setText("Data Sent");
}
void closeBT() {
try {
if (mmOutputStream != null) {
mmOutputStream.close();
}
if (mbuffStream != null) {
mbuffStream.close();
}
if (mmSocket != null) {
mmSocket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myLabel.setText("Bluetooth Closed");
}
@Override
public void onBackPressed() {
closeBT();
finish();
}
}
thanks Shriram M
Solución