Last active
March 31, 2019 09:43
-
-
Save CWrecker/8b37df3a9c1d0a92f231a47262ca436e to your computer and use it in GitHub Desktop.
In this step we will create the serial connection that attaches the android app and the arduino together. In addition there will be buttons that will call functions to initiate specific tasks like sending, closing, and opening the connection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
//-------------------------------- | |
// Serial Connection | |
//------------------------------- | |
public final String ACTION_USB_PERMISSION = "com.hariharan.arduinousb.USB_PERMISSION"; | |
Button startButton, sendButton, orderButton, clearButton, stopButton; | |
TextView textView; | |
EditText editText; | |
UsbManager usbManager; | |
UsbDevice device; | |
UsbSerialDevice serialPort; | |
UsbDeviceConnection connection; | |
UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read. | |
@Override | |
public void onReceivedData(byte[] arg0) { | |
String data = null; | |
try { | |
data = new String(arg0, "UTF-8"); | |
data.concat("/n"); | |
tvAppend(textView, data); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { //Broadcast Receiver to automatically start and stop the Serial connection. | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if (intent.getAction().equals(ACTION_USB_PERMISSION)) { | |
boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); | |
if (granted) { | |
connection = usbManager.openDevice(device); | |
serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection); | |
if (serialPort != null) { | |
if (serialPort.open()) { //Set Serial Connection Parameters. | |
setUiEnabled(true); | |
serialPort.setBaudRate(9600); | |
serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8); | |
serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1); | |
serialPort.setParity(UsbSerialInterface.PARITY_NONE); | |
serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); | |
serialPort.read(mCallback); | |
tvAppend(textView,"Serial Connection Opened!\n"); | |
} else { | |
Log.d("SERIAL", "PORT NOT OPEN"); | |
} | |
} else { | |
Log.d("SERIAL", "PORT IS NULL"); | |
} | |
} else { | |
Log.d("SERIAL", "PERM NOT GRANTED"); | |
} | |
} else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { | |
onClickStart(startButton); | |
} else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { | |
onClickStop(stopButton); | |
} | |
} | |
; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment