package com.unity.sp;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android_serialport_api.ComPort;
//import android.serialport.sample.R;
public class MyserialActivity extends UnityPlayerActivity {
public static ComPort mComPort;
public static ComPort mComPort3;
private static final int Port = 5281;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setAction("android.intent.action.HIDE_NAVIGATION_BAR");
MyserialActivity.this.sendBroadcast(intent);
}
public void OpenttyS1()
{
if(mComPort == null)
{
mComPort = new ComPort();
}
mComPort.Start("ttyS1");
}
public void OpenttyS3()
{
if(mComPort3 == null)
{
mComPort3 = new ComPort();
}
mComPort3.Start("ttyS3");
}
public static String bytes2HexString(byte[] b, int len) {
String ret = "";
for (int i = 0; i < len; i++) {
String hex = Integer.toHexString(b[ i ] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (byte)(_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte)(_b0 ^ _b1);
return ret;
}
public void ttyS1Send(byte[] data,int length)
{
mComPort.SendData(data, length);
}
public void ttyS3Send(byte[] data,int length)
{
mComPort3.SendData(data, length);
}
public void RestartSystem()
{
new ServerThread().start();
}
class ServerThread extends Thread {
ServerSocket serversocket = null;
@Override
public void run()
{
super.run();
try {
serversocket = new ServerSocket(Port);
Socket socket = serversocket.accept();
while (true) {
// OutputStream ou = socket.getOutputStream();
DataOutputStream wirter = new DataOutputStream(socket.getOutputStream());
BufferedReader buffer = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
wirter.writeBytes("reboot\n");
unity Android 串口通讯完整项目实例
最新推荐文章于 2025-06-29 11:44:35 发布