yo3ggx Active Member Licensed User Longtime User Dec 14, 2018 #1 I have an application using USB/Serial and USB audio devices. In the manifest.xml file I have the following section, used to make the app start when a specific USB serial device is connected: Code: AddActivityText(main, <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />) Then the resource file is created from manifest.xml too: Code: CreateResource(xml, device_filter.xml, <resources> <!-- 0x0403 / 0x6015: FTDI FT230XS UART --> <usb-device vendor-id="1027" product-id="24597" /> </resources> ) In the main code, I'm using BroadcastReceiver library to capture the following events: Code: Broadcast.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED") Broadcast.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED") I need this to handle the situations when USB devices are connected/disconnected when the application is running. UNfortunately because USB_DEVICE_ATTACHED event is captured through the manifest file, the BroadcastReceiver even is no more triggered inside the app for the same event, only for USB_DEVICE_DETACHED What can I do to capture this event from the app code? Thank you. Dan
I have an application using USB/Serial and USB audio devices. In the manifest.xml file I have the following section, used to make the app start when a specific USB serial device is connected: Code: AddActivityText(main, <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />) Then the resource file is created from manifest.xml too: Code: CreateResource(xml, device_filter.xml, <resources> <!-- 0x0403 / 0x6015: FTDI FT230XS UART --> <usb-device vendor-id="1027" product-id="24597" /> </resources> ) In the main code, I'm using BroadcastReceiver library to capture the following events: Code: Broadcast.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED") Broadcast.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED") I need this to handle the situations when USB devices are connected/disconnected when the application is running. UNfortunately because USB_DEVICE_ATTACHED event is captured through the manifest file, the BroadcastReceiver even is no more triggered inside the app for the same event, only for USB_DEVICE_DETACHED What can I do to capture this event from the app code? Thank you. Dan
Erel B4X founder Staff member Licensed User Longtime User Dec 16, 2018 #2 Don't add the dynamic filter. Try this: Add this code: Code: #if Java public void _OnNewIntent(Object intent) { BA.Log("" + intent); } #end if Do you see the message in the logs when you connect or disconnect a USB device? Upvote 0
Don't add the dynamic filter. Try this: Add this code: Code: #if Java public void _OnNewIntent(Object intent) { BA.Log("" + intent); } #end if Do you see the message in the logs when you connect or disconnect a USB device?
yo3ggx Active Member Licensed User Longtime User Dec 16, 2018 #3 If I don't add the filter in the manifest file, the event is triggered in BroadcastReceiver even without the java code. If I keep the USB_DEVICE_ATTACHED filter in manifest file, this event is no more captured in BroadcastReceiver, only the USB_DEVICE_DETACHED is. If I remove the event from manifest, then I'm asked to approve USB device each time I connect it, even if I choose to accept it always by default. Upvote 0
If I don't add the filter in the manifest file, the event is triggered in BroadcastReceiver even without the java code. If I keep the USB_DEVICE_ATTACHED filter in manifest file, this event is no more captured in BroadcastReceiver, only the USB_DEVICE_DETACHED is. If I remove the event from manifest, then I'm asked to approve USB device each time I connect it, even if I choose to accept it always by default.
yo3ggx Active Member Licensed User Longtime User Dec 16, 2018 #4 There is any other way to capture the event when audio goes from local speaker to the USB audio? This can solve my problem too, as I don't need to capture in my app when USB Serial device is connected, only when USB audio device is. Upvote 0
There is any other way to capture the event when audio goes from local speaker to the USB audio? This can solve my problem too, as I don't need to capture in my app when USB Serial device is connected, only when USB audio device is.
Erel B4X founder Staff member Licensed User Longtime User Dec 16, 2018 #5 I understand. Have you tried my suggestion? Upvote 0
yo3ggx Active Member Licensed User Longtime User Dec 16, 2018 #6 I've commented the following lines in manifest: Code: 'AddActivityText(main, <intent-filter> ' <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> ' </intent-filter> ' <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" ' android:resource="@xml/device_filter" />) Then I've added the java code you provided, but nothing is logged when connecting or disconnecting USB device. I'm not yet familiar with inline java code. I have to put it in a specific place? I have to comment BroadcastReceiver too? Upvote 0
I've commented the following lines in manifest: Code: 'AddActivityText(main, <intent-filter> ' <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> ' </intent-filter> ' <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" ' android:resource="@xml/device_filter" />) Then I've added the java code you provided, but nothing is logged when connecting or disconnecting USB device. I'm not yet familiar with inline java code. I have to put it in a specific place? I have to comment BroadcastReceiver too?
Erel B4X founder Staff member Licensed User Longtime User Dec 16, 2018 #7 Dynamic filter = BroadcastReciever (in the code). 1. Keep the filter you added with the manifest editor. 2. Add the code I posted to the main activity at the bottom. Upvote 0
Dynamic filter = BroadcastReciever (in the code). 1. Keep the filter you added with the manifest editor. 2. Add the code I posted to the main activity at the bottom.
yo3ggx Active Member Licensed User Longtime User Dec 16, 2018 #8 Done. Nothing logged when I attach the USB device. Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Dec 16, 2018 #9 You can use another activity to handle the intent. Start the main activity when the second activity is resumed. Code: AddActivityText(SecondActivity, <intent-filter>... This way the second activity will be resumed whenever the intent is intercepted. Upvote 0
You can use another activity to handle the intent. Start the main activity when the second activity is resumed. Code: AddActivityText(SecondActivity, <intent-filter>... This way the second activity will be resumed whenever the intent is intercepted.