我想做的是
- 创建一个应用程序的windows连接android手机。
- 发送和接收数据。
- Windows(C#)是主机设备--〉安卓UsbAccessory.
C编号:
现在我可以选择COM5(Android手机)并连接到它,使用以下代码:
serialPort1.PortName = this.devicesList.Text; // COM5
serialPort1.Open();
addToList("SerialPort is opened", Color.Green);
Java(安卓手机):
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbAccessory[] accessories = manager.getAccessoryList();
accessories
始终为空,我尝试了找到的每一个代码,但我做不到。
我做错了什么
谢谢。
:::编辑:::
Android Studio 代码(Java)
package com.example.camerausb;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class UsbConnector extends Activity {
private static final String ACTION_USB_PERMISSION = "com.example.camerausb.UsbConnector.USB_PERMISSION";
public UsbManager usbManager;
public UsbAccessory usbAccessory;
public PendingIntent mPermissionIntent;
public Context global_context;
private IntentFilter filter;
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
Toast.makeText(global_context, "Allow Usb Permission", Toast.LENGTH_LONG).show();
// TODO:: open Accessory method
} else {
Toast.makeText(global_context, "Deny Usb Permission", Toast.LENGTH_LONG).show();
}
}
}
else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
Toast.makeText(global_context, "Usb detached", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(global_context, "something else", Toast.LENGTH_LONG).show();
}
}
};
public UsbConnector(Context context) {
super();
global_context = context;
Toast.makeText(global_context, "started", Toast.LENGTH_LONG).show();
usbManager = (UsbManager) global_context.getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(global_context, 0, new Intent(ACTION_USB_PERMISSION), 0);
filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
usbManager.requestPermission(usbAccessory, mPermissionIntent);
global_context.registerReceiver(mUsbReceiver, filter);
}
public BroadcastReceiver getReceiver() {
return this.mUsbReceiver;
}
public IntentFilter getFilter() {
return this.filter;
}
public void resumeAccessory(Intent intent) {
// Intent intent = getIntent();
String action = intent.getAction();
Toast.makeText(global_context, "resume: " + action, Toast.LENGTH_LONG).show();
UsbAccessory[] accessories = usbManager.getAccessoryList();
if (accessories != null) {
Toast.makeText(global_context, "Accessory not NULL", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(global_context, "Accessory is NULL", Toast.LENGTH_LONG).show();
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.camerausb">
<uses-sdk android:minSdkVersion="12" />
<uses-feature android:name="android.hardware.usb.accessory" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CameraUsb">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter" />
</activity>
</application>
</manifest>
xml/附件过滤器:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-accessory model="DemoKit" manufacturer="Google" version="1.0" />
</resources>
1条答案
按热度按时间zsbz8rwp1#
有人告诉我添加
usbManager.requestPermission(usbAccessory, mPermissionIntent);
手机屏幕变黑了,祝你好运!