I post mu my Android code, but everything work well, I connect to bluetooth and I send data without problem. Then I am using Arduino UNO so RX, TX is 2 and 3 it is alos Ok.
here my code
public class ClientSocketActivity extends Activity
{
.
.
.
.
.
public void onToggleClicked(View view) throws IOException {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if(view ==lampe1) {
if (on) {
// Enable vibrate
String tmpStr ="1";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
Toast.makeText(getApplicationContext(),"Lampe1 turned on" ,
Toast.LENGTH_LONG).show();
} else {
// Disable vibrate
String tmpStr ="2";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
Toast.makeText(getApplicationContext(),"Lampe1 turned oFF" ,
Toast.LENGTH_LONG).show();
}
}
if(view ==lampe2) {
if (on) {
// Enable vibrate
String tmpStr ="l2";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
Toast.makeText(getApplicationContext(),"Lampe2 turned on" ,
Toast.LENGTH_LONG).show();
} else {
// Disable vibrate
String tmpStr ="l22";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
Toast.makeText(getApplicationContext(),"Lampe2 turned oFF" ,
Toast.LENGTH_LONG).show();
}
}
if(view ==appareil1) {
if (on) {
// Enable vibrate
Toast.makeText(getApplicationContext(),"Appareil 1 turned on" ,
Toast.LENGTH_LONG).show();
String tmpStr ="a1";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
} else {
// Disable vibrate
Toast.makeText(getApplicationContext(),"Appareil 1 turned oFF" ,
Toast.LENGTH_LONG).show();
String tmpStr ="a12";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
}
}
if(view ==appareil2) {
if (on) {
// Enable vibrate
String tmpStr ="a2";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
Toast.makeText(getApplicationContext(),"Appareil 2 turned on" ,
Toast.LENGTH_LONG).show();
} else {
// Disable vibrate
String tmpStr ="a22";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
Toast.makeText(getApplicationContext(),"Appareil 2 turned oFF" ,
Toast.LENGTH_LONG).show();
}
}
if(view ==smartvigil) {
if (on) {
// Enable vibrate
String tmpStr ="s1";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
Toast.makeText(getApplicationContext(),"Smart vigil turned on" ,
Toast.LENGTH_LONG).show();
} else {
// Disable vibrate
String tmpStr ="s12";
byte bytes[] = tmpStr.getBytes();
outputStream.write(bytes);
Toast.makeText(getApplicationContext(),"Smart vigil turned oFF" ,
Toast.LENGTH_LONG).show();
}
}
}
/* after select, connect to device */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != REQUEST_DISCOVERY) {
finish();
return;
}
if (resultCode != RESULT_OK) {
finish();
return;
}
final BluetoothDevice device = data.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
new Thread() {
public void run() {
connect(device);
};
}.start();
}
protected void onDestroy() {
super.onDestroy();
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("EF-BTBee", ">>", e);
}
}
protected void connect(BluetoothDevice device) {
//BluetoothSocket socket = null;
try {
//Create a Socket connection: need the server's UUID number of registered
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();
Log.d("EF-BTBee", ">>Client connectted");
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
int read = -1;
final byte[] bytes = new byte[2048];
for (; (read = inputStream.read(bytes)) > -1;) {
final int count = read;
_handler.post(new Runnable() {
public void run() {
StringBuilder b = new StringBuilder();
for (int i = 0; i < count; ++i) {
String s = Integer.toString(bytes[i]);
b.append(s);
b.append(",");
}
String s = b.toString();
String[] chars = s.split(",");
sbu = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
sbu.append((char) Integer.parseInt(chars[i]));
}
Log.d("EF-BTBee", ">>inputStream");
if(str != null)
{
sTextView.setText(str + "<-- " + sbu);
str += ("<-- " + sbu.toString());
}
else
{
sTextView.setText("<-- " + sbu);
str = "<-- " + sbu.toString();
}
str += '\n';
}
});
}
} catch (IOException e) {
Log.e("EF-BTBee", ">>", e);
finish();
return ;
} finally {
if (socket != null) {
try {
Log.d("EF-BTBee", ">>Client Socket Close");
socket.close();
finish();
return ;
} catch (IOException e) {
Log.e("EF-BTBee", ">>", e);
}
}
}
}
}
arduino
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
int ledPin = 13;
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
void setup()
{
// define pin modes for tx, rx, led pins:[color=#222222][/color]
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(ledPin, OUTPUT);
if (mySerial.available()){
mySerial.begin(9600);
Serial.begin(9600);
}
}
void loop()
{
char someChar = 0;
// listen for new serial coming in:
// print out the character:
if (mySerial.available()>0){
someChar = mySerial.read();
if( someChar=='1')
{
digitalWrite(ledPin, HIGH);
mySerial.println('OK');
}
if( someChar='1')
{
digitalWrite(ledPin, HIGH);
mySerial.print('OK');
}
if( someChar=='2')
{
digitalWrite(ledPin, LOW);
mySerial.print('OK');
}
mySerial.println(someChar);
//print to Serial Monitor
Serial.print(someChar);
}
}
I can get the same data I want to get from Android, but my (if test ) won't to work, what if I use case?