我正在尝试开发一个通过USB与arduino通信的android应用程序。我已经实现了允许我连接两个设备的类。
在 Android 端,我有以下代码来建立连接:
public boolean openUsbConnection(UsbDevice usbDevice,int baudRate){
_isConnected = false;
if(connection != null) {
_isConnected = true;
return true;
}
else{
PendingIntent permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION),
PendingIntent.FLAG_IMMUTABLE);
usbManager.requestPermission(usbDevice,permissionIntent);
if(usbManager.hasPermission(usbDevice)){
connection = usbManager.openDevice(usbDevice);
if(connection!=null){
//int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
// res2 = connection.controlTransfer(0x21, 32, 0, 0, getLineEncoding(baudRate), 7, 0);
//if(setTransferParameters(CDC_SET_LINE_CODING,0,getLineEncoding(baudRate)) >=0
//&& setTransferParameters(CDC_SET_CONTROL_LINE_STATE,CDC_CONTROL_LINE_ON,null)>=0){
if(getInOutEndPoint(usbDevice)){
_isConnected = true;
}
//}
return true;
}
}
}
return false;
}
在上面的函数中,connection.controlTransfer被禁用,因为它总是返回-1,我发现没有它我仍然可以连接设备。
为了读取/写入数据,我使用了以下函数:
public int sendData(byte[] data, int timeout){
if(_isConnected){
Log.d("USB_DEBUG", "usbOutEndPoint: " + usbOutEndPoint);
Log.d("USB_DEBUG", "connection: " + connection);
int length = 0;
if(data!=null)length= data.length;
return connection.bulkTransfer(usbOutEndPoint, data, length, timeout);
}
else return -2;
}
public int readData(byte[] buffer, int timeout) {
if (_isConnected) {
try {
Log.d("USB_DEBUG", "usbOutEndPoint: " + usbOutEndPoint);
Log.d("USB_DEBUG", "connection: " + connection);
int bytesRead = connection.bulkTransfer(usbInEndPoint, buffer, buffer.length, timeout);
if (bytesRead < 0) {
Log.e("USB_DEBUG", "Error occurred during bulk transfer. bytesRead: " + bytesRead);
}
return bytesRead;
} catch (Exception e) {
Log.e("USB_DEBUG", "Exception occurred during bulk transfer: " + e.getMessage(), e);
return -1; // Return -1 to indicate an error occurred
}
} else {
Log.e("USB_DEBUG", "USB is not connected.");
return -2; // Return -2 if USB is not connected
}
}
为了从android发送数据,我最终这样做了:
void BtnOnClick(View view) {
if(view == startBtn)
{
int result = arduinoManager.sendData(new byte[]{1},5000);
int result2 = arduinoManager.readData(new byte[1],5000);
}
else if(view == btn)
{
int result = arduinoManager.sendData(new byte[]{0},5000);
int result2 = arduinoManager.readData(new byte[1],5000);
}
}
为了检查android和arduino之间是否有通信,我在arduino端做了以下操作:
void loop() {
if (Serial.available() > 0) {
if(ledState == 1) ledState == 0;
else ledState = 1;}
Serial.write(ledState);
}
当我从android发送数据时,arduino中的LED打开,所以我确定两个设备正在通信,而且在读取功能的结果中我得到的值是“1”。
问题是当我尝试在按下 Android 中的一个按钮时打开 LED 并在按下另一个按钮时关闭 LED 时,从 Android 端我没有收到任何错误,它说已发送 1 个字节。 在arduino一侧,LED灯不亮。不幸的是我无法使用串行监视器,因为它正在被 Android 设备使用。我在arduino中使用的函数是:
int led = 13;
bool ledState = LOW;
void setup() {
// Initialize the LED pin as an output
Serial.begin(115200);
pinMode(led, OUTPUT);
}
void loop() {
// Check if data is available to read from serial
if (Serial.available() > 0) {
byte received = Serial.read();
// Update the LED state based on the received value
if (received == 1) {
ledState = HIGH; // Turn the LED on
} else if (received == 0) {
ledState = LOW; // Turn the LED off
}
Serial.write(ledState);
}
// Update the LED with the current state
digitalWrite(led, ledState);
}
但我找不到一种方法来打开 LED。我什至问chatgpt 我能做什么,但这让我很困惑,因为它的答案是矛盾的。 ;(
我该怎么做才能让它发挥作用?
实现自己的库很困难,我使用了这个库,现在它工作完美