我想使用以下代码 (AndroidStudio Chipmunk) 将 USB CDC 设备(FTDI 芯片或 CD2010 或定制芯片)连接到我的 Galaxy A12 (Android SDK 30):
public class MainActivity extends AppCompatActivity {
public static final String ACTION_USB_PERMISSION = "com.example.usbtestapp.USB_PERMISSION";
static final int intentFlags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) ? PendingIntent.FLAG_IMMUTABLE : 0;
static final int MY_CAMERA_PERMISSION_CODE = 100;
private Context ContextofMainAcitivity;
public IntentFilter USBconnectdisconnect_intentFilter;
public Intent GetEXTRA_PERMISSION_GRANTEDIntent;
public PendingIntent current_usbpermissionIntent;
public UsbDevice current_usbdevice;
public UsbManager current_usbManager;
private static final String TAG = "usbtestapp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContextofMainAcitivity = this;
current_usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
USBconnectdisconnect_intentFilter = new IntentFilter();
USBconnectdisconnect_intentFilter.addAction(ACTION_USB_PERMISSION);
USBconnectdisconnect_intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
USBconnectdisconnect_intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(currentUSBBroasdcastReceiver, USBconnectdisconnect_intentFilter);
GetEXTRA_PERMISSION_GRANTEDIntent = new Intent(ACTION_USB_PERMISSION);
GetEXTRA_PERMISSION_GRANTEDIntent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
Log.v(TAG, "Waiting for USB devices...");
} // create
BroadcastReceiver currentUSBBroasdcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
current_usbdevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.v(TAG,"ACTION_USB_DEVICE_ATTACHED.");
if (current_usbManager.hasPermission(current_usbdevice)) {
Log.v(TAG,"Permission is already granted.");
return;
}
current_usbpermissionIntent = PendingIntent.getBroadcast(ContextofMainAcitivity, 0, GetEXTRA_PERMISSION_GRANTEDIntent , intentFlags);
if (!current_usbManager.hasPermission(current_usbdevice)) {
Log.v(TAG, "Request Permision from User...");
current_usbManager.requestPermission(current_usbdevice, current_usbpermissionIntent);
}
break;
case UsbManager.ACTION_USB_DEVICE_DETACHED:
Log.v(TAG,"ACTION_USB_DEVICE_DETACHED");
break;
case ACTION_USB_PERMISSION:
if (intent.getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED, false) == true) {
Log.v(TAG,"ACTION_USB_PERMISSION - Granted !");
} else {
Log.v(TAG,"ACTION_USB_PERMISSION - Denied");
}
break;
} //switch
} // onReceive
}; // braadcast
} // 类
连接 FTDI FT232 - CDC 设备后,会发生以下情况:
2022-08-07 14:19:44.206 15767-15767/com.social.usbtestapp V/usbtestapp:正在等待 USB 设备...
2022-08-07 14:19:50.765 15767-15767/com.social.usbtestapp V/usbtestapp:ACTION_USB_DEVICE_ATTACHED。
2022-08-07 14:19:50.770 15767-15767/com.social.usbtestapp V/usbtestapp:请求用户许可...
然后弹出一个对话框并显示设备字符串+请求用户许可 – 无论选择什么“确定”或“取消”,结果始终是:
2022-08-07 14:19:52.759 15767-15767/com.social.usbtestapp V/usbtestapp:ACTION_USB_PERMISSION – 被拒绝
我还检查了 FTDI 示例,但最终根据我的理解,它们的内部功能几乎相同。我还检查了 StackOverflow java - UsbDevice requestPermission 被拒绝而不显示请求对话框提示 - 堆栈内存溢出,但这并没有解决我的问题。
出于任何未知的原因,我也让这个工作正常,这意味着在权限对话框中选择“确定”后, getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED 确实是 true 同时添加或删除一些额外的 SDK,例如 Lolipop 或 Pie,但我无法再重现它 - 如果有任何建议,我会很高兴。
在intentFlags初始化中将PendingIntent.FLAG_IMMUTABLE更改为PendingIntent.FLAG_MUTABLE。我不知道为什么它必须是 FLAG_MUTABLE 但它解决了我的问题。
有人可以提供一个关于如何使用具有明确意图的 PendingIntent 和广播接收器的示例吗?我试过了:
Intent intent = new Intent(m_context, MyBroadcastReceiver.class);
intent.setAction(ACTION_USB_PERMISSION);
PendingIntent permissionIntent = PendingIntent.getBroadcast(m_context, 0, intent, PendingIntent.FLAG_MUTABLE);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
m_context.registerReceiver(mUsbReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
但是只有 USB_DEVICE_ATTACHED 事件到达 MyBroadcastReceiver,而不是权限事件(尽管按下“确定”时似乎已授予权限,因为下次我启动应用程序时它已获得权限)。