Android TCP/IP 傳送接收16進位制資料
幫朋友推薦,贏眾投理財,CEO是我朋友,全是真實可靠的農業專案,投資收益可達年化9.8%,且有多重安全保障!
// 設定伺服器IP和埠
private static final String SERVERIP_2 ="192.168.5.178";
private static final int SERVERPORT_2 = 4002;
private Socket _socket_2 = null;
//private BufferedReader _bufferedReader = null;
private static PrintWriter _printWriter_2 = null;
//定義下發資料的陣列
byte[] buf = {(byte)0x88,(byte)0x18,(byte)0xff,(byte)0xf3,(byte)0x02,(byte)0x04,(byte)0x00,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff}; //傳送的資料要用BYTE的陣列下發
Button b_chazhao;//
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b_chazhao = (Button) findViewById(R.id.chazhao);
b_chazhao.setOnClickListener(b);//按鈕的按下事件
b_chazhao.setOnTouchListener(b);//按鈕的擡起事件
load();
}
//初始化時建立連線
public void load(){
try {
// 連線伺服器
_socket_2 = new Socket(SERVERIP, SERVERPORT);
if(_socket_2.isConnected()){//socket連線上
// 取得輸入/輸出流
//_bufferedReader = new BufferedReader(new InputStreamReader(_socket.getInputStream(), "GB2312"));
_printWriter_2 = new PrintWriter(_socket_2.getOutputStream(), true);
// 開啟監聽執行緒
_thread.start();
}else{//socket沒有連線上
//Toast.makeText(getApplicationContext(), "與伺服器連線失敗,請等待...",Toast.LENGTH_SHORT).show();
int isconnect=1;
while(isconnect==1){
_socket _2= new Socket(SERVERIP_2, SERVERPORT_2);
Thread.sleep(100);//停100毫秒
if(_socket_2.isConnected()){
isconnect=0;
}
}
}
}
catch (Exception e) {
System.out.println("Connect is failed!");
}
}
//定義下發資料的方法
public void sendMessage_btye(byte[] buf){//寫個下發資料的方法
try {
DataInputStream is=new DataInputStream(new ByteArrayInputStream(buf));
OutputStream os = _socket_2.getOutputStream();
os.write(buf);
os.flush();
//os.close();
//is.close();
_socket_2.close();
} catch (Exception e) {
System.out.println("send:"+e.getMessage());
}
}
//接受資料的執行緒
private Thread _thread = new Thread(){
public void run(){
while(true){
try{
Thread.sleep(200);
isCome=1;
InputStream in = _socket.getInputStream();
byte[] buffer = new byte[in.available()];
while(in.read(buffer)>0){
for (int i = 0; i < buffer.length; i++) {
String hex = Integer.toHexString(buffer[i] & 0xFF); //這個可以把16進位制接收上來的資料,以16進位制形式在String中表現
if (hex.length() == 1) {
hex='0' + hex;
}else{
}
Receive_date[i]=hex;
}
i_hex=Integer.valueOf(Receive_date[4],16);
switch(i_hex){
case 20:
Receive_kty=Receive_date;
is_sb=20;break;//空調-1
case 21:
Receive_ktd=Receive_date;
is_sb=21;break;//空調-2
case 40:
Receive_dy=Receive_date;
is_sb=40;
break;//電壓表
}
Handle_date(is_sb);//處理接收到的裝置
}
}
catch (Exception e){
}
}
}
};
// 按鈕按下和擡起事件
class ButtonListener implements OnClickListener, OnTouchListener {
public void onClick(View v) {
if (v.getId() == R.id.chazhao) {
// Log.d("test", "cansal button ---> click");
}
}
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.chazhao) {
if (event.getAction() == MotionEvent.ACTION_UP) { // 擡起事件
Handle_data(0);//
connnnn();
sendMessage(buf);
}
if (event.getAction() == MotionEvent.ACTION_DOWN) { // 按下事件
if (isFirst == 0) {
try {
_socket.close();
_thread.stop();
} catch (Exception ex) {
System.out.println("anxia:" + ex.getMessage());
}
}
isFirst = 1;
Handle_data(1);// 處理下發的資料
connnnn();
sendMessage(buf);
}
}
return false;
}
}
/ /處理資料
public void Handle_data(int isOpen) {
try{
wz_10 = Integer.parseInt("10");
wz_16 = Integer.toHexString(wz_10);// 把10進位制轉成16進位制
if (wz_16.length() == 1) {
wz_16 = "0" + wz_16;
wz_16 = wz_16.toUpperCase();
int length = wz_16.length() / 2;
char[] hexchars = wz_16.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexchars[pos]) << 4 | charToByte(hexchars[pos + 1]));
buf[1] = d[i];
}
jy = Long.parseLong(wz_16, 16);
System.out.println("1");
} else if (wz_16.length() == 2) {
charArray[1] = (char) Long.parseLong(wz_16, 16);
charArray[2] = 0x00;
jy = Long.parseLong(wz_16, 16);
wz_16 = wz_16.toUpperCase();
int length = wz_16.length() / 2;
char[] hexchars = wz_16.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexchars[pos]) << 4 | charToByte(hexchars[pos + 1]));
buf[1] = d[i];
}
} else if (wz_16.length() == 3) {
wz_16 = "0" + wz_16;
st_low = wz_16.substring(2, 4);
st_low = st_low.toUpperCase();
int length = st_low.length() / 2;
char[] hexchars = st_low.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexchars[pos]) << 4 | charToByte(hexchars[pos + 1]));
buf[1] = d[i];
}
st_hight = wz_16.substring(0, 2);
length = st_hight.length() / 2;
hexchars = st_hight.toCharArray();
d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexchars[pos]) << 4 | charToByte(hexchars[pos + 1]));
buf[2] = d[i];
}
jy = Long.parseLong(st_low, 16) + Long.parseLong(st_hight, 16);
System.out.println("leng_3");
} else if (wz_16.length() == 4) {
st_low = wz_16.substring(2, 4);
st_low = st_low.toUpperCase();
int length = st_low.length() / 2;
char[] hexchars = st_low.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexchars[pos]) << 4 | charToByte(hexchars[pos + 1]));
buf[1] = d[i];
}
st_hight = wz_16.substring(0, 2);
length = st_hight.length() / 2;
hexchars = st_hight.toCharArray();
d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexchars[pos]) << 4 | charToByte(hexchars[pos + 1]));
buf[2] = d[i];
}
/*
* charArray[1] = (char) Long.parseLong(st_low,16); st_hight =
* wz_16.substring(0, 2); charArray[2] = (char)
* Long.parseLong(st_hight,16);
*/
jy = Long.parseLong(st_low, 16) + Long.parseLong(st_hight, 16);
// jiaoyan = Integer.parseInt(st_low) + Integer.parseInt(st_hight);
System.out.println("leng_4");
}
if (isOpen >= 1) {
buf[3] = 0x01;
jy = jy + 1;
System.out.println("jy:" + jy);
} else {
buf[3] = 0x00;
}
buf[4] = 0x00;
// charArray[4] = 0x00;//
buf[5] = (byte) jy;
}catch(Exception e){
}
}
private byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}