Android 怎样接收十六进制数据?急!!!

wwj19918031 wwj19918031 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
制作了一个代码,已经能够从串口通信接收二进制数据进行处理,现在由于串口另一端的数据格式有变,需要将其改成接收十六进制数据,然后变成相应的字符串。请各位大神帮忙改改啊,自己折腾好几天了都弄不出来~~~原来读二进制的程序如下:



package com.android.book;

import com.android.book.DBAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Nearby extends SerialPortActivity {
TextView tv=null;
String bookname;
String str="";
String check, check2;
int find=0;
int i=1;
DBAdapter db = new DBAdapter(this);
@Override
protected void onDataReceived(final byte[] buffer, final int size) {

runOnUiThread(new Runnable() {
public void run() {
if ( tv!= null && size >0) {
str=str+new String(buffer,0,size);
int start=str.indexOf(8');
int end=str.indexOf('9');
while(end<start){
str=str.substring(start);
start=str.indexOf('8');
end=str.indexOf('9');
if(start<end-6){
check=str.substring(start+1,end);
if(find==0){
tv.append(check);
find=1;
find(check);
}
return;
}
if(end==-1)
break;
}
if(start<end-6){
check=str.substring(start+1,end);
if(find==0){
tv.append(check);
find=1;
find(check);
}
}

}
}
});

tv.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int i = 4;
Intent intent = new Intent();
intent.setClass(Nearby.this,user9.class);
Bundle bundle = new Bundle();
bundle.putString("name",check);
bundle.putInt("i", i);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
private void find(String book)
{
int c=0;
db.open();
Cursor c1 = db.getISBN(book);
if (c1.moveToFirst())
{
c++;
//db.updateTitle(c1.getString(0), c1.getString(1), c1.getString(2), c1.getString(3), c1.getString(4), "yes");
DisplayTitle(c1,tv);
} while (c1.moveToNext());

db.close();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearby);
Intent intent =this.getIntent();

tv=(TextView)findViewById(R.id.TextView_content);

/*button_return to user1*/
Button b1 = (Button) findViewById(R.id.Button_return);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(Nearby.this,enter.class);
startActivity(intent);
Nearby.this.finish();
}
});



}

public void DisplayTitle (Cursor c,TextView tv)
{
tv.setText( c.getString(2) + "\n");
}

}





package com.android.book;

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

import com.android.book.Application;
import com.android.book.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.serialport.SerialPort;

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(DialogInterface dialog, int which) {
SerialPortActivity.this.finish();
}
});
b.show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApplication = (Application) getApplication();
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();
}
}

...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
wwj19918031
Android 80,489 成员 91,375 内容