ผมทำ app ว่าจะส่ง string (แต่ตอนนี้ทดสอบส่งตัวอักษร)ทำไมมันไม่ขึ้น ใน Serial Monitor ใน Arduino ครับ ผมทำผิดตรงไหน ตัวอย่างส่ง T มา 1 ตัว
code Android
[open_code]
package app.teststring.app;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.roommanage.app.R;
import app.akexorcist.bluetoothspp.BluetoothSPP;
import app.akexorcist.bluetoothspp.BluetoothSPP.BluetoothConnectionListener;
import app.akexorcist.bluetoothspp.BluetoothSPP.OnDataReceivedListener;
import app.akexorcist.bluetoothspp.BluetoothState;
import app.akexorcist.bluetoothspp.DeviceList;
public class MainActivity extends ActionBarActivity {
BluetoothSPP bt;
BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = new BluetoothSPP(this);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "No bluetooth adapter available"
, Toast.LENGTH_SHORT).show();
}
bt.setOnDataReceivedListener(new OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {
Toast.makeText(getApplicationContext(), message
, Toast.LENGTH_SHORT).show();
}
});
bt.setBluetoothConnectionListener(new BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
Toast.makeText(getApplicationContext()
, "Connected to " + name + "\n" + address
, Toast.LENGTH_SHORT).show();
}
public void onDeviceDisconnected() {
Toast.makeText(getApplicationContext()
, "Connection lost", Toast.LENGTH_SHORT).show();
}
public void onDeviceConnectionFailed() {
Toast.makeText(getApplicationContext()
, "Unable to connect", Toast.LENGTH_SHORT).show();
}
});
Button btnConnect = (Button)findViewById(R.id.btnConnect);
btnConnect.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
bt.disconnect();// แก้จาก bt.disconnected()
} else {
Intent intent = new Intent(getApplicationContext()
, DeviceList.class);
startActivityForResult(intent
, BluetoothState.REQUEST_CONNECT_DEVICE);
}
}
});
}
public void onDestroy() {
super.onDestroy();
bt.stopService();
}
public void onStart() {
super.onStart();
if (!bt.isBluetoothEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
} else {
if (!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
setup();
}
}
}
public void setup() {
Button btnSubmit = (Button)findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bt.send("T");
}
});
bt.autoConnect("HC-05");
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if(resultCode == Activity.RESULT_OK)
bt.connect(data);
} else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if(resultCode == Activity.RESULT_OK) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
setup();
} else {
Toast.makeText(getApplicationContext() ,
"Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
[/close_code]
code Arduino
[open_code]
#include <SoftwareSerial.h>
int bluetoothTx = 2;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup()
{
pinMode(bluetoothTx,INPUT);
pinMode(bluetoothRx,OUTPUT);
//Setup usb serial connection to computer
Serial.begin(9600);// Begin the serial monitor at 9600bps
//Setup Bluetooth serial connection to android
bluetooth.begin(115200);// The Bluetooth Mate defaults to 115200bps
bluetooth.print("$$$"); // Print three times individually //ต้นฉบับ print 3 คำสั่ง แยกที่ละตัว
delay(100);// Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N");// Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600);// Start bluetooth serial at 9600
inputString.reserve(200);
}
void loop()
{
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
// while (Serial.available()) {
while (bluetooth.available()) {
// get the new byte:
char inChar = (char)bluetooth.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if ((inChar == '\r') ||(inChar == '\n')) {
stringComplete = true;
}
}
}
[/close_code]
ใน Serial Monitor ผมปรับให้เป็น Newline ด้วยนะ
App ส่ง string ไป Arduino ผ่าน bluetooth ลองส่งแล้วไม่มีอะไรเกิดขึ้น
code Android
[open_code]
package app.teststring.app;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.roommanage.app.R;
import app.akexorcist.bluetoothspp.BluetoothSPP;
import app.akexorcist.bluetoothspp.BluetoothSPP.BluetoothConnectionListener;
import app.akexorcist.bluetoothspp.BluetoothSPP.OnDataReceivedListener;
import app.akexorcist.bluetoothspp.BluetoothState;
import app.akexorcist.bluetoothspp.DeviceList;
public class MainActivity extends ActionBarActivity {
BluetoothSPP bt;
BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = new BluetoothSPP(this);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "No bluetooth adapter available"
, Toast.LENGTH_SHORT).show();
}
bt.setOnDataReceivedListener(new OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {
Toast.makeText(getApplicationContext(), message
, Toast.LENGTH_SHORT).show();
}
});
bt.setBluetoothConnectionListener(new BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
Toast.makeText(getApplicationContext()
, "Connected to " + name + "\n" + address
, Toast.LENGTH_SHORT).show();
}
public void onDeviceDisconnected() {
Toast.makeText(getApplicationContext()
, "Connection lost", Toast.LENGTH_SHORT).show();
}
public void onDeviceConnectionFailed() {
Toast.makeText(getApplicationContext()
, "Unable to connect", Toast.LENGTH_SHORT).show();
}
});
Button btnConnect = (Button)findViewById(R.id.btnConnect);
btnConnect.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
bt.disconnect();// แก้จาก bt.disconnected()
} else {
Intent intent = new Intent(getApplicationContext()
, DeviceList.class);
startActivityForResult(intent
, BluetoothState.REQUEST_CONNECT_DEVICE);
}
}
});
}
public void onDestroy() {
super.onDestroy();
bt.stopService();
}
public void onStart() {
super.onStart();
if (!bt.isBluetoothEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
} else {
if (!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
setup();
}
}
}
public void setup() {
Button btnSubmit = (Button)findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bt.send("T");
}
});
bt.autoConnect("HC-05");
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if(resultCode == Activity.RESULT_OK)
bt.connect(data);
} else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if(resultCode == Activity.RESULT_OK) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
setup();
} else {
Toast.makeText(getApplicationContext() ,
"Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
[/close_code]
code Arduino
[open_code]
#include <SoftwareSerial.h>
int bluetoothTx = 2;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup()
{
pinMode(bluetoothTx,INPUT);
pinMode(bluetoothRx,OUTPUT);
//Setup usb serial connection to computer
Serial.begin(9600);// Begin the serial monitor at 9600bps
//Setup Bluetooth serial connection to android
bluetooth.begin(115200);// The Bluetooth Mate defaults to 115200bps
bluetooth.print("$$$"); // Print three times individually //ต้นฉบับ print 3 คำสั่ง แยกที่ละตัว
delay(100);// Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N");// Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600);// Start bluetooth serial at 9600
inputString.reserve(200);
}
void loop()
{
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
// while (Serial.available()) {
while (bluetooth.available()) {
// get the new byte:
char inChar = (char)bluetooth.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if ((inChar == '\r') ||(inChar == '\n')) {
stringComplete = true;
}
}
}
[/close_code]
ใน Serial Monitor ผมปรับให้เป็น Newline ด้วยนะ