signalwareltd / rtl_tcp_andro-

rtl_tcp and libusb-1.0 port for Android modified to support opening devices from Linux file descriptors
GNU General Public License v2.0
347 stars 118 forks source link

Add support for UsbManager.USB_DEVICE_ATTACHED action #15

Closed twoofseven closed 7 years ago

twoofseven commented 7 years ago

Add support for UsbManager.USB_DEVICE_ATTACHED action as described in https://developer.android.com/guide/topics/connectivity/usb/host.html chapter "Working with Devices". This enabled the driver to receive the UsbManager.ACTION_USB_DEVICE_ATTACHED intent, which will be forwarded as ACTION_SDR_DEVICE_ATTACHED intent. The receiving application uses the already existing mechanism as described within the README.md to start the RTL-SDR driver. But to ensure that the RTL-SDR driver initialises the newly attached USB device, the application needs to add the received USB device from the ACTION_SDR_DEVICE_ATTACHED action to the new intent as follows:

    public static final String ACTION_SDR_DEVICE_ATTACHED = "com.sdrtouch.rtlsdr.SDR_DEVICE_ATTACHED";
    ...
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();

        if (intent.getAction().equals(ACTION_SDR_DEVICE_ATTACHED)) {
            UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            Log.i(TAG, "USB attached: " + usbDevice.getDeviceName());

            Intent newIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("iqsrc://-a 127.0.0.1 -p "+ port + " -s "+ samplerate));
            newIntent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);
            startActivityForResult(newIntent, 1234);
        }
    }

The following intent filter need to be added to receive the ACTION_SDR_DEVICE_ATTACHED intent:

    <intent-filter>
        <category android:name="android.intent.category.DEFAULT"/>
        <action android:name="com.sdrtouch.rtlsdr.SDR_DEVICE_ATTACHED"/>
    </intent-filter>