Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android USB Host mode service - Start based on USB_DEVICE_ATTACHED

Tags:

android

host

usb

I want to write a service in Android which starts based on USB_DEVICE_ATTACHED intent. So, basically my service should start when a specific USB Device(FT232C - VID:PID 0403:6010) is connected and stop when that USB device is detached. Is it possible to do that or should I always have an Activity which starts this service in case it is not already started? The intent of the service in the end is to update the location on the LocationProvider with a TEST_PROVIDER based on what location is provided from this USB device.

I already tried creating a service with this configuration in AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.testlocservice"
        android:versionCode="1"
        android:versionName="1.0">

        <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="15" />
        <uses-feature android:name="android.hardware.usb.host"/>
        <supports-screens android:resizeable="true" android:smallScreens="true" android:anyDensity="true" android:largeScreens="true" android:xlargeScreens="true" android:normalScreens="true"/>
        <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

        <application android:label="@string/app_name"
            android:icon="@drawable/ic_launcher"
            android:theme="@style/AppTheme">
                <service android:name="com.testlocservice.LocationService"         android:process=":LocService" android:enabled="true" android:exported="true">
                    <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" />                
                </service>
        </application>
    </manifest>

The xml/device_filter.xml contains this

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <usb-device vendor-id="0403" product-id="6010"/>
    </resources>

My LocationService class has overridden onStartCommand() which handles the USB_DEVICE_ATTACHED intent

like image 300
user1400473 Avatar asked Nov 20 '12 03:11

user1400473


People also ask

Is USB host based?

Standard USB uses a Master/Slave architecture; a host acts as the Host device for the entire bus, and a USB device acts as a Peripheral.

What is Android USB host?

When your Android-powered device is in USB host mode, it acts as the USB host, powers the bus, and enumerates connected USB devices. USB host mode is supported in Android 3.1 and higher.


1 Answers

From my experience a <service> cannot receive the USB intents. I overcame this by creating a hidden activity to receive the intent and re-broadcast it. Of course this activity could also handle starting/stopping your service.

I have already put up some working code here:
https://stackoverflow.com/a/15151075/588476
You will just have to change it so it starts and stops your service automatically.

like image 77
Wayne Uroda Avatar answered Sep 21 '22 19:09

Wayne Uroda