Hello,
I have tried many ways to make the connection of 2 Arduino to an App but I still can't get it to run smoothly. Is there any experts out there that can help me out? I just want to connect one Arduino for 5 seconds and disconnects and then connect to the other Arduino continuously. My code is working but it sometimes doesn't connect at all or where after 5 seconds, the first Arduino BT disconnects and the other Arduino doesn't get connected. Or both the Arduino starts connecting once the App starts.
Much help appreciated!!
Thank you!
Here is my code:
I have tried many ways to make the connection of 2 Arduino to an App but I still can't get it to run smoothly. Is there any experts out there that can help me out? I just want to connect one Arduino for 5 seconds and disconnects and then connect to the other Arduino continuously. My code is working but it sometimes doesn't connect at all or where after 5 seconds, the first Arduino BT disconnects and the other Arduino doesn't get connected. Or both the Arduino starts connecting once the App starts.
Much help appreciated!!
Thank you!
Here is my code:
Java:
<pre>package com.busapp.simple.lcdlightsensor;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "ArduinoBT";
//Light
private static final String BT_NAME = "RNBT-E341";
String incomingMsg;
TextView lightConn;
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mDevice;
Set<BluetoothDevice> pairedDevices;
ConnectThread mConnectThread;
ConnectedThread mConnectedThread;
//LCD
TextView lcdConn;
private static final String BT_NAME_LCD = "RNBT-E34E";
BluetoothAdapter BluetoothAdapterforLCD;
BluetoothDevice DeviceforLCD;
Set<BluetoothDevice> pairedDevicesforLCD;
ConnectThread_LCD ConnectThreadforLCD;
ConnectedThread_LCD ConnectedThreadforLCD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothAdapterforLCD = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
pairedDevicesforLCD = BluetoothAdapterforLCD.getBondedDevices();
if (pairedDevicesforLCD.size() > 0) {
for (BluetoothDevice device : pairedDevicesforLCD) {
if (device.getName().equals(BT_NAME_LCD)) {
DeviceforLCD = device;
}
}
}
pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals(BT_NAME)) {
mDevice = device;
}
}
}
Toast.makeText(
this,
"" + DeviceforLCD.getName() + " & " + mDevice.getName(),
Toast.LENGTH_LONG).show();
CountDownTimer countDownTimer = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// ConnectThreadforLCD = new ConnectThread_LCD(DeviceforLCD);
// ConnectThreadforLCD.start();
Log.i(TAG, "Countdown start");
}
@Override
public void onFinish() {
if ((ConnectedThreadforLCD != null) && (ConnectThreadforLCD != null)) {
ConnectThreadforLCD.cancel();
ConnectedThreadforLCD.cancel();
Log.i(TAG, "LCD Cancelled");
}
}
};
countDownTimer.start();
ConnectThreadforLCD = new ConnectThread_LCD(DeviceforLCD);
ConnectThreadforLCD.start();
// mConnectThread = new ConnectThread(mDevice);
// mConnectThread.start();
// Log.i(TAG, "Connect to device: " + mDevice.getName());
}
//
public class ConnectThread_LCD extends Thread {
private BluetoothSocket lcdSocket;
private BluetoothDevice lcdDevice;
private UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread_LCD(BluetoothDevice device) {
BluetoothSocket tmp = null;
lcdDevice = device;
if (tmp == null) {
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
}
else {
lcdSocket = tmp;
}
// try {
// tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
// } catch (IOException e) { }
lcdSocket = tmp;
}
public void run() {
Log.i(TAG, "LCD Discover");
if (BluetoothAdapterforLCD.isDiscovering()) {
BluetoothAdapterforLCD.cancelDiscovery();
}
try {
Log.i(TAG, "LCD Connected");
lcdSocket.connect();
} catch (IOException connectException) {
try {
lcdSocket.close();
Log.i(TAG, "LCD Socket Closed");
} catch (IOException closeException) { }
return;
}
Log.i(TAG, "LCD ConnectedThread start");
ConnectedThreadforLCD = new ConnectedThread_LCD(lcdSocket);
ConnectedThreadforLCD.start();
}
public void cancel() {
try {
if (lcdSocket.isConnected()) {
lcdSocket.close();
lcdSocket = null;
}
} catch (IOException e) { }
}
}
public class ConnectedThread_LCD extends Thread {
private BluetoothSocket lcdSocket;
private InputStream lcdInstream;
private OutputStream lcdOutstream;
public ConnectedThread_LCD(BluetoothSocket socket) {
Log.i(TAG, "Connected Thread");
lcdSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = lcdSocket.getInputStream();
tmpOut = lcdSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
lcdInstream = tmpIn;
lcdOutstream = tmpOut;
write(1);
lcdHandler.obtainMessage(0, 0).sendToTarget();
Log.i(TAG, "InputStream contains tmpIn");
}
public void run() {
byte[] buffer = new byte[2048];
int bytes;
}
public void cancel() {
try {
if (lcdSocket.isConnected()) {
lcdInstream.close();
lcdOutstream.close();
lcdSocket.close();
lcdSocket = null;
}
} catch (IOException e) {
}
}
public void write (int bytes) {
try {
lcdOutstream.write(bytes);
}
catch (IOException e) {
}
}
}
Handler lcdHandler = new Handler() {
String s;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
// ConnectThreadforLCD.cancel();
// ConnectedThreadforLCD.cancel();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mConnectThread = new ConnectThread(mDevice);
mConnectThread.start();
Log.i(TAG, "LCD BT Cancelled");
break;
}
}
};
//Arduino Code
public class ConnectThread extends Thread {
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
private UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
if (tmp == null) {
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
}
else {
mmSocket = tmp;
}
// try {
// tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
// } catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
Log.i(TAG, "Discovering");
try {
mmSocket.connect();
Log.i(TAG, "Socket Connected");
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
Log.i(TAG, "Connect Started");
}
public void cancel() {
try {
if (mmSocket.isConnected()) {
mmSocket.close();
mmSocket = null;
}
} catch (IOException e) { }
}
}
public class ConnectedThread extends Thread {
private BluetoothSocket mmSocket;
private InputStream mmInstream;
private OutputStream mmOutstream;
public ConnectedThread(BluetoothSocket socket) {
Log.i(TAG, "Connected Thread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
Log.i(TAG, "(aaa6)");
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG, "(aaa7)");
mmInstream = tmpIn;
mmOutstream = tmpOut;
write(1);
Log.i(TAG, "InputStream contains tmpIn");
}
public void run() {
byte[] buffer = new byte[2048];
//String incomingMsg;
int bytes;
while (true) {
Log.i(TAG, "While true");
try {
bytes = mmInstream.read(buffer);
//Log.i(TAG, "Buffer read" + String.valueOf(bytes));
byte[] buff = new byte[bytes];
Log.i(TAG, "Buffer read" + bytes);
System.arraycopy(buffer, 0, buff, 0, bytes);
incomingMsg = new String(buff);
if(incomingMsg.equals(" "))
{
continue;
}
Log.i(TAG, "(aaa11)");
mHandler.obtainMessage(1, incomingMsg).sendToTarget();
Log.i(TAG, "InputStream " + incomingMsg);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "Error reading input " + e.getMessage());
}
}
}
public void cancel() {
try {
if (mmSocket.isConnected()) {
mmInstream.close();
mmOutstream.close();
mmSocket.close();
mmSocket = null;
}
} catch (IOException e) {
}
}
public void write (int bytes) {
try {
mmOutstream.write(bytes);
}
catch (IOException e) {
}
}
}
//
Handler mHandler = new Handler() {
String s;
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
// case 0:
//// ConnectThreadforLCD.cancel();
//// ConnectedThreadforLCD.cancel();
//
// aConnectThread = new ConnectThread(aDevice);
// aConnectThread.start();
//
// Log.i(TAG, "LCD BT Cancelled");
// break;
case 1:
Log.i(TAG, "Message handled");
s = (String) incomingMsg;
if (s!=null) {
// lightValue.setText(s);
// if ((Integer.parseInt(s) <= 300) || (Integer.parseInt(s) >= 500)) {
// sendRequest(lightOn);
// Log.i(TAG, "Send IFTTT request");
// }
// else {
// sendRequest(lightOff);
// Log.i(TAG, "Send IFTTT request");
// }
}
break;
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
ConnectedThreadforLCD.cancel();
ConnectThreadforLCD.cancel();
mConnectThread.cancel();
mConnectedThread.cancel();
}
}