我正在开发一款小型Android应用程序,它可以通过USB向Arduino发送信号,从而控制机器人。这是代码。有了这个应用程序,我可以检测到Arduino板以及供应商ID和所有,但我不知道需要初始化什么来向Arduino板发送串行信号。
这是我的代码:
package com.example.lenovo.client;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.nfc.tech.IsoDep;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.MifareUltralight;
import android.nfc.tech.Ndef;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.PendingIntent;
import android.nfc.NfcAdapter;
import android.widget.TextView;
import com.example.lenovo.client.UsbSerialDevice;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
public class MainActivity extends Activity {
public void Button(View view) {
Toast.makeText(getApplicationContext(), "Please enter your code", Toast.LENGTH_LONG).show();
}
Button mButton;
EditText mEdit;
public String d;
private final String[][] techList = new String[][]{
new String[]{
NfcA.class.getName(),
NfcB.class.getName(),
NfcF.class.getName(),
NfcV.class.getName(),
IsoDep.class.getName(),
MifareClassic.class.getName(),
MifareUltralight.class.getName(), Ndef.class.getName()
}
};
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button1);
mEdit = (EditText) findViewById(R.id.editText);
mButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
Log.v("EditText", mEdit.getText().toString());
d = mEdit.getText().toString();
}
}
);
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbDeviceConnection connection;
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
UsbDevice device = null;
while(deviceIterator.hasNext()) {
device = deviceIterator.next();
String s = device.getDeviceName();
int pid = device.getProductId();
int did = device.getDeviceId();
int vid = device.getVendorId();
TextView tv = (TextView) findViewById(R.id.valueList);
tv.setText(s + "\n" + Integer.toString(pid) + "\n" + Integer.toString(did));
}
connection = manager.openDevice(device);
DataTransfer dt = new DataTransfer(device,connection);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public String Uid;
@Override
protected void onNewIntent(Intent intent) {
Log.i("onNewIntent", "Action=" + intent.getAction());
if ("android.nfc.action.TAG_DISCOVERED".equals(intent.getAction())) {
((TextView) findViewById(R.id.valueList)).setText("NFC Tag\n" + ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
Uid = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
Toast.makeText(getApplicationContext(), Uid, Toast.LENGTH_LONG).show();
if (d == null){
} else{
a();
}
}
}
@Override
protected void onPause() {
super.onPause();
// disabling foreground dispatch:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onResume() {
super.onResume();
// creating pending intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// creating intent receiver for NFC events:
IntentFilter filter = new IntentFilter();
filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
// enabling foreground dispatch for getting intent from NFC event:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);
}
private String ByteArrayToHexString(byte[] inarray) {
int i, j, in;
String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
String out = "";
for (j = 0; j < inarray.length; ++j) {
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
public void a() {
Toast.makeText(getApplicationContext(), d, Toast.LENGTH_SHORT).show();
int val1 = 1;
int val2 = 2;
int val3 = 3;
int val4 = 4;
int val5 = 5;
int val6 = 6;
int val7 = 7;
int val8 = 8;
int val9 = 9;
if (Integer.parseInt(d) == val1) {
String f = "1758330E";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 1", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
if (Integer.parseInt(d) == val2) {
String f = "17574025";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 2", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
if (Integer.parseInt(d) == val3) {
String f = "1757F736";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 3", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
if (Integer.parseInt(d) == val4) {
String f = "1758330C";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 4", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
if (Integer.parseInt(d) == val5) {
String f = "1757D80C";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 5", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
if (Integer.parseInt(d) == val6) {
String f = "17572D00";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 6", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
if (Integer.parseInt(d) == val7) {
String f = "1757B529";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 7", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
if (Integer.parseInt(d) == val8) {
String f = "17581013";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 8", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
if (Integer.parseInt(d) == val9) {
String f = "1757D609";
if (f.equals(Uid)){
Toast.makeText(getApplicationContext(), "product 9", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "U will Win", Toast.LENGTH_SHORT).show();
}
}
}
我有所有文件,例如:
SerialInputOutputManager.java
SerialInputStream.java
SerialOutputStream.java
DataTransfer.java
一切准备就绪并且完整,但我不知道需要初始化什么,以便向Arduino发送串行信号。
注意:我想从主要活动本身,从if语句向Arduino发送串行信号。
这是Arduino代码:
char c;
void setup(){
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(7,OUTPUT);
Serial.begin(9600);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
}
void loop() {
if (Serial.available()>0) {
c = Serial.read() - '0';
Serial.flush();
if(c == 1){
digitalWrite(5, HIGH);
digitalWrite(7, LOW);
delay(1000);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
}
if(c == 2){
digitalWrite(3, HIGH);
delay(4900);
digitalWrite(3, LOW);
}
if(c == 3) {
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
}
}
}