Android USB Permissions Dialog never appears - android

I've written a simple app to send commands to a USB printer connected to an Android 4.0 tablet via USB. For some reason, I am unable to get past obtaining permissions to claim an interface and open a connection. Here's the relevant code :
public class TestPrintActivity extends Activity {
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private UsbInterface mInterface;
private UsbEndpoint mBulkIn;
private UsbEndpoint mBulkOut;
private static final String ACTION_USB_PERMISSION =
"com.demo.xprinter.USB_PERMISSION";
private PendingIntent mPermissionIntent;
private BroadcastReceiver mUsbReceiver;
#Override
private static final String ACTION_USB_PERMISSION =
"com.demo.printerDemo.USB_PERMISSION";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_print);
mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
registerReceiver(mUsbReceiver, filter);
mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
//call method to set up device communication
openPort(device);
}
}
else {
Toast.makeText(getApplicationContext(), "Denied!", Toast.LENGTH_SHORT).show();
}
}
}
}
};
}
public void onResume() {
super.onResume();
HashMap<String,UsbDevice> deviceList = mUsbManager.getDeviceList();
for (HashMap.Entry<String,UsbDevice> entry : deviceList.entrySet()) {
UsbDevice aDevice = entry.getValue();
if ((aDevice.getProductId() == 8965) && (aDevice.getVendorId() == 1659) ){
if (mUsbManager.hasPermission(aDevice))
openPort(aDevice);
else
mUsbManager.requestPermission(aDevice, mPermissionIntent);
break;
}
}
}
Here's the relevant part of the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.printerDemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-feature android:name="android.hardware.usb.host"/>
I've configured the device so that it launches my app whenever the printer is connected, and the device is found during the enumeration (onResume()) and the request for permission is called. However, for whatever reason, I never see the "Request Permission" dialog (I've seen screenshots of it online) nor does onReceive() ever get called. Any ideas why this might be happening?

1) Why do you have two members ACTION_USB_PERMISSION declared?
2) In onCreate() try to register your Intent like this, maybe there is a difference:
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(ACTION_USB_PERMISSION);
context.registerReceiver(usbReceiver, filter);
3) Due to a bug in Android (http://code.google.com/p/android/issues/detail?id=25703), I'm not sure if ACTION_USB_ACCESSORY_ATTACHED action is ever called. Try to add the below in your AndroidManifest.xml:
<activity ...>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="#xml/device_filter"/>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

So it turns out that SystemUI.apk/SystemUI.odex in /system/ had been change to SystemUI.apk.backup/SystemUI.odex.backup respectively. This presumably was to prevent some aspects of the System UI from mucking up the intended "kiosk" mode of the device. The logcat entry in my previous comment was the big clue. Once the filenames were restored, I started seeing the Permission dialog.

Related

USB Permissions not received by Broadcast receiver

Good evening community, I am reaching with the hopes of being educated about the following problem.
My intention with this code is to be able to handle USB permission intents in a receiver registered in a manifest file. The receiver gets USB Attached and detached actions, but not USB permissions when the user either accepts or declines the prompt.
Here is the code for the manifest, receiver and an activity to send the permissions request to the USB manager. And Finally, my target SDK is 28.
Any help is very much appreciated. Thank you very much.
public class BroadcastReceiver extends android.content.BroadcastReceiver{
public static final String USB_DEVICE_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
public static final String USB_DEVICE_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
public static final String USB_PERMISSION ="com.android.example.USB_PERMISSION";
#Override
public void onReceive(Context context, Intent intent) {
Context applicationContext = context.getApplicationContext();
try{
if (intent != null) {
String action = intent.getAction();
if (!TextUtils.isEmpty(action)) {
if (action.equals(USB_DEVICE_ATTACHED) || action.equals(USB_PERMISSION)){
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
UsbManager usbManager = (UsbManager) applicationContext.getSystemService(Context.USB_SERVICE);
if (action.equals(USB_DEVICE_ATTACHED)){
if (!usbManager.hasPermission(device)){
intent.setAction(USB_PERMISSION);
intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
intent.setClass(applicationContext, PermissionActivity.class);
applicationContext.startActivity(intent);
Toast.makeText(applicationContext, "Device Attached.", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(applicationContext, "Permissions already assigned", Toast.LENGTH_LONG).show();
}
}
else if (action.equals(USB_PERMISSION)){
if (usbManager.hasPermission(device)){
Toast.makeText(applicationContext, "USB Permissions are granted.", Toast.LENGTH_LONG).show();
}
}
}
else if (action.equals(USB_DEVICE_DETACHED)) {
Toast.makeText(applicationContext, "Device Detached.", Toast.LENGTH_LONG).show();
}
}
}
}
catch(Exception e){
Toast.makeText(applicationContext, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Here is the activity:
public class PermissionActivity extends android.support.v7.app.AppCompatActivity {
public static final String USB_PERMISSION ="com.android.example.USB_PERMISSION";
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context applicationContext = this.getApplicationContext();
Intent intent = getIntent();
if (intent != null )
{
if (intent.getAction().equals(USB_PERMISSION)){
if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false )) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(applicationContext, 0, new Intent(USB_PERMISSION), 0);
mUsbManager.requestPermission(device, mPermissionIntent);
Toast.makeText(applicationContext, "Requesting Permission", Toast.LENGTH_LONG).show();
}
}
}
}
finish();
}
#Override
protected void onResume() {
super.onResume();
}
}
And finally, the manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.usbtest">
<uses-feature android:name="android.hardware.usb.host"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PermissionActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize"
android:excludeFromRecents="true"
android:exported="true"
android:noHistory="true"
android:process=":UsbEventReceiverActivityProcess"
android:taskAffinity="com.example.taskAffinityUsbEventReceiver"
android:theme="#style/Theme.AppCompat.Translucent">
<intent-filter>
<action android:name="com.android.example.USB_PERMISSION"/>
</intent-filter>
</activity>
<receiver android:name=".BroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
<action android:name="com.android.example.USB_PERMISSION"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
I found the problem. Since Android 8.0, there are more restrictions with manifest-declared
broadcast receivers and the type of actions that can be received. The USB Permissions action is not part of the limited list of actions that can be received. Here are some links regarding this issue.
https://developer.android.com/guide/components/broadcasts#context-registered-recievers
https://developer.android.com/guide/components/broadcast-exceptions

how to detect device connect on android?

I want to detect my connected device using an app on android.
I want to detect a keyboard, mouse, and flash drive.
I am currently using the hwinfo command and Timer
//keyboard detect class.
public class detectService extends Service {
static Process hwinfo;
static String keyboard = "";
private Handler handler;
private Timer timer;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
handler = new Handler();
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
String[] cmd = new String[] {"su", "-c", "hwinfo --keyboard | grep - i 'keyboard'"};
try {
hwinfo = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(hwinfo.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
line = line.trim().toLowerCase();
keyboard = line;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
if (keyboard.contains("keyboard")) {
timer.cancel();
Intent intent = new Intent(this, keyboardDialog.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
}
};
timer = new Timer("Service");
timer.scheduleAtFixedRate(timerTask, 0 , 4000);
}
This source code detects the keyboard just fine.
But it executes every 4 second.
I want to not use a Timer when I connect a keyboard, mouse, and flash drive.
Detect devices connected on android.
Event detect, or receiver.
How to not use Timer for detecting devices connected on android?
I am going to illustrate USB device detection code. Hope, it will help you.
Step 1 : To ask for permission to access the USB port, which is done in our manifest file like:
<uses-feature android:name="android.hardware.usb.host" />
Step 2 : USB configuration in the Manifest file
<activity
android:name="yourPackageName.MainActivity"
android:label="#string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data android:name=
"android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
<meta-data android:name=
"android.hardware.usb.action.USB_DEVICE_DETACHED"
android:resource="#xml/device_filter" />
</activity>
Step 3 : Create file under res/xml and name it device_filter
paste below code in it :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" />
<!-- 0x2341 / Arduino -->
<usb-device vendor-id="9025" />
</resources>
Step 4 : Security and user permission to connect to a USB device
Since our App does not know if the user has already granted permission, we need to check the user permission flag always, every time that we want to start a USB connection:
void checkUSB(){
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
// Get the list of attached devices
HashMap<String, UsbDevice> devices = manager.getDeviceList();
// Iterate over all devices
Iterator<String> it = devices.keySet().iterator();
while (it.hasNext()) {
String deviceName = it.next();
UsbDevice device = devices.get(deviceName);
String VID = Integer.toHexString(device.getVendorId()).toUpperCase();
String PID = Integer.toHexString(device.getProductId()).toUpperCase();
if (!manager.hasPermission(device)) {
private static final String ACTION_USB_PERMISSION = "yourPackageName.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
manager.requestPermission(device, mPermissionIntent);
return;
} else {
... //user permission already granted; prceed to access USB device
}
}
}
That's it!!
Happy Coding :-)

How to detect attached USB device with UsbManager?

I am new to android programming, my main aim is to communicate over USB to an MCU using FT200XD USB to I2C bridge.
First I am trying to detect attached USB device via the UsbManager. From what I understand, at on create a popup window should ask permission to connect from the user but no permission is asked. While debugging its clear that the control doesn't go into the broadcast receiver section.
I have refereed few example code snippet and wrote the code below. I don't know what I am doing wrong.
I have downloaded an app called"USB host Controller" which does detect the FT200XD. Which means my tablet has the USB host functionality. It will be great if you can point me to the right direction or an entire working code can be shared.
My code is as follows:
Java file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent= PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(UsbManager.EXTRA_PERMISSION_GRANTED);
filter.addAction(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
}
// Broadcast receiver
public class mUsbReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(),
"Inside USB Broadcast", Toast.LENGTH_SHORT).show();
}
}
Manifest file part:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.usb.host" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="mUsbReceiver">
<intent-filter>
<action android:name="android.hardware.usb.action.ACTION_USB_PERMISSION"/>
</intent-filter>
</receiver>
<activity
android:name="com.example.usb.FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<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" />
</activity>
</application>
</manifest>
Device_filter.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" />
<!-- 0x2341 / Arduino -->
<usb-device vendor-id="9025" />
<!-- 0x16C0 / 0x0483: Teensyduino -->
<usb-device vendor-id="5824" product-id="1155" />
<!-- 0x617 / 0x000b: EFPL CC2531 -->
<usb-device vendor-id="1559" product-id="11" />
<!-- vendor-id="0x0403" product-id="0x6015" // converted to Int vendor ID and product ID of my FT200XD-->
<usb-device vendor-id="1027" product-id="24597" />
</resources>
You probably want to look at the return code for registerReceiver - from what you are posting in your code I'd assume it would be failing since you don't actually instantiate your mUsbReceiver. Take a look at this code, which is from extracted from an application I've written that works, notice the difference in the way I'm setting up my BroadcastReceiver, this also shows how to request permission if your device is inserted:
PendingIntent mPermissionIntent = null;
UsbManager mUsbManager = null;
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (ACTION_USB_PERMISSION.equals(action)) {
// Permission requested
synchronized (this) {
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
// User has granted permission
// ... Setup your UsbDeviceConnection via mUsbManager.openDevice(usbDevice) ...
} else {
// User has denied permission
}
}
}
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
// Device removed
synchronized (this) {
// ... Check to see if usbDevice is yours and cleanup ...
}
}
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
// Device attached
synchronized (this) {
// Qualify the new device to suit your needs and request permission
if ((usbDevice.getVendorId() == MY_VID) && (usbDevice.getProductId() == MY_PID)) {
mUsbManager.requestPermission(usbDevice, mPermissionIntent);
}
}
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
// ... App Specific Setup Here ...
mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
// Register an intent filter so we can get permission to connect
// to the device and get device attached/removed messages
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbReceiver, filter);
// ... More App Specific Setup ...
}
Also - it appears that for my app I did not need the extra intent-filter or meta-data XML for the USB actions.

Android: Receive UsbDevice from intent

I'm messing about with the USB host, and following the guidelines on the Android Developers site I've managed to create a Hello World that starts up once a particular USB device is plugged in. However, when I try and "...obtain the UsbDevice that represents the attached device from the intent" it returns null:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
// device is always null
if (device == null){Log.i(TAG,"Null device");}
Here's my manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="#xml/device_filter" />
</activity>
</application>
And my xml/device_filter.xml (I know these are the correct VID and PID because I've got a similar app working using the enumeration method described on the Android Developers site):
<resources>
<usb-device vendor-id="1234" product-id="1234"/>
</resources>
When your application is (re-)started due to the USB device attach event, then the device is passed on to the intent when onResume is called. You can get to it using the getParcelableExtra method. For example:
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (intent != null) {
Log.d("onResume", "intent: " + intent.toString());
if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (usbDevice != null) {
Log.d("onResume", "USB device attached: name: " + usbDevice.getDeviceName());
I found a workaround (or the intended usage?) thanks to Taylor Alexander. Basically, The way I understand it is that firing the intent that opens the application only opens the application. After that you have to search for and access usb devices as per the Enumerating Devices section of the Android Developers page in the onResume method.
#Override
public void onResume() {
super.onResume();
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
// Your code here!
}
I'm not convinced this is the RIGHT way to do it, but it seems to be working. If anyone has any further suggestions I'd be glad to listen.

How to detect USB device in Android

I have USB host android device for that I need to connect USB device. to detect usb device to host I written following code.
public class ReadData extends Activity {
UsbManager usbManager;
PendingIntent mPermissionIntent;
UsbDevice usbDevice;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_data);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
final String ACTION_USB_PERMISSION =
"com.example.udevice.USB_PERMISSION";
IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED");
registerReceiver(mUsbReceiver, filter);
}
private static final String ACTION_USB_PERMISSION =
"com.example.udevice.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
usbManager.requestPermission(usbDevice, mPermissionIntent);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(usbDevice != null){
//call method to set up device communication
int deviceId = usbDevice.getDeviceId();
int productId = usbDevice.getProductId();
Log.i("device id", "****"+deviceId);
Log.i("product id", "****"+productId);
}else{
Log.i("device id", "No USB device");
}
}
else {
Log.d("shiv", "permission denied for device ");
}
}
}
}
};
and manifest is like below:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.udevice"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ReadData"
android:label="#string/title_activity_heat_con" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</activity>
</application>
</manifest>
device_filter.xml
<resources>
<usb-device vendor-id="67b"
product-id="2303"/>
</resources>
in above xml file I added device attributes. I am expecting a broadcast intent whenever USB device connected to host device. but it is not happening. What is wrong with above code.
Thanks
shiv
I think you need to add:
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
It is described here
There is onething you are doing it wrong.
The vendor id and device id should be in decimals not in hex. For example, you need to define as follows
<resources>
<usb-device vendor-id="1659"
product-id="8963"/>
</resources>
I converted your device id and vendor-id from hex to decimal
Let me know if this helps

Categories

Resources