重新启动后USB附件无法通信 [英] USB Accessory not communicating after restart
问题描述
我试图弄清楚为什么手机重启后我的应用程序无法开始与USB附件通信.如果拔下电缆,然后重新插入,通信将继续.该应用程序针对Android API 19.
I'm trying to figure out why my application does not start communicating with the USB accessory after the phone restarts. If I unplug the cable and plug it back in, the communication resumes. The application is targeted at Android API 19.
在安装应用程序时,我将其设置为始终为Home应用程序,当我首次将其连接到附件时,请选中该框以始终允许访问当前附件.
At the app install I set it to always be the Home app and when I first connect it to the accessory I check the box to always permit the access to the current accessory.
因此,当我重新启动手机时,该应用程序会自动打开,它会执行检查权限(usbmanager.hasPermission
)的步骤,并且实际上具有该权限,而不会出现任何警报,但是通讯不是在
So when I restart the phone, the app opens up automatically, it goes through the steps of checking for permission (usbmanager.hasPermission
) and it actually has the permission without any alert appearing, but the communication is not starting in OpenAccessory
method.
清单:
<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"></meta-data>
通讯:
// Init USB Manager
usbmanager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
context.registerReceiver(mUsbReceiver, filter);
// Resume Accessory
if (usbmanager.hasPermission(accessory)) {
OpenAccessory(accessory);
}
else
{
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
Toast.makeText(global_context, "Request USB Permission", Toast.LENGTH_SHORT).show();
usbmanager.requestPermission(accessory,
mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
// Broadcast Receiver
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_SHORT).show();
OpenAccessory(accessory);
}
else
{
Toast.makeText(global_context, "Deny USB Permission", Toast.LENGTH_SHORT).show();
Log.d("LED", "permission denied for accessory "+ accessory);
}
mPermissionRequestPending = false;
}
}
else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action))
{
DestroyAccessory(true);
//CloseAccessory();
}else
{
Log.d("LED", "....");
}
}
};
// OpenAccessory
private void OpenAccessory(UsbAccessory accessory)
{
filedescriptor = usbmanager.openAccessory(accessory);
if(filedescriptor != null){
usbaccessory = accessory;
FileDescriptor fd = filedescriptor.getFileDescriptor();
inputstream = new FileInputStream(fd);
outputstream = new FileOutputStream(fd);
/*check if any of them are null*/
if(inputstream == null || outputstream==null){
return;
}
if(READ_ENABLE == false){
READ_ENABLE = true;
readThread = new read_thread(inputstream);
readThread.start();
}
// Initialize the serial port here
SerialComm.getInstance().initPort();
}
}
推荐答案
在UsbManager
和IntentFilter
中,您使用在插入或拔出设备时触发的ACTION_USB_ACCESSORY_DETACHED
和ACTION_USB_ACCESSORY_ATTACHED
事件.
in UsbManager
and IntentFilter
you use ACTION_USB_ACCESSORY_DETACHED
and ACTION_USB_ACCESSORY_ATTACHED
events that are triggered when plugging or unplugging the device.
如果发生这些事件/操作,则代码将被执行并且应用可以正常工作
if these events /actions occur the code is executed and the app works
( https://developer.android.com/reference/android/hardware/usb/UsbManager.html )
现在的问题是,在重新启动应用程序时启动这些事件/操作不会触发,因此链接到它们的代码将不会执行
the problem is now that when the app is started at reboot these events /actions are not triggered and so the code that is linked to them will not be executed
绕开它并不容易,因为应用程序仅通过ACTION_USB_ACCESSORY_ATTACHED
意图获得UsbAccessory
类的实例:
it is not easy to circumvent this because an application gets an instance of the UsbAccessory
class only via the ACTION_USB_ACCESSORY_ATTACHED
intent :
此类的实例通过以下方式发送到应用程序: ACTION_USB_ACCESSORY_ATTACHED目的.然后,应用程序可以调用 openAccessory(UsbAccessory)打开文件描述符以进行读取和 在附件中写入数据.
An instance of this class is sent to the application via the ACTION_USB_ACCESSORY_ATTACHED Intent. The application can then call openAccessory(UsbAccessory) to open a file descriptor for reading and writing data to and from the accessory.
source: https://developer.android.com/reference /android/hardware/usb/UsbAccessory.html
如果应用没有类,则该应用将无法使用该类(附件)...
it is not possible for the app to use a class ( the accessory ) if it has no instance of it ...
这篇关于重新启动后USB附件无法通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!