Android-SerialPort-API is downloaded online. If the original author sees it, please leave a name. Thank you.
The following are essays, which are messy and contain errors and mistakes. -- The purpose of the record is to pave the way for ndk development in the early stage.
Put Android. SerialPort. sample_preferences in lib and shared_prefs in data/Android. SerialPort. Sample to save the serial port and baud rate parameters,The Android system stores the value of the preference element in the sharedpreference file. The file storage path is located in
In the data/[packgename]/shared_prefs/file in the ddms view, the naming convention is packagename_preferencse.xml.
Therefore, Android. SerialPort. sample_preferences.xml contains the device and baud rate set in the serialportpreferences class, which is a shared file.
Public class application extends Android. App. Application {
Public serialportfinder mserialportfinder = new serialportfinder ();
Private SerialPort mserialport = NULL;
Public SerialPort getserialport () throws securityexception, ioexception, invalidparameterexception {
If (mserialport = NULL ){
/* Read serial port parameters */
/* Check parameters */
If (path. Length () = 0) | (baudrate =-1 )){
Throw new invalidparameterexception ();
}
/* Open the serial port */
Mserialport = new SerialPort (new file (PATH), baudrate );
}
Return mserialport;
}
Public void closeserialport (){
If (mserialport! = NULL ){
Mserialport. Close ();
Mserialport = NULL;
}
}
1. Jump to serialportpreferences; --> Create "setup"
Obtain the Custom Data class of the application. The class member mserialportfinder is constructed (I .e. initialized) in the application)
Addpreferencesfromresource (R. xml. serial_port_preferences); sets the interface layout and configures device and baudrates.
Listpreference class !!!!!! Save
Android. SerialPort. sample_preferences in lib and shared_prefs of data/Android. SerialPort. Sample
Obtain all the serial devices and paths under the device. The serialportfinder class !!!!
Serialportfinder class !!!! ------------------------ As follows
Itdriv = getdrivers (). iterator (); iterator adds an iterator to getdrivers to traverse all drivers.
In getdrivers, proc/tty/drivers traverses the console device
Get the name and path of the driver:
1. Traverse itdriv = getdrivers (). iterator (); that is, getdrivers
Every time you traverse the file, add it to the device mdrivers. Add (New Driver (W [0], W [1]); Driver Class
Public String [] getalldevices () {vector <string> devices = new vector <string> (); // parse each dristmterator <driver> itdriv; try {// franklog. D (TAG, "getalldevices1:"); itdriv = getdrivers (). iterator (); // first traverse and register all the conforming terminal devices while (itdriv. hasnext () {log. D (TAG, "getalldevices2:"); driver = itdriv. next (); iterator <File> itdev = driver. getdevices (). iterator (); // compare whether the path prefix in/Dev is the same
Then go to/dev to search for the device names with the same prefix:
For example, if/dev/tty is traversed above, dev/tty0, 1, 2, 3, and so on will be searched here.
In this way, you can map the initialization to/dev/ttygs0 DEV/s3c2410_serial0/1/2/3 and so on.
While(Itdev. hasnext ()){
Log.D(Tag, "Getalldevices3 :");
String device = itdev. Next (). getname ();
String value = string.Format("% S (% s)", device, driver. getname ());
Devices. Add (value );
}
}
}Catch(Ioexception e ){
E. printstacktrace ();
}
ReturnDevices. toarray (NewString [devices. Size ()]);
}
PublicString [] getalldevicespath (){
Vector <string> devices =NewVector <string> ();
// Parse each driver
Iterator <driver> itdriv;
Try{
// Frank
Log.D(Tag, "Getalldevicespath1 :");
Itdriv = getdrivers (). iterator ();While(Itdriv. hasnext ()){
Log.D(Tag, "Getalldevicespath1 :");
Driver driver = itdriv. Next ();
Iterator <File> itdev = driver. getdevices (). iterator ();
While(Itdev. hasnext ()){
Log.D(Tag, "Getalldevicespath1 :");
String device = itdev. Next (). getabsolutepath ();
Devices. Add (device );
}
}
}Catch(Ioexception e ){
E. printstacktrace ();
}
ReturnDevices. toarray (NewString [devices. Size ()]);
}
}
Output
D/SerialPort (577): getalldevices1:
D/SerialPort (577): Found new driver1:/dev/ttygs
D/SerialPort (577): Found new driver3: 0
D/SerialPort (577): Found new driver2: 251
D/SerialPort (577): Found new driver0: g_serial
D/SerialPort (577): Found new driver1:/dev/ttyusb
D/SerialPort (577): Found new driver3: 0-253
D/SerialPort (577): Found new driver2: 188
D/SerialPort (577): Found new driver0: usbserial
D/SerialPort (577): Found new driver1:/dev/s3c2410_serial
D/SerialPort (577): Found new driver3: 64-67
D/SerialPort (577): Found new driver2: 204
D/SerialPort (577): Found new driver0: ttysac
D/SerialPort (577): getalldevices2:
D/SerialPort (577): Found new deviceww:/dev/ttygs0
D/SerialPort (577): getalldevices3:
D/SerialPort (577): getalldevices2:
D/SerialPort (577): getalldevices2:
D/SerialPort (577): Found new deviceww:/dev/s3c2410_serial0
D/SerialPort (577): Found new deviceww:/dev/s3c2410_serial1
D/SerialPort (577): Found new deviceww:/dev/s3c2410_serial
D/SerialPort (577): Found new deviceww:/dev/s3c2410_seri_3
D/SerialPort (577): getalldevices3:
D/SerialPort (577): getalldevices3:
D/SerialPort (577): getalldevices3:
D/SerialPort (577): getalldevices3:
D/SerialPort (577): getalldevicespath1:
D/SerialPort (577): getalldevicespath1:
D/SerialPort (577): getalldevicespath1:
D/SerialPort (577): getalldevicespath1:
D/SerialPort (577): getalldevicespath1:
D/SerialPort (577): getalldevicespath1:
D/SerialPort (577): getalldevicespath1:
D/SerialPort (577): getalldevicespath1:
D/SerialPort (577): getalldevicespath1:
The first part of the "setup" initialization of the total device is basically here, traversing the interface, the baud rate is similar
Record here