Android connectivity-USB slave mode (4)

Source: Internet
Author: User

Get the permission to communicate with attachments

Your application must be licensed from the user before you can communicate with the USB attachment device.

Note:If your application uses the intent filter to discover the connected attachment devices, and the user allows your application to process the intent object, it will automatically receive the license. Otherwise, you must explicitly apply for an access license before connecting to the attachment device.

Explicitly applying for an access license is required in some cases. For example, if your application lists the connected Accessory devices and wants to communicate with one of them. Before trying to communicate with the attachment, you must check the permission to access the attachment device. Otherwise, if the user rejects your request to access the attachment device, you will receive a runtime error.

To explicitly obtain the license, you must first create a broadcast receiver. This receiver listens to the intent object that is used for broadcast when the requestpermission () method is called. When you call the requestpermission () method, a dialog box is displayed, requesting the user the permission to connect to the attachment device. The following code creates a broadcast receiver:

Private Static final string action_usb_permission =


"Com. Android. example. usb_permission ";

Private Final broadcastreceiver musbreceiver = new broadcastreceiver (){

 


Public void onreceive (context, intent ){


String action = intent. getaction ();


If (action_usb_permission.equals (Action )){


Synchronized (this ){


Usbaccessory accessory = (usbaccessory) intent. getparcelableextra (usbmanager. extra_accessory );

 


If (intent. getbooleanextra (usbmanager. extra_permission_granted, false )){


If (accessory! = NULL ){


// Call method to set up accessory Communication


}


}


Else {


Log. D (TAG, "Permission denied for accessory" + accessory );


}


}


}


}

};

Register the broadcast receiver in the oncreate () method of your activity:

Usbmanager musbmanager = (usbmanager) getsystemservice (context. usb_service );

Private Static final string action_usb_permission =


"Com. Android. example. usb_permission ";

...

Mpermissionintent = pendingintent. getbroadcast (this, 0, new intent (action_usb_permission), 0 );

Intentfilter filter = new intentfilter (action_usb_permission );

Registerreceiver (musbreceiver, filter );

Call the requestpermission () method to display the dialog box for permission to access the attachment device:

Usbaccessory accessory;

...

Musbmanager. requestpermission (accessory, mpermissionintent );

When the user responds to this dialog box, your broadcast receiver will receive an intent object containing the extra_permission_granted type attachment, which uses a Boolean value to represent the answer result. Before connecting to the attachment device, check whether the attachment value is true.

Communicate with an attachment device

You can use the usbmanagerd object to obtain a file descriptor to communicate with the attachment device. You can use this file descriptor to create an input and output stream for reading and writing data. The stream represents the input and data block endpoints of the attachment device. You should establish communication between devices and attachments in another thread so as not to block the main UI thread. The following example shows how to enable the communication with the attachment device:

Usbaccessory maccessory;

Parcelfiledescriptor mfiledescriptor;

Fileinputstream minputstream;

Fileoutputstream moutputstream;

...

Private void openaccessory (){


Log. D (TAG, "openaccessory:" + accessory );


Mfiledescriptor = musbmanager. openaccessory (maccessory );


If (mfiledescriptor! = NULL ){


Filedescriptor FD = mfiledescriptor. getfiledescriptor ();


Minputstream = new fileinputstream (FD );


Moutputstream = new fileoutputstream (FD );


Thread thread = new thread (null, this, "accessorythread ");


Thread. Start ();


}

}

In the run () method of the thread, you can use fileinputstream and fileoutputstream objects to read and write attachment devices. When you use a fileinputstream object to read data from the attachment device, make sure that there is a large enough cache to store USB packet data. The maximum package cache size supported by the android slave mode protocol is 16384 bytes, so you can simply declare your cache to this size.

Note:At the underlying layer, for the 64-byte package of the full-speed USB attachment device and the 512-byte package of the high-speed USB attachment device, the android slave mode protocol will simply package the two data into a logical package.

Terminate the communication with the accessory device

When you complete the communication with the attachment device or the attachment device is pulled out, you must call the close () method to disable the opened file descriptor. Create the following broadcast receiver to listen for separation events:

Broadcastreceiver musbreceiver = new broadcastreceiver (){


Public void onreceive (context, intent ){


String action = intent. getaction ();

 


If (usbmanager. action_usb_accessory_detached.equals (Action )){


Usbaccessory accessory = (usbaccessory) intent. getparcelableextra (usbmanager. extra_accessory );


If (accessory! = NULL ){


// Call your method that cleans up and closes communication with the accessory


}


}


}

};

A broadcast receiver created in an application that is not included in the list file allows your application to process separation events only at runtime. In this way, the separation event is only sent to the currently running application and not broadcast to all applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.