hello forum. i am working on a project, that involves an atmega328 reading some sensors, and then sending the data with serial, to an android tablet.
to do so, i use the arduino USBserial Light.
to be clear, this is the first time ever i wrote java, so this code will have way to much "mistakes" , but my main problem is this :
when i start the app, (or when i connect the arduino to the tablet) the app starts by recognizing the Serial Light.
then it informs the user, with a toast, if it found anything.
the problem is, that some times, it will not find the device. if that happens, no matter how many times i restart the app, it will never recognize the usb. (msg "no device found" ) . it will only change, when i disconnect and reconect the Serial light adapter...
can someone who has done serial connection to android on the past, help me, on what i am doing wrong?? the code relative to connecting the usb is as follows:
package com.example.rectangle;
imports ......
public class MainActivity extends Activity {
private static final int ARDUINO_USB_VENDOR_ID =0x2341; // 0x403
private static final int ARDUINO_UNO_USB_PRODUCT_ID =0x43; // 0x01
private static final int ARDUINO_UNO_R3_USB_PRODUCT_ID =0x3b ; //0x43
private final static String TAG = "ArduinoCommunicatorActivity";
private final static boolean DEBUG = false;
private volatile UsbDevice mUsbDevice = null;
private volatile UsbDeviceConnection mUsbConnection = null;
private volatile UsbEndpoint mInUsbEndpoint = null;
private volatile UsbEndpoint mOutUsbEndpoint = null;
public boolean DTH = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
findDevice();
DataHandler();
}
private void findDevice() {
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> usbDeviceList = usbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = usbDeviceList.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice tempUsbDevice = deviceIterator.next();
if (tempUsbDevice.getVendorId() == ARDUINO_USB_VENDOR_ID) {
switch (tempUsbDevice.getProductId()) {
case ARDUINO_UNO_USB_PRODUCT_ID:
Toast.makeText(getBaseContext(), "Arduino Uno " + getString(R.string.found), Toast.LENGTH_SHORT).show();
mUsbDevice= tempUsbDevice ;
if (initDevice()==true ) {
startReceiverThread();
}
break;
case ARDUINO_UNO_R3_USB_PRODUCT_ID:
Toast.makeText(getBaseContext(), "Arduino Uno R3 " + getString(R.string.found), Toast.LENGTH_SHORT).show();
mUsbDevice= tempUsbDevice ;
if (initDevice()==true ) {
startReceiverThread();
}
break;
}
}
}
if (mUsbDevice == null) {
Toast.makeText(getBaseContext(), getString(R.string.no_device_found), Toast.LENGTH_LONG).show();
} else {
if (DEBUG) Log.i(TAG, "Device found!");
}
}
@Override
public void onDestroy() {
super.onDestroy();
mUsbDevice = null;
DTH=false;
if (mUsbConnection != null) {
mUsbConnection.close();
}
}
private byte[] getLineEncoding(int baudRate) {
final byte[] lineEncodingRequest = { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };
switch (baudRate) {
case 14400:
lineEncodingRequest[0] = 0x40;
lineEncodingRequest[1] = 0x38;
break;
case 19200:
lineEncodingRequest[0] = 0x00;
lineEncodingRequest[1] = 0x4B;
break;
}
return lineEncodingRequest;
}
public boolean initDevice() {
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mUsbConnection = usbManager.openDevice(mUsbDevice);
if (mUsbConnection == null) {
if (DEBUG) Log.e(TAG, "Opening USB device failed!");
Toast.makeText(getBaseContext(), getString(R.string.opening_device_failed), Toast.LENGTH_LONG).show();
return false;
}
UsbInterface usbInterface = mUsbDevice.getInterface(1);
if (!mUsbConnection.claimInterface(usbInterface, true)) {
if (DEBUG) Log.e(TAG, "Claiming interface failed!");
Toast.makeText(getBaseContext(), getString(R.string.claimning_interface_failed), Toast.LENGTH_LONG).show();
mUsbConnection.close();
return false;
}
mUsbConnection.controlTransfer(0x21, 0x22, 0, 0, null, 0, 0);
mUsbConnection.controlTransfer(0x21, 0x20, 0, 0, getLineEncoding(9600), 7, 0);
for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
if (usbInterface.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (usbInterface.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN) {
mInUsbEndpoint = usbInterface.getEndpoint(i);
} else if (usbInterface.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_OUT) {
mOutUsbEndpoint = usbInterface.getEndpoint(i);
}
}
}
if (mInUsbEndpoint == null) {
if (DEBUG) Log.e(TAG, "No in endpoint found!");
Toast.makeText(getBaseContext(), getString(R.string.no_in_endpoint_found), Toast.LENGTH_LONG).show();
mUsbConnection.close();
return false;
}
if (mOutUsbEndpoint == null) {
if (DEBUG) Log.e(TAG, "No out endpoint found!");
Toast.makeText(getBaseContext(), getString(R.string.no_out_endpoint_found), Toast.LENGTH_LONG).show();
mUsbConnection.close();
return false;
}
return true;
}
also, i have the resources res/xml/devicefilter.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" />
<!-- 0x2341 / Arduino -->
<usb-device vendor-id="9025" />
<!-- 0x16C0 / 0x0483: Teensyduino -->
<usb-device vendor-id="5824" product-id="1155" />
<!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
<usb-device vendor-id="4292" product-id="60000" />
</resources>