0

我正在尝试使用 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();
    }
}
}
4

1 回答 1

1

终端仿真器需要几秒钟来初始化连接并启动。在此之前,写入模拟器的字符串会被静默删除。

这样你就可以:

  • 如果不需要立即写入模拟器,请忽略此
  • 等待一段固定的时间
  • 检查终端模拟器api,看看是否有api来询问连接状态
于 2013-01-04T13:37:09.910 回答