Package com.mitac.api.libs
Class SerialManager
- java.lang.Object
-
- com.mitac.api.libs.ApiBindBase
-
- com.mitac.api.libs.SerialManager
-
public class SerialManager extends ApiBindBase
This class provides serial port related APIs.Sample Code:
package com.mitac.api; import android.app.Activity; import android.os.Bundle; import com.mitac.api.libs.SerialManager; import com.mitac.api.libs.ServiceStatusCallback; public class MainActivity extends Activity { private SerialManager mSerialManager; private boolean mStopReading = false;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // when creating the instance the service is auto binding // register a callback to listen service status mSerialManager = new SerialManager(getApplicationContext(), new ServiceStatusCallback() {@Override
public void ready() { startTest(); } }); } private void startTest() { if (mSerialManager != null) { String[] ports = mSerialManager.getSerialPorts(); if (ports != null && ports.length > 1) { // Skip the first invalid port number String port = ports[1]; if (mSerialManager.openSerialPort(port, 115200)) { Log.d(TAG, "open serial port successfully"); readSerialPortTest(); writeSerialPortTest(); } else { Log.d(TAG, "fail to open serial port"); } } } } private void startNonBlockTest() { if (mSerialManager != null) { String[] ports = mSerialManager.getSerialPorts(); if (ports != null) { String port = ports[0]; if (mSerialManager.openSerialPort(port, 115200, 8, "N", 1, false)) { Log.d(TAG, "open serial port successfully"); readSerialPortTest(); writeSerialPortTest(); } else { Log.d(TAG, "fail to open serial port"); } } } } private void readSerialPortTest() { final ByteBuffer inputByteBuffer = ByteBuffer.allocate(1024); inputByteBuffer.clear(); Log.d(TAG, "try to read data"); // the read process may cause blocking and ANR so recommend to run in working thread new Thread(new Runnable() {@Override
public void run() { int ret = 0; while ( ret >= 0 ) { ret = mSerialManager.read(inputByteBuffer); Log.d(TAG, "ret = " + ret); if ( ret > 0 ) { Log.d(TAG, "read data:" + new String(inputByteBuffer.array(), 0, ret)); if (mStopReading) break; } } } }).start(); } private void writeSerialPortTest() { String text = "Test1234567890"; byte[] bytes = text.getBytes(); ByteBuffer outputByteBuffer = ByteBuffer.allocate(1024); outputByteBuffer.clear(); outputByteBuffer.put(bytes); if (mSerialManager.write(outputByteBuffer, bytes.length)) { Log.d(TAG, "write data successfully"); } else { Log.e(TAG, "fail to write data"); } }@Override
protected void onStop() { super.onStop(); mStopReading = true; if (mSerialManager != null) { mSerialManager.close(); mSerialManager.unbindService(); } } }
-
-
Constructor Summary
Constructors Constructor Description SerialManager(android.content.Context context)
This constructor will not bind the service automatically.SerialManager(android.content.Context context, ServiceStatusCallback callback)
This constructor will bind the service automatically.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
bindService()
bind serviceboolean
close()
Close opened serial port.java.lang.String[]
getSerialPorts()
boolean
isServiceReady()
Check if service is ready.boolean
openSerialPort(java.lang.String name, int speed)
Opens the serial port with the given name.boolean
openSerialPort(java.lang.String name, int speed, int nbits, java.lang.String parity, int stopbit, boolean blocked)
Opens the serial port with the given name.int
read(java.nio.ByteBuffer buffer)
Reads data into the provided buffer.boolean
sendBreak()
Sends a stream of zero valued bits for 0.25 to 0.5 secondsvoid
unbindService()
unbind serviceboolean
write(java.nio.ByteBuffer buffer, int length)
Writes data from provided buffer.
-
-
-
Constructor Detail
-
SerialManager
public SerialManager(android.content.Context context)
This constructor will not bind the service automatically. Please callbindService()
at least once before using APIs.- Parameters:
context
- better to useContext.getApplicationContext()
to prevent memory leaks- Throws:
java.lang.IllegalArgumentException
- if context is null.
-
SerialManager
public SerialManager(android.content.Context context, ServiceStatusCallback callback)
This constructor will bind the service automatically.- Parameters:
context
- better to useContext.getApplicationContext()
to prevent memory leakscallback
- used to get the notification when service is ready.- Throws:
java.lang.IllegalArgumentException
- if context or callback is null.- See Also:
ServiceStatusCallback
-
-
Method Detail
-
bindService
public void bindService()
Description copied from class:ApiBindBase
bind service- Specified by:
bindService
in classApiBindBase
-
unbindService
public void unbindService()
Description copied from class:ApiBindBase
unbind service- Specified by:
unbindService
in classApiBindBase
-
openSerialPort
public boolean openSerialPort(java.lang.String name, int speed)
Opens the serial port with the given name. The speed of the serial port must be one of: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000 or 4000000- Parameters:
name
- of the serial port, RS232 port should always be /dev/ttyHH1speed
- at which to open the serial port- Returns:
false
if remote fail to open a port;true
otherwise.
-
openSerialPort
public boolean openSerialPort(java.lang.String name, int speed, int nbits, java.lang.String parity, int stopbit, boolean blocked)
Opens the serial port with the given name. The speed of the serial port must be one of: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000 or 4000000- Parameters:
name
- of the serial port, RS232 port should always be /dev/ttyHH1speed
- at which to open the serial portnbits
- number of bits to use, must be one of 5bit, 6bit, 7bit and 8bitparity
- parity mode to use, can be "O"(Odd), "E"(Even), "N"(No parity), default is "N"stopbit
- stop bits to use, can be 1 and 2, default is 1blocked
- whether the reading/writing of the serial port shall be blocked.- Returns:
false
if remote fail to open a port;true
otherwise.
-
getSerialPorts
public java.lang.String[] getSerialPorts()
-
write
public boolean write(java.nio.ByteBuffer buffer, int length)
Writes data from provided buffer.- Parameters:
buffer
- to writelength
- number of bytes to write- Returns:
- true means success, false means failed.
- Throws:
java.lang.IllegalArgumentException
- if buffer is null.
-
read
public int read(java.nio.ByteBuffer buffer)
Reads data into the provided buffer. Note that the value returned byBuffer.position()
on this buffer is unchanged after a call to this method.Note: it's highly recommended that this method is called from worker thread.
- Parameters:
buffer
- to read into- Returns:
- number of bytes read; -1 if fail.
- Throws:
java.lang.IllegalArgumentException
- if buffer is null.
-
close
public boolean close()
Close opened serial port.- Returns:
false
if remote fail to close the port.
-
sendBreak
public boolean sendBreak()
Sends a stream of zero valued bits for 0.25 to 0.5 seconds- Returns:
false
if remote fail.
-
isServiceReady
public boolean isServiceReady()
Check if service is ready.- Returns:
true
if ready; false otherwise.
-
-