Correct using bytearray.

This commit is contained in:
SeungheeOh 2019-09-15 09:21:17 +09:00
parent 27814e5a8e
commit 53a6431453
3 changed files with 95 additions and 42 deletions

View File

@ -26,7 +26,7 @@ public class MainActivity extends AppCompatActivity {
public static final String TAG="carnav"; public static final String TAG="carnav";
static final String ACTION_USB_PERMISSION = "com.flywithu.carnav.USB_PERMISSION"; static final String ACTION_USB_PERMISSION = "com.flywithu.carnav.USB_PERMISSION";
private BroadcastReceiver usbReceiver = null; private BroadcastReceiver usbReceiver = null;
private static UsbSerialPort sPort = null;
private void goService() private void goService()
{ {
@ -109,20 +109,21 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(getApplicationContext(), "Unknown error", Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), "Unknown error", Toast.LENGTH_LONG).show();
} else { } else {
UsbSerialPort sPort = null;
sPort = driver.getPorts().get(0); sPort = driver.getPorts().get(0);
sPort.open(connection); sPort.open(connection);
Toast.makeText(getApplicationContext(),"Serial Count:"+String.valueOf(driver.getPorts().size()),Toast.LENGTH_LONG).show();
sPort.close(); sPort.close();
sPort = null; sPort = null;
Toast.makeText(getApplicationContext(),"Serial Count:"+String.valueOf(driver.getPorts().size()),Toast.LENGTH_LONG).show();
goService(); goService();
} }
} }
} }
catch(Exception e) catch(Exception e)
{ {
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
} }

View File

@ -54,10 +54,10 @@ public class RealService extends Service {
} }
private synchronized void sendSerial(String str,Charset enc) { private synchronized void sendSerial(String str,Charset enc) {
byte t1[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x30, 0x33, 0x7C, 0x31, 0x2C, 0x31, 0x7C, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}; // byte t1[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x30, 0x33, 0x7C, 0x31, 0x2C, 0x31, 0x7C, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30};
byte t2[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x31, 0x33, 0x7C, 0x41, (byte) 0xec, (byte) 0xa7, (byte) 0x80, (byte) 0xec, (byte) 0x96, (byte) 0xb4, (byte) 0xeb, (byte) 0xb0, (byte) 0x98, 0x5a, 0x7c}; // byte t2[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x31, 0x33, 0x7C, 0x41, (byte) 0xec, (byte) 0xa7, (byte) 0x80, (byte) 0xec, (byte) 0x96, (byte) 0xb4, (byte) 0xeb, (byte) 0xb0, (byte) 0x98, 0x5a, 0x7c};
byte t3[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x32, 0x33, 0x7C, (byte) 0xfe, (byte) 0xff, 0x00, 0x41, (byte) 0xc9, (byte) 0xc0, (byte) 0xc5, (byte) 0xb4, (byte) 0xbc, 0x18, 0x00, 0x5a, 0x7c}; // byte t3[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x32, 0x33, 0x7C, (byte) 0xfe, (byte) 0xff, 0x00, 0x41, (byte) 0xc9, (byte) 0xc0, (byte) 0xc5, (byte) 0xb4, (byte) 0xbc, 0x18, 0x00, 0x5a, 0x7c};
byte t4[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x33, 0x33, 0x7C, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, (byte) 0xc9, (byte) 0xc0, 0x00, 0x00, (byte) 0xc5, (byte) 0xb4, 0x00, 0x00, (byte) 0xbc, 0x18, 0x00, 0x00, 0x00, 0x5a, 0x7c}; // byte t4[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x33, 0x33, 0x7C, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, (byte) 0xc9, (byte) 0xc0, 0x00, 0x00, (byte) 0xc5, (byte) 0xb4, 0x00, 0x00, (byte) 0xbc, 0x18, 0x00, 0x00, 0x00, 0x5a, 0x7c};
/* /*
41eca780ec96b4ebb0985a 41eca780ec96b4ebb0985a
feff0041c9c0c5b4bc18005a feff0041c9c0c5b4bc18005a
@ -67,8 +67,15 @@ feff0041c9c0c5b4bc18005a
try { try {
sPort.write(getStringToByte(str,enc), 3000); sPort.write(getStringToByte(str,enc), 3000);
sPort.write(new byte[]{0xd}, 3000); // Toast.makeText(getApplicationContext(),String.valueOf(result),Toast.LENGTH_SHORT).show();
sPort.write(new byte[]{0xd}, 3000);
// Toast.makeText(getApplicationContext(),String.valueOf(result2),Toast.LENGTH_SHORT).show();
Log.d("TAG",str);
// showToast(getApplication(), "SEND:"+str);
// sPort.write(t1, 3000); // sPort.write(t1, 3000);
// sPort.write(new byte[]{0xd}, 3000); // sPort.write(new byte[]{0xd}, 3000);
// //
@ -81,17 +88,22 @@ feff0041c9c0c5b4bc18005a
// sPort.write(t4, 3000); // sPort.write(t4, 3000);
// sPort.write(new byte[]{0xd}, 3000); // sPort.write(new byte[]{0xd}, 3000);
} catch (Exception e) { } catch (Exception e) {
showToast(getApplication(),"Error:"+e.toString());
} }
} }
else
{
showToast(getApplication(),"SERIAL NULL");
}
} }
protected byte[] getStringToByte(String str,Charset enc) protected byte[] getStringToByte(String str,Charset enc)
{ {
ByteBuffer buf = ByteBuffer.allocate(100);; ByteBuffer buf = ByteBuffer.allocate(300);;
/* /*
sendSerial("U4PL13|soar|"); sendSerial("U4PL13|soar|");
sendSerial("U4PL23|헤어지자 (Prod. 정키)|"); sendSerial("U4PL23|헤어지자 (Prod. 정키)|");
@ -106,8 +118,10 @@ feff0041c9c0c5b4bc18005a
buf.put(arrStr[0].getBytes(Charset.forName("UTF-8"))); buf.put(arrStr[0].getBytes(Charset.forName("UTF-8")));
buf.put(new byte[]{0x7c}); buf.put(new byte[]{0x7c});
buf.put(arrStr[1].getBytes(enc)); buf.put(arrStr[1].getBytes(enc));
// Log.d("TAG", String.valueOf( buf.position()));
buf.put(new byte[]{0x7c}); buf.put(new byte[]{0x7c});
// System.out.println(new String(buf.array())); // System.out.println(new String(buf.array()));
// Log.d("TAG", String.valueOf( buf.position()));
} }
else else
@ -116,10 +130,11 @@ feff0041c9c0c5b4bc18005a
buf.put(str.getBytes(Charset.forName("UTF-8"))); buf.put(str.getBytes(Charset.forName("UTF-8")));
} }
byte[] bytes = new byte[buf.position()];
buf.get(bytes,0,buf.position());
return bytes;
return buf.array();
} }
@ -147,6 +162,7 @@ feff0041c9c0c5b4bc18005a
UsbDeviceConnection connection = null; UsbDeviceConnection connection = null;
try { try {
driver = availableDrivers.get(0); driver = availableDrivers.get(0);
connection = manager.openDevice(driver.getDevice()); connection = manager.openDevice(driver.getDevice());
if (connection == null) { if (connection == null) {
showToast(getApplication(), "No connection"); showToast(getApplication(), "No connection");
@ -154,8 +170,11 @@ feff0041c9c0c5b4bc18005a
} }
sPort = driver.getPorts().get(0); sPort = driver.getPorts().get(0);
// showToast(getApplication(), driver.getDevice().getProductName());
} catch (Exception e) { } catch (Exception e) {
showToast(getApplication(), e.toString()); showToast(getApplication(), e.toString());
return START_NOT_STICKY;
} }
try { try {
@ -172,6 +191,7 @@ feff0041c9c0c5b4bc18005a
// Ignore. // Ignore.
} }
sPort = null; sPort = null;
return START_NOT_STICKY;
} }
//set the time //set the time
//TMST001801140907 //TMST001801140907
@ -199,31 +219,9 @@ feff0041c9c0c5b4bc18005a
//U4PL03|1,1|0001010000000 U4PL13|soar| U4PL23|헤어지자 (Prod. 정키)| U4PL33|휘인 (Whee In)| //U4PL03|1,1|0001010000000 U4PL13|soar| U4PL23|헤어지자 (Prod. 정키)| U4PL33|휘인 (Whee In)|
showToast(getApplication(), currentTime);
// try {
// sPort.write("zxcv".getBytes(), 50);
// }
// catch(Exception e)
// {
//
// }
// SimpleDateFormat sdf = new SimpleDateFormat("aa hh:mm");
// boolean run = true;
// while (run) {
// try {
// Thread.sleep(1000 * 60 * 1); // 1 minute
// Date date = new Date();
// //showToast(getApplication(), sdf.format(date));
// sendNotification(sdf.format(date));
// } catch (InterruptedException e) {
// run = false;
// e.printStackTrace();
// }
// }
// }
// });
// mainThread.start();
IntentFilter iF = new IntentFilter(); IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.metachanged"); iF.addAction("com.android.music.metachanged");
@ -255,13 +253,21 @@ feff0041c9c0c5b4bc18005a
{ {
try try
{ {
Thread.sleep(1000); Thread.sleep(5000);
SimpleDateFormat format1 = new SimpleDateFormat("00yyMMddHHmm"); SimpleDateFormat format1 = new SimpleDateFormat("00yyMMddHHmm");
// String f = format1.format(System.currentTimeMillis()); // String f = format1.format(System.currentTimeMillis());
String currentInfo = format1.format(new Date()); String currentInfo = format1.format(new Date());
String currentTime = String.format("TMST%s", currentInfo); String currentTime = String.format("TMST%s", currentInfo);
sendSerial(currentTime); sendSerial(currentTime);
showToast(getApplication(), currentTime);
sendSerial("U4EN00");
sendSerial("*R|R1R2R3D1D2A1A3U4|");
sendSerial("U4PL03|5,3|0001010000000");
sendSerial("U4PL13|8소아|",Charset.forName("UTF-8"));
sendSerial("U4PL23|16헤어지자 (Prod. 정키)|", Charset.forName("UTF-16"));
sendSerial("U4PL33|32휘인 (Whee In)|",Charset.forName("UTF-32"));
} }
catch(InterruptedException e) catch(InterruptedException e)
{ {
@ -275,6 +281,49 @@ feff0041c9c0c5b4bc18005a
}).start(); }).start();
// new Thread(new Runnable() {
// @Override
// public void run()
// {
//
// boolean myRun = true;
// showToast(getApplication(), "READ start");
// while (myRun)
// {
// try
// {
// byte[] mArray = new byte[100];
// int len = sPort.read(mArray,1000);
// if(len >0)
// {
// String result = new String (mArray,0,len,Charset.defaultCharset());
// showToast(getApplication(), "READ:"+result);
// }
// else
// {
// showToast(getApplication(), "READ FAIL");
// Log.d("FAIL","FAIL");
// }
//
//
// }
// catch(Exception e)
// {
// myRun = false;
//
// }
// }
//
// }
//
// }).start();
return START_NOT_STICKY; return START_NOT_STICKY;
} }
@ -285,12 +334,13 @@ feff0041c9c0c5b4bc18005a
sPort.close(); sPort.close();
sPort = null; sPort = null;
} catch (Exception e) { } catch (Exception e) {
showToast(getApplication(),"stop Error");
} }
serviceIntent = null; serviceIntent = null;
setAlarmTimer(); setAlarmTimer();
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
unregisterReceiver(mReceiver); unregisterReceiver(mReceiver);
showToast(getApplication(),"stop Service");
// if (mainThread != null) { // if (mainThread != null) {
// mainThread.interrupt(); // mainThread.interrupt();
// mainThread = null; // mainThread = null;
@ -317,7 +367,7 @@ feff0041c9c0c5b4bc18005a
h.post(new Runnable() { h.post(new Runnable() {
@Override @Override
public void run() { public void run() {
Toast.makeText(application, msg, Toast.LENGTH_LONG).show(); Toast.makeText(application, msg, Toast.LENGTH_SHORT).show();
} }
}); });
} }

View File

@ -4,10 +4,12 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.widget.Toast;
public class RebootReceiver extends BroadcastReceiver { public class RebootReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent in = new Intent(context, RestartService.class); Intent in = new Intent(context, RestartService.class);
context.startForegroundService(in); context.startForegroundService(in);