Android のライブラリを使用して端末エミュレータに接続しようとしています。これはシリアル デバイスに接続され、送受信されたデータが表示されます。どちらの場合も、端末の下のテキスト ボックスを介して、または端末自体に入力してキーボードの Enter キーを押すことで、接続を介してデータを送信できるはずです。エミュレータの画面に書き込むには、ライブラリに write という関数があります。ただし、これは機能する場合と機能しない場合があります。
[1]、[2]、および [3] とマークされたコードの行では正常に動作しますが、[4] と [5] では動作しません。誰でも理由がわかりますか?4 と 5 の前に端末セッションを作成したので、うまくいくはずですが、そうではありません。しかし、1,2,3 に対して write を呼び出し始めると、正常に動作しますか?!
public class TermActivity extends Activity
{
private EditText mEntry;
private EmulatorView mEmulatorView;
private TermSession mSession;
private InputStream bis;
private OutputStream bos;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.term_activity);
/* Text entry box at the bottom of the activity. Note that you can
also send input (whether from a hardware device or soft keyboard)
directly to the EmulatorView. */
mEntry = (EditText) findViewById(R.id.term_entry);
mEntry.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int action, KeyEvent ev) {
// Ignore enter-key-up events
if (ev != null && ev.getAction() == KeyEvent.ACTION_UP) {
return false;
}
// Don't try to send something if we're not connected yet
TermSession session = mSession;
if (mSession == null) {
return true;
}
Editable e = (Editable) v.getText();
// Write to the terminal session
//for when i press enter on keyboard.
[1] session.write(e.toString());
[2] session.write("test");
[3] session.write('\r');
TextKeyListener.clear(e);
return true;
}
});
/**
* EmulatorView setup.
*/
EmulatorView view = (EmulatorView) findViewById(R.id.emulatorView);
mEmulatorView = view;
/* Let the EmulatorView know the screen's density. */
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
view.setDensity(metrics);
/* Create a TermSession. */
Intent myIntent = getIntent();
String sessionType = myIntent.getStringExtra("type");
TermSession session;
if (sessionType != null && sessionType.equals("telnet")) {
/* Telnet connection: we need to do the network connect on a
separate thread, so kick that off and wait for it to finish. */
// connectToTelnet(myIntent.getStringExtra("host"));
byte[] a = new byte[]{'y','y', 'y', 'y', 'y'};
byte[] b = new byte[]{'a','a', 'l', 'l', 'o'};
bis = new ByteArrayInputStream(b);
bos = new ByteArrayOutputStream();
session = new TelnetSession(bis, bos);
mEmulatorView.attachSession(session);
[4]session.write("test");
mSession = session;
[5]session.write("test");
return;
} else {
// Create a local shell session.
session = createLocalTermSession();
mSession = session;
}
/* Attach the TermSession to the EmulatorView. */
view.attachSession(session);
/* That's all you have to do! The EmulatorView will call the attached
TermSession's initializeEmulator() automatically, once it can
calculate the appropriate screen size for the terminal emulator. */
}
Socket mSocket;
private static final int MSG_CONNECTED = 1;
/* Create the TermSession which will handle the Telnet protocol and
terminal emulation. */
private void createTelnetSession() {
Socket socket = mSocket;
// Get the socket's input and output streams
InputStream termIn;
OutputStream termOut;
try {
termIn = socket.getInputStream();
termOut = socket.getOutputStream();
} catch (IOException e) {
//Handle exception here
return;
}
/* Create the TermSession and attach it to the view. See the
TelnetSession class for details. */
byte[] a = new byte[]{'y','y', 'y', 'y', 'y'};
byte[] b = new byte[]{'a','a', 'l', 'l', 'o'};
bis = new ByteArrayInputStream(b);
bos = new ByteArrayOutputStream();
TermSession session = new TelnetSession(bis, bos);
mEmulatorView.attachSession(session);
mSession = session;
session.write("test");
try {
bos.write(a);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}