0% found this document useful (0 votes)
74 views20 pages

Pl2303hxd Android SW v1 01

This document discusses code for implementing a USB serial port driver on Android. It includes declarations for classes to define baud rate, data bits, flow control, parity and stop bits. It also covers dynamically registering a broadcast receiver to detect USB device attachments/detachments, using PendingIntents to request USB permissions, reading/writing serial data, and implementing a drop-down menu with a spinner to select baud rates.

Uploaded by

Puri Purwantari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views20 pages

Pl2303hxd Android SW v1 01

This document discusses code for implementing a USB serial port driver on Android. It includes declarations for classes to define baud rate, data bits, flow control, parity and stop bits. It also covers dynamically registering a broadcast receiver to detect USB device attachments/detachments, using PendingIntents to request USB permissions, reading/writing serial data, and implementing a drop-down menu with a spinner to select baud rates.

Uploaded by

Puri Purwantari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

PL2303HXD Android Development

Package
Agenda

 PL2303Driver Class Declaration


 Documents Index
 PL2303Driver Class
 PL2303Driver.BaudRate Class
 PL2303Driver.DataBits Class
 PL2303Driver.FlowControl Class
 PL2303Driver.Parity Class
 PL2303Driver.StopBits Class
Home
 Implementing USB Interface in Android
Dynamically registered BroadcastReceiver
PendingIntent
Read Data
Write Data
 The Drop-Down Menu Program

Home
PL2303Driver Class Declaration

Home
Documents Index

 PL2303HXD_Android_v1000\doc\Index.html
To develop Android AP need to refer to the doc files, the start file
as index.html

Home
PL2303Driver Class

Home
PL2303Driver.BaudRate Class

Home
PL2303Driver.DataBits Class

Home
PL2303Driver.FlowControl Class

Home
PL2303Driver.Parity Class

Home
PL2303Driver.StopBits Class

Home
Implementing USB Interface in Android

 Implementation of a broadcast receiver


(BroadcastReceiver) detect USB interface.

 Android framework layer TelephonyManager


underlying remote service tracking, final
PendingIntent to track.

 http://torvafirmus-android.blogspot.tw/

 SMS Example: Use registerReceiver, SmsManager, PendingIntent


http://shung007.blogspot.tw/2012/04/tqc-android-3-7-use-registerreceiver.html
Home
Dynamically registered
BroadcastReceiver

/* listen for new devices */


IntentFilter filter = new IntentFilter();

/* BroadcastReceiver specified action, that is to listen for the


message name */
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED );
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);

/* Register listener*/
registerReceiver(mUsbReceiver, filter);

Home
PendingIntent

/* It is the starting point for controlling HW */


mSerial = new PL2303Driver((UsbManager)
getSystemService(Context.USB_SERVICE));

/* Register the intent */


PendingIntent permissionIntent =
PendingIntent.getBroadcast(this, 0, new Intent(
ACTION_USB_PERMISSION), 0);
mSerial.setPermissionIntent(permissionIntent);

Home
/* BroadcastReceiver when insert/remove the device USB plug into/from a
USB port */
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (UsbManager.ACTION_USB_DEVICE_ATTACHED.
equals(action)) {
mBaudrate = loadDefaultBaudrate();
mSerial.begin();
}
Home
else if(UsbManager.ACTION_USB_DEVICE_DETACHED.
equals(action)) {
mSerial.usbDetached(intent);
mSerial.end();
}
else if (ACTION_USB_PERMISSION.equals(action))
{
/* A permission response has been received, validate if the
user has GRANTED, or DENIED permission */

Home
if (!mSerial.isConnected()) {
mBaudrate = loadDefaultBaudrate();
mSerial.begin();
loadDefaultSettingValues();
mTvSerial.setTextSize(mTextFontSize);
}
}

unregisterReceiver(mUsbReceiver); /* Cancel listener */

Home
Read Data

private void readDataFromSerial() {


.
.
if(!mSerial.isConnected()) return;

len = mSerial.read(rbuf);
.
.
}

Home
Write Data

private void writeDataToSerial() {

if(!mSerial.isConnected()) return;

String strWrite = etWrite.getText().toString();


if (SHOW_DEBUG) {
Log.d(TAG, "PL2303Driver Write(" + strWrite.length() + ") :
" + strWrite);
}
mSerial.write(strWrite.getBytes(), strWrite.length());
}
Home
The Drop-Down Menu Program
Src\PL2303HXDSimpleTest.java
PL2303HXD_BaudRate_spinner =
(Spinner)findViewById(R.id.spinner1);

adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);

PL2303HXD_BaudRate_spinner.setAdapter(adapter);

http://www.myandroid.tw/bbs-topic-26.sea
Res\layout\activity_pl2303_hxdsimple_test.xml

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:background= Home
"@android:drawable/btn_dropdown" />

You might also like