| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware; |
| 18 |
|
| 19 |
import android.annotation.SystemService; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.os.Build; |
| 23 |
import android.os.ParcelFileDescriptor; |
| 24 |
import android.os.RemoteException; |
| 25 |
|
| 26 |
import java.io.IOException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
@SystemService(Context.SERIAL_SERVICE) |
| 32 |
public class SerialManager { |
| 33 |
private static final String TAG = "SerialManager"; |
| 34 |
|
| 35 |
private final Context mContext; |
| 36 |
private final ISerialManager mService; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public SerialManager(Context context, ISerialManager service) { |
| 42 |
mContext = context; |
| 43 |
mService = service; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 52 |
public String[] getSerialPorts() { |
| 53 |
try { |
| 54 |
return mService.getSerialPorts(); |
| 55 |
} catch (RemoteException e) { |
| 56 |
throw e.rethrowFromSystemServer(); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 72 |
public SerialPort openSerialPort(String name, int speed) throws IOException { |
| 73 |
try { |
| 74 |
ParcelFileDescriptor pfd = mService.openSerialPort(name); |
| 75 |
if (pfd != null) { |
| 76 |
SerialPort port = new SerialPort(name); |
| 77 |
port.open(pfd, speed); |
| 78 |
return port; |
| 79 |
} else { |
| 80 |
throw new IOException("Could not open serial port " + name); |
| 81 |
} |
| 82 |
} catch (RemoteException e) { |
| 83 |
throw e.rethrowFromSystemServer(); |
| 84 |
} |
| 85 |
} |
| 86 |
} |