android app for arduino

  • Replies:2
  • OpenNot stickiedUnanswered

Aug 7, 2012 6:56:27 PM via Website

Hello
My application must receive digital pin value (1(high) or 0(low)) from an Arduino by the serial port.
but I am receiving too slowly 1 or 0 on the Android.
here is my code:

public class MainActivity extends Activity {

private static final String ACTION_USB_PERMISSION = "com.google.android.DemoKit.action.USB_PERMISSION";
private UsbManager mUsbManager;
private PendingIntent mPermissionIntent;
private boolean mPermissionRequestPending;
private ToggleButton buttonLED;
TextView PinValue;
UsbAccessory mAccessory;
ParcelFileDescriptor mFileDescriptor;
FileInputStream mInputStream;
FileOutputStream mOutputStream;

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbAccessory accessory = UsbManager.getAccessory(intent);
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
openAccessory(accessory);
} else {


}
mPermissionRequestPending = false;
}
} else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
UsbAccessory accessory = UsbManager.getAccessory(intent);
if (accessory != null && accessory.equals(mAccessory)) {
closeAccessory();
}
}
}
};



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mUsbManager = UsbManager.getInstance(this);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
registerReceiver(mUsbReceiver, filter);

if (getLastNonConfigurationInstance() != null) {
mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
openAccessory(mAccessory);
}

setContentView(R.layout.main);
buttonLED = (ToggleButton) findViewById(R.id.toggleButtonLED);
// ButtonOut=(Button)findViewById(R.id.lesen);
PinValue= (TextView)findViewById(R.id.textView3);
}

@Override
public Object onRetainNonConfigurationInstance() {
if (mAccessory != null) {
return mAccessory;
} else {
return super.onRetainNonConfigurationInstance();
}
}

@Override
public void onResume() {
super.onResume();

if (mInputStream != null && mOutputStream != null) {
return;
}

UsbAccessory[] accessories = mUsbManager.getAccessoryList();
UsbAccessory accessory = (accessories == null ? null : accessories[0]);
if (accessory != null) {
if (mUsbManager.hasPermission(accessory)) {
openAccessory(accessory);
} else {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
mUsbManager.requestPermission(accessory,mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
} else {

}
}


public void onPause() {
super.onPause();
closeAccessory();
}

@Override
public void onDestroy() {
unregisterReceiver(mUsbReceiver);
super.onDestroy();
}

private void openAccessory(UsbAccessory accessory) {
mFileDescriptor = mUsbManager.openAccessory(accessory);
if (mFileDescriptor != null) {
mAccessory = accessory;
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
mInputStream = new FileInputStream(fd);
mOutputStream = new FileOutputStream(fd);

} else {

}
}


private void closeAccessory() {
try {
if (mFileDescriptor != null) {
mFileDescriptor.close();
}
} catch (IOException e) {
} finally {
mFileDescriptor = null;
mAccessory = null;
}
}

public void blinkLED(View v){

byte[] buffer = new byte[1];

if(buttonLED.isChecked())
buffer[0]=(byte)1; // button says on, light is off

else
buffer[0]=(byte)0; // button says off, light is on

if (mOutputStream != null) {
try {
mOutputStream.write(buffer);
} catch (IOException e) {

}


}
}

public void ClickBtlesen(View view) {
byte[] buffer = new byte[1];
buffer[0]=(byte)2; // if 2 -> arduino sends pin value
if (mOutputStream != null) {
try {
mOutputStream.write(buffer);
} catch (IOException e) {
}
}


int ret = 0;
byte[] bbuffer = new byte[1];

try {
ret = mInputStream.read(bbuffer);
} catch (IOException e) {

}


if(ret==1){


PinValue.setText("HIGH");
return;

}

if(ret==0)
{
PinValue.setText("LOW");

return;
}

}




}