Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: usb hid about communication

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default usb hid about communication

    Hi ! ,I want to USB HID hardware communicates with android tablet but newbe java and android (good c#.net)


    -Question 1-knowledge of how to get the usb device attached to the tablet
    UsbDevice usbdeneme = getIntent().getParcelableExtra("usbdeneme"); - - -> I receive Null data

    -Question 2-"Parcelable" did not understand, is it true for "parcelable"

    -Question 3-Do you have a script file, you need to add manifest
    Ex: <uses-feature android:name="android.hardware.usb.host"/><uses-sdk

    -Question 4-How do I listen to a continuous data retrieval for USB HID

    - main code the following commands

    Java Code:

    package com.Usb_deneme;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends Activity {

    protected static final int Class = 0;
    Button button1;
    TextView textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = (Button) findViewById(R.id.button1);
    textView1 =(TextView) findViewById(R.id.textView1);


    button1.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View v) {
    try{

    UsbDevice usbdeneme = getIntent().getParcelableExtra("usbdeneme");

    if (usbdeneme != null){
    textView1.setText("Success --> " + usbdeneme.toString());
    }
    }
    catch(Exception ex) {textView1.setText("Error --> "+ex.toString());}

    }
    });





    }


    }


    - usb device class the following commands
    Java Code:

    package com.Usb_deneme;
    import android.hardware.usb.UsbInterface;
    //import android.os.Bundle;
    import android.os.Parcel;
    import android.os.Parcelable;
    //import android.util.Log;

    //import java.io.FileDescriptor;

    /**
    * A class representing a USB device.
    */
    public class UsbDevice implements Parcelable {

    private static final String TAG = "UsbDevice";

    private final String mName;
    private final int mVendorId;
    private final int mProductId;
    private final int mClass;
    private final int mSubclass;
    private final int mProtocol;
    private final Parcelable[] mInterfaces;

    /**
    * UsbDevice should only be instantiated by UsbService implementation
    * @hide
    */
    public UsbDevice(String name, int vendorId, int productId,
    int Class, int subClass, int protocol, Parcelable[] interfaces) {
    mName = name;
    mVendorId = vendorId;
    mProductId = productId;
    mClass = Class;
    mSubclass = subClass;
    mProtocol = protocol;
    mInterfaces = interfaces;
    }

    /**
    * Returns the name of the device.
    * In the standard implementation, this is the path of the device file
    * for the device in the usbfs file system.
    *
    * @return the device name
    */
    public String getDeviceName() {
    return mName;
    }

    /**
    * Returns a unique integer ID for the device.
    * This is a convenience for clients that want to use an integer to represent
    * the device, rather than the device name.
    * IDs are not persistent across USB disconnects.
    *
    * @return the device ID
    */
    public int getDeviceId() {
    return getDeviceId(mName);
    }

    /**
    * Returns a vendor ID for the device.
    *
    * @return the device vendor ID
    */
    public int getVendorId() {
    return mVendorId;
    }

    /**
    * Returns a product ID for the device.
    *
    * @return the device product ID
    */
    public int getProductId() {
    return mProductId;
    }

    /**
    * Returns the devices's class field.
    * Some useful constants for USB device classes can be found in
    * {@link android.hardware.usb.UsbConstants}
    *
    * @return the devices's class
    */
    public int getDeviceClass() {
    return mClass;
    }

    /**
    * Returns the device's subclass field.
    *
    * @return the device's subclass
    */
    public int getDeviceSubclass() {
    return mSubclass;
    }

    /**
    * Returns the device's subclass field.
    *
    * @return the device's protocol
    */
    public int getDeviceProtocol() {
    return mProtocol;
    }

    /**
    * Returns the number of {@link android.hardware.usb.UsbInterface}s this device contains.
    *
    * @return the number of interfaces
    */
    public int getInterfaceCount() {
    return mInterfaces.length;
    }

    /**
    * Returns the {@link android.hardware.usb.UsbInterface} at the given index.
    *
    * @return the interface
    */
    public UsbInterface getInterface(int index) {
    return (UsbInterface)mInterfaces[index];
    }

    @Override
    public boolean equals(Object o) {
    if (o instanceof UsbDevice) {
    return ((UsbDevice)o).mName.equals(mName);
    } else if (o instanceof String) {
    return ((String)o).equals(mName);
    } else {
    return false;
    }
    }

    @Override
    public int hashCode() {
    return mName.hashCode();
    }

    @Override
    public String toString() {
    return "UsbDevice[mName=" + mName + ",mVendorId=" + mVendorId +
    ",mProductId=" + mProductId + ",mClass=" + mClass +
    ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol +
    ",mInterfaces=" + mInterfaces + "]";
    }

    public static final Parcelable.Creator<UsbDevice> CREATOR =
    new Parcelable.Creator<UsbDevice>() {
    public UsbDevice createFromParcel(Parcel in) {
    String name = in.readString();
    int vendorId = in.readInt();
    int productId = in.readInt();
    int clasz = in.readInt();
    int subClass = in.readInt();
    int protocol = in.readInt();
    Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClass Loader());
    return new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, interfaces);
    }

    public UsbDevice[] newArray(int size) {
    return new UsbDevice[size];
    }
    };

    public int describeContents() {
    return 0;
    }

    public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeString(mName);
    parcel.writeInt(mVendorId);
    parcel.writeInt(mProductId);
    parcel.writeInt(mClass);
    parcel.writeInt(mSubclass);
    parcel.writeInt(mProtocol);
    parcel.writeParcelableArray(mInterfaces, 0);
    }

    public static int getDeviceId(String name) {
    return native_get_device_id(name);
    }

    public static String getDeviceName(int id) {
    return native_get_device_name(id);
    }

    private static native int native_get_device_id(String name);
    private static native String native_get_device_name(int id);
    }


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: usb hid about communication

    getParcelableExtra probably isn't what you want. Check out the USB Accessories API guide. You will need to install Google play services from the SDK manager and add this to the manifest:

    <uses-feature android:name="android.hardware.usb.accessory" />

Similar Threads

  1. Android to Pc usb communication
    By namınf in forum Android Development
    Replies: 4
    Last Post: May 22nd, 2013, 06:51 AM
  2. Java USB detection
    By wrightm96 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2012, 06:27 AM
  3. USB signal
    By mp3rara in forum Java Networking
    Replies: 3
    Last Post: May 6th, 2011, 03:57 AM
  4. [SOLVED] Detect USB signal
    By mine0926 in forum Java Native Interface
    Replies: 2
    Last Post: February 4th, 2011, 07:46 PM
  5. Java and usb
    By righi in forum Java Theory & Questions
    Replies: 1
    Last Post: January 28th, 2011, 10:21 AM