Reputation: 1295
I am creating an app for Android-Arduino bluetooth serial communication. I am able to connect to arduino successfully. My app can send data to arduino without hassle and I have verified it. But While receiving data from arduino, my app only receives a part of data being send. For example if "404" is being send from arduino, my app only show "4" as being received.
I checked with other such apps and all other apps are able to receive "404" itself. So problem is with my code.
This is my code which read data from arduino:
public String read(byte[] bytes){
try {
mInput.read(bytes);
strInput = new String(bytes);
}catch(Exception e){
e.printStackTrace();
}
return strInput;
}
//mInput is the input stream of bluetooth connection
As you can see data is recived to a byte
buffer and converted to a string using new String(bytes);
method. How ever when I toast the string, only 4
is being toasted instead of 404
send from arduino.
The byte
buffer is of size 256
.
EDIT: as requested the full code for BluetoothManager.java
is this:
public class BluetoothManager {
private BluetoothAdapter bluetoothAdapter;
private BluetoothDevice bluetoothDevice;
private BluetoothSocket bluetoothSocket;
private ConnectedThread connectedThread;
private byte[] buffer;
public BluetoothManager(){
buffer=new byte[256];
bluetoothSocket=null;
bluetoothAdapter=null;
bluetoothDevice=null;
connectedThread=null;
getBluetoothAdapter();
if(!isBluetoothAvailable()){
turnBluetoothOn();
}
scanToConnect();
}
public void turnBluetoothOff(){
try {
bluetoothSocket.close();
bluetoothSocket=null;
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.disable();
bluetoothAdapter=null;
bluetoothDevice=null;
}catch(Exception e){
e.printStackTrace();
}
}
private boolean isBluetoothAvailable(){
return bluetoothAdapter.isEnabled();
}
private void turnBluetoothOn(){
bluetoothAdapter.enable();
}
public String readData(Context context){
String outputString=null;
if(isBluetoothAvailable()) {
outputString = connectedThread.read(buffer);
}else{
Toast.makeText(context, "Error: Not Connected", Toast.LENGTH_LONG).show();
}
return outputString;
}
public void writeData(String string, Context context){
if(isBluetoothAvailable()) {
connectedThread.write(string.getBytes());
}else{
Toast.makeText(context, "Error: Not Connected", Toast.LENGTH_LONG).show();
}
}
private void getBluetoothAdapter(){
try{
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
}catch (Exception e){
e.printStackTrace();
}
}
private void scanToConnect(){
Set<BluetoothDevice> pairedDevices=bluetoothAdapter.getBondedDevices();
if(pairedDevices.size()>0){
try {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("HC-05")) {
bluetoothDevice = device;
new connectBt(bluetoothDevice);
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
private class connectBt extends Thread {
public connectBt(BluetoothDevice device) {
BluetoothSocket tmp = null;
bluetoothDevice = device;
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
try {
tmp = device.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket = tmp;
run();
}
public void run() {
bluetoothAdapter.cancelDiscovery();
try {
bluetoothSocket.connect();
connectedThread = new ConnectedThread(bluetoothSocket);
} catch (IOException connectException) {
closeSocket();
}
}
private void closeSocket() {
try {
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ConnectedThread extends Thread{
private InputStream mInput=null;
private OutputStream mOutput=null;
private String strInput;
public ConnectedThread(BluetoothSocket socket){
bluetoothSocket=socket;
InputStream tmpIn=null;
OutputStream tmpOut=null;
try{
tmpIn=socket.getInputStream();
tmpOut=socket.getOutputStream();
}catch(IOException e){
e.printStackTrace();
closeSocket();
}
mInput=tmpIn;
mOutput=tmpOut;
}
public void write(byte[] bytes){
try{
mOutput.write(bytes);
}catch(IOException e){
e.printStackTrace();
}
}
public String read(byte[] bytes){
try {
mInput.read(bytes);
strInput = new String(bytes);
}catch(Exception e){
e.printStackTrace();
}
return strInput;
}
public void closeSocket(){
try{
bluetoothSocket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
Edit-2: On further debugging I found out that mInput.available()
returns 0
while mInput.read(bytes)
returns 1
.Why is this behavior while in my arduino code I am using bluetooth.println("404");
Upvotes: 1
Views: 1091
Reputation: 1295
Okey I solved this problem by putting a delay in my read()
method.
Upvotes: 0
Reputation: 1154
I was having the same problem. I solved by adding delimiter(\n) while sending from arduino i.e println() and checking in android code. Try below code in android, its working for me:
Try this code: http://pastebin.com/sfb3kuu6
Upvotes: 0
Reputation: 31
http://felhr85.net/2014/11/11/usbserial-a-serial-port-driver-library-for-android-v2-0/
Try this it should work. Also please provide the full code of it. May be the Error should be in the part where you are connecting the function to Serial Event handler.
Upvotes: 1