当前位置:编程学习 > wap >>

android 串口 通信

package org.winplus.serial;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;

import org.winplus.serial.utils.SerialPort;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInte易做图ce;
import android.content.DialogInte易做图ce.OnClickListener;
import android.os.Bundle;

public abstract class SerialPortActivity extends Activity {
protected Application mApplication;
protected SerialPort mSerialPort;
protected OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;

private class ReadThread extends Thread {

@Override
public void run() {
super.run();
while (!isInterrupted()) {
int size;
try {
byte[] buffer = new byte[64];
if (mInputStream == null)
return;
size = mInputStream.read(buffer);
if (size > 0) {
onDataReceived(buffer, size);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}

private void DisplayError(int resourceId) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Error");
b.setMessage(resourceId);
b.setPositiveButton("OK", new OnClickListener() {
public void onClick(DialogInte易做图ce dialog, int which) {
SerialPortActivity.this.finish();
}
});
b.show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApplication = (Application) getApplication();//这是被指出来的65行的问题,可是看不出来任何问题啊
try {
mSerialPort = mApplication.getSerialPort();
mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream();

/* Create a receiving thread */
mReadThread = new ReadThread();
mReadThread.start();
} catch (SecurityException e) {
DisplayError(R.string.error_security);
} catch (IOException e) {
DisplayError(R.string.error_unknown);
} catch (InvalidParameterException e) {
DisplayError(R.string.error_configuration);
}
}

protected abstract void onDataReceived(final byte[] buffer, final int size);

@Override
protected void onDestroy() {
if (mReadThread != null)
mReadThread.interrupt();
mApplication.closeSerialPort();
mSerialPort = null;
super.onDestroy();
}

}
//以上是SerialPortActivity.java的代码
//以下是SerialDemoActivity.java的代码
package org.winplus.serial;

import java.io.IOException;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class SerialDemoActivity extends SerialPortActivity{
EditText mReception;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);//这是16行  被提示有问题的代码。。。也实在看不出来
            setContentView(R.layout.main);

//          setTitle("Loopback test");
            mReception = (EditText) findViewById(R.id.EditTextReception);

            EditText Emission = (EditText) findViewById(R.id.EditTextEmission);
            Emission.setOnEditorActionListener(new OnEditorActionListener() {
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            int i;
                            CharSequence t = v.getText();
                            char[] text = new char[t.length()];
                            for (i=0; i<t.length(); i++) {
                                    text[i] = t.charAt(i);
                            }
                            try {
                                    mOutputStream.write(new String(text).getBytes());
                                    mOutputStream.write('\n');
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
                            return false;
                    }
            });
    }

    @Override
    protected void onDataReceived(final byte[] buffer, final int size) {
            runOnUiThread(new Runnable() {
                    public void run() {
                            if (mReception != null) {
                                    mReception.append(new String(buffer, 0, size));
                            }
                    }
            });
    }
}
//这是整个项目链接地址
http://download-vdisk.sina.com.cn/96530318/0c97d521af831584fb1fcbb5c9df366310344083?ssig=t3PynRl6j1&Expires=1386412231&KID=sae,l30zoo1wmz&fn=SerialDemo.rar --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 手机好像没有串口 --------------------编程问答-------------------- http://www.kuaipan.cn/file/id_81686790456515535.htm?source=1
这是新的下载地址,以前那个不能用  麻烦 大家帮忙解决一下啊
补充:移动开发 ,  Android
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,