Java USB主机Android无法发送字符串
我想让我的Android使用USB主机与Arduino通信。我尝试了很多参考资料,但似乎无法向我的Arduino发送任何字符。我可以检测到我的Arduino,但向它发送字符或字符串是令人沮丧的。这是我的密码: MainActivity.javaJava USB主机Android无法发送字符串,java,android,arduino,usb,usb-otg,Java,Android,Arduino,Usb,Usb Otg,我想让我的Android使用USB主机与Arduino通信。我尝试了很多参考资料,但似乎无法向我的Arduino发送任何字符。我可以检测到我的Arduino,但向它发送字符或字符串是令人沮丧的。这是我的密码: MainActivity.java public class MainActivity extends AppCompatActivity { UsbDevice device=null; UsbManager manager=null; PendingIntent mPermission
public class MainActivity extends AppCompatActivity {
UsbDevice device=null;
UsbManager manager=null;
PendingIntent mPermissionIntent;
UsbInterface intf;
UsbEndpoint endpoint;
UsbDeviceConnection connection;
Button find;
Button send;
TextView hello;
UsbManager mUsbManager;
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
private byte[] bytes;
private static int TIMEOUT = 0;
private boolean forceClaim = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
find = (Button) findViewById(R.id.Find);
send = (Button) findViewById(R.id.Send);
hello = (TextView) findViewById(R.id.hello);
find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hello.setText("");
checkInfo();
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(device!=null) {
intf = device.getInterface(0);
endpoint = intf.getEndpoint(0);
connection = mUsbManager.openDevice(device);
hello.setText("Kirim");
String kirim = "a";
bytes = kirim.getBytes();
connection.claimInterface(intf, forceClaim);
connection.bulkTransfer(endpoint, bytes, bytes.length, 0);
}
else{
Toast.makeText(MainActivity.this,"Device ==
null",Toast.LENGTH_SHORT);
}
}
});
private void checkInfo(){
manager=(UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this,0,new
Intent(ACTION_USB_PERMISSION),0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver,filter);
HashMap<String,UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
String i="";
int j=0;
while(deviceIterator.hasNext()){
device = deviceIterator.next();
manager.requestPermission(device,mPermissionIntent);
i += "\n" + "DeviceID: " + device.getDeviceId() + "\n"
+ "DeviceName: " + device.getDeviceName() + "\n"
+ "DeviceClass: " + device.getDeviceClass() + " - "
+ "DeviceSubClass: " + device.getDeviceSubclass() + "\n"
+ "VendorID: " + device.getVendorId() + "\n"
+ "ProductID: " + device.getProductId() + "\n";
j++;
}
hello.setText(i);
}
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) {
UsbDevice device = (UsbDevice) intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
// call method to set up device communication
}
} else {
Log.d("ERROR", "permission denied for device " +
device);
}
}
}
}
};
}
public类MainActivity扩展了AppCompatActivity{
UsbDevice设备=null;
UsbManager manager=null;
悬而未决的犯罪意图;
UsbInterface intf;
UsbEndpoint端点;
UsbDeviceConnection连接;
按钮查找;
按钮发送;
文本视图你好;
UsbManager musbanager;
私有静态最终字符串操作\u USB\u权限=
“com.android.example.USB_权限”;
专用字节[]字节;
私有静态int超时=0;
私有布尔forceClaim=true;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
find=(按钮)findviewbyd(R.id.find);
send=(按钮)findviewbyd(R.id.send);
hello=(TextView)findViewById(R.id.hello);
find.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
您好,setText(“”);
checkInfo();
}
});
send.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(设备!=null){
intf=device.getInterface(0);
endpoint=intf.getEndpoint(0);
连接=mUsbManager.openDevice(设备);
你好,setText(“Kirim”);
字符串kirim=“a”;
bytes=kirim.getBytes();
连接.claimInterface(intf,forceClaim);
连接.bulkTransfer(端点,字节,bytes.length,0);
}
否则{
Toast.makeText(MainActivity.this,“设备==
空”,吐司长度(短);
}
}
});
私有void checkInfo(){
manager=(UsbManager)getSystemService(Context.USB_服务);
mPermissionIntent=pendingent.getBroadcast(此,0,新
意图(操作权限),0);
IntentFilter筛选器=新建IntentFilter(操作\u USB\u权限);
寄存器接收器(mUsbReceiver,过滤器);
HashMap deviceList=manager.getDeviceList();
迭代器deviceIterator=deviceList.values().Iterator();
字符串i=“”;
int j=0;
while(deviceIterator.hasNext()){
device=deviceIterator.next();
manager.requestPermission(设备、mPermissionIntent);
i+=“\n”+“设备ID:”+device.getDeviceId()+“\n”
+DeviceName:“+device.getDeviceName()+”\n
+“DeviceClass:+设备。getDeviceClass()+”-“
+DeviceSubClass:“+device.getDeviceSubclass()+”\n
+VendorID:“+device.getVendorId()+”\n
+ProductID:“+device.getProductId()+”\n”;
j++;
}
你好,setText(我);
}
专用最终广播接收器mUsbReceiver=新广播接收器(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(动作\u USB\u权限等于(动作)){
已同步(此){
UsbDevice设备=(UsbDevice)意图
.getParcelableExtra(UsbManager.EXTRA_设备);
if(intent.getBooleanExtra)(
UsbManager.EXTRA_权限(已授予,错误)){
如果(设备!=null){
//调用方法来设置设备通信
}
}否则{
Log.d(“错误”,“设备权限被拒绝”+
装置);
}
}
}
}
};
}
我使用“查找”按钮进行迭代,使用“发送”按钮进行发送。迭代进行得很顺利,但发送不起作用。我只需要向Arduino发送一个字符。我的手机支持OTG
,并且已经使用其他串行通信应用程序测试过,可以向Arduino发送字符,效果很好
提前感谢您在以下位置使用发行版中的usb串行库:然后导入库并按照url所示安装依赖项。要解决此问题,您可以创建一个处理usb相关内容的服务类(有关详细信息,请参阅usb主机android文档: 详情如下:
java
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String auxi2="1"; // put whatever you need to send here
if (!auxi2.equals("")) {
String data = auxi2;
if (usbService != null) { // if UsbService was correctly binded, Send data
usbService.write(data.getBytes());
// Toast.makeText(getApplicationContext(),"auxi2 escrito"+ auxi2, Toast.LENGTH_SHORT).show();
}
}
}
});
公共类UsbService扩展服务{
公共静态最终字符串TAG=“UsbService”;
public static final String ACTION_USB_READY=“com.felhr.connectivityservices.USB_READY”;
公共静态最终字符串ACTION\u USB\u ATTACHED=“android.hardware.USB.ACTION.USB\u DEVICE\u ATTACHED”;
公共静态最终字符串ACTION\u USB\u DETACHED=“android.hardware.USB.ACTION.USB\u DEVICE\u DETACHED”;
public static final String ACTION_USB_NOT_SUPPORTED=“com.felhr.usbservice.USB_NOT_SUPPORTED”;
公共静态最终字符串操作\u NO\u USB=“com.felhr.usbservice.NO\u USB”;
公共静态最终字符串操作\u USB\u权限\u授予=“com.felhr.usbservice.USB\u权限\u授予”;
公共静态最终字符串操作\u USB\u权限\u未授予=“com.felhr.usbservice.USB\u权限\u未授予”;
公共静态最终字符串操作\u USB\u DISCONNECTED=“com.felhr.usbservice.USB\u DISCONNECTED”;
公共静态最终字符串ACTION\u CDC\u DRIVER\u NOT\u WORKING=“com.felhr.connectivityservices.ACTION\u CDC\u DRIVER\u NOT\u WORKING”;
public static final String ACTION\u USB\u DEVICE\u NOT\u WORKING=“com.felhr.connectivityservices.ACTION\u USB\u DEVICE\u NOT\u WORKING”;
来自串行端口的公共静态最终int消息=0;
公共静态最终int CTS_变更=1;
公共静态最终int DSR_变更=2;
私有静态最终字符串操作\u USB\u PERMISSION=“com.android.example.USB\u PERMISSION”;
私有静态最终整数波特率=9600;//波特率。如果需要,请更改此值
公共静态布尔服务_CONNECTED=false;
专用IBinder活页夹=新的UsbBinder();
普里瓦特
java
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String auxi2="1"; // put whatever you need to send here
if (!auxi2.equals("")) {
String data = auxi2;
if (usbService != null) { // if UsbService was correctly binded, Send data
usbService.write(data.getBytes());
// Toast.makeText(getApplicationContext(),"auxi2 escrito"+ auxi2, Toast.LENGTH_SHORT).show();
}
}
}
});