AccessoryChatpublic class AccessoryChat extends android.app.Activity implements Runnable, TextView.OnEditorActionListener
Fields Summary |
---|
private static final String | TAG | private static final String | ACTION_USB_PERMISSION | private android.widget.TextView | mLog | private android.widget.EditText | mEditText | private android.os.ParcelFileDescriptor | mFileDescriptor | private FileInputStream | mInputStream | private FileOutputStream | mOutputStream | private android.hardware.usb.UsbManager | mUsbManager | private android.app.PendingIntent | mPermissionIntent | private boolean | mPermissionRequestPending | private static final int | MESSAGE_LOG | private final android.content.BroadcastReceiver | mUsbReceiver | android.os.Handler | mHandler |
Methods Summary |
---|
public void | onCreate(android.os.Bundle savedInstanceState)
super.onCreate(savedInstanceState);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
setContentView(R.layout.accessory_chat);
mLog = (TextView)findViewById(R.id.log);
mEditText = (EditText)findViewById(R.id.message);
mEditText.setOnEditorActionListener(this);
| public void | onDestroy()
unregisterReceiver(mUsbReceiver);
super.onDestroy();
| public boolean | onEditorAction(android.widget.TextView v, int actionId, android.view.KeyEvent event)
if (actionId == EditorInfo.IME_ACTION_DONE && mOutputStream != null) {
try {
mOutputStream.write(v.getText().toString().getBytes());
} catch (IOException e) {
Log.e(TAG, "write failed", e);
}
v.setText("");
return true;
}
Log.d(TAG, "onEditorAction " + actionId + " event: " + event);
return false;
| public void | onPause()
super.onPause();
if (mFileDescriptor != null) {
try {
mFileDescriptor.close();
} catch (IOException e) {
} finally {
mFileDescriptor = null;
}
}
| public void | onResume()
super.onResume();
Intent intent = getIntent();
Log.d(TAG, "intent: " + intent);
UsbAccessory[] accessories = mUsbManager.getAccessoryList();
UsbAccessory accessory = (accessories == null ? null : accessories[0]);
if (accessory != null) {
if (mUsbManager.hasPermission(accessory)) {
openAccessory(accessory);
} else {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
mUsbManager.requestPermission(accessory, mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
} else {
Log.d(TAG, "mAccessory is null");
}
| private void | openAccessory(android.hardware.usb.UsbAccessory accessory)
Log.d(TAG, "openAccessory: " + accessory);
mFileDescriptor = mUsbManager.openAccessory(accessory);
if (mFileDescriptor != null) {
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
mInputStream = new FileInputStream(fd);
mOutputStream = new FileOutputStream(fd);
Thread thread = new Thread(null, this, "AccessoryChat");
thread.start();
Log.d(TAG, "openAccessory succeeded");
} else {
Log.d(TAG, "openAccessory fail");
}
| public void | run()
int ret = 0;
byte[] buffer = new byte[16384];
while (ret >= 0) {
try {
ret = mInputStream.read(buffer);
} catch (IOException e) {
break;
}
if (ret > 0) {
Message m = Message.obtain(mHandler, MESSAGE_LOG);
String text = new String(buffer, 0, ret);
Log.d(TAG, "chat: " + text);
m.obj = text;
mHandler.sendMessage(m);
}
}
Log.d(TAG, "thread out");
|
|