Reputation: 1194
I'm new at Android and I'm trying to use Android USB to send some data. I tried to use this example code
// Get UsbManager from Android.
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
// Find the first available driver.
UsbSerialDriver driver = UsbSerialProber.acquire(manager);
if (driver != null) {
driver.open();
try {
driver.setBaudRate(115200);
byte buffer[] = new byte[16];
int numBytesRead = driver.read(buffer, 1000);
Log.d(TAG, "Read " + numBytesRead + " bytes.");
} catch (IOException e) {
// Deal with error.
} finally {
driver.close();
}
}
and I'm getting this error
The method getSystemService(String) is undefined for the type TCPClient
I'm not sure if I'm missing some points. Any help would be great. I'm using Eclipse Juno and JDK 1.7.
Upvotes: 0
Views: 5783
Reputation: 51581
getSystemService(String)
is a method defined for Context
class: Context#getSystemService(String)
. Since Activity is a subclass of Context, using this.getSystemService(String)
or simply getSystemService(String)
works fine inside an Activity.
But, it seems like you are trying to use this method inside a class named TCPClient
. And the error is pointing out that TCPClient#getSystemService(String)
is not defined: correct.
You should either pass the Activity's Context to TCPClient
and use it as:
UsbManager manager = (UsbManager)
passedContext.getSystemService(Context.USB_SERVICE);
Or if TCPClient is an inner class to your Activity, the following should work too:
UsbManager manager = (UsbManager)
YourActivityName.this.getSystemService(Context.USB_SERVICE);
Upvotes: 5