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

wwj19918031 2014-06-05 05:24:06
制作了一个代码,已经能够从串口通信接收二进制数据进行处理,现在由于串口另一端的数据格式有变,需要将其改成接收十六进制数据,然后变成相应的字符串。请各位大神帮忙改改啊,自己折腾好几天了都弄不出来~~~原来读二进制的程序如下:



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();
}
}

...全文
545 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_22721443 2014-10-30
  • 打赏
  • 举报
回复
这个方案比好
吾是锋子 2014-10-30
  • 打赏
  • 举报
回复
3楼的方法可以用,非常感谢
kp_liu 2014-08-15
  • 打赏
  • 举报
回复
楼主有源码吗,求发下。最近也正在愁这方面
映映 2014-06-06
  • 打赏
  • 举报
回复
byte[]转化为十六进制
wwj19918031 2014-06-05
  • 打赏
  • 举报
回复
已改成功~~主要是读byte[]的时候要合理将十六进制转换为字符串。 附代码如下:

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 {
	private static final int Oxff = 0;
	TextView tv=null;
	String bookname;
	String str="";
	String str1="";
	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) {
				
					
					
		/****************************************************************************/
					str1 = bytesToHexString(buffer);
		int start = str1.indexOf('8');
			int end = str1.indexOf('9');
			while(end<start){
				str1=str1.substring(start);
				start=str1.indexOf('8');
				end=str1.indexOf('9');
				if(start<end-6){
					check=str1.substring(start+1,end);
					
		/*****************************************************************************/
						if(find==0){
								tv.append(check);
								find=1;
								find(check);
							}
							return;
						}
						if(end==-1)
							break;
					}
					if(start<end-6){
						check=str1.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); 
				}
			});
		 		 
		}

/************************** string to 16 *******************************************/
	
	public static String bytesToHexString(byte[] src)
	{
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length<= 0){
			return null;
		}
		for(int i = 0;i < src.length;i++){
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if(hv.length() < 2){
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
		
	}
	
	/*******************************************************************/
	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");
	}
	
}
	


wwj19918031 2014-06-05
  • 打赏
  • 举报
回复
您能具体给我讲下或者举个例子告诉我怎么转么?刚开始自学的android,手上第一个尝试的东西就是这个,真的是不会啊......
网络咖啡 2014-06-05
  • 打赏
  • 举报
回复
你接收到的数据是byte数组,自己按照协议转换一下不就可以了吗?

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧