Commit b2f7cb8f authored by YONG-LIN SU's avatar YONG-LIN SU

1. 新增LK-P34L印表機,印製嘉義路邊停車單功能

2. 新增LK-P34L,電量讀取功能
parent 7296d6e9
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="577125220043" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-08-18T02:22:35.164972600Z" />
</component>
</project>
\ No newline at end of file
...@@ -56,6 +56,7 @@ android { ...@@ -56,6 +56,7 @@ android {
dependencies { dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation files('libs\\Sewoo_Android_1110.jar')
implementation 'androidx.appcompat:appcompat:1.4.2' implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1' implementation 'com.google.android.material:material:1.6.1'
......
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
......
...@@ -318,4 +318,6 @@ public class BlueToothComm { ...@@ -318,4 +318,6 @@ public class BlueToothComm {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
\ No newline at end of file
package ecom.android.newparkapp.adapter; package ecom.android.newparkapp.adapter;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.BindingAdapter; import androidx.databinding.BindingAdapter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -62,4 +65,54 @@ public class ConvertBindingAdapter { ...@@ -62,4 +65,54 @@ public class ConvertBindingAdapter {
textView.setTextColor(colorCode); textView.setTextColor(colorCode);
} }
@BindingAdapter("printerPowerPercent2String")
public static void PrinterPowerPercent2String(TextView textView, float printerPowerPercent){
String printerPowerPercentString = "未偵測到電池";
if (printerPowerPercent >= 0){
printerPowerPercentString = printerPowerPercent + "%";
}
textView.setText(printerPowerPercentString);
}
@BindingAdapter("printerPowerPercent2Image")
public static void PrinterPowerPercent2Image(ImageView imageView, float printerPowerPercent){
String uri = "@drawable/no_battery";
int batteryLevel = (int) printerPowerPercent/20;
if (printerPowerPercent < 0)
{
batteryLevel = -1;
}
switch (batteryLevel){
case 0:
uri = "@drawable/low_battery";
break;
case 1:
uri = "@drawable/low_battery";
break;
case 2:
uri = "@drawable/middle_battery";
break;
case 3:
uri = "@drawable/middle_battery";
break;
case 4:
uri = "@drawable/full_battery";
break;
case 5:
uri = "@drawable/full_battery";
break;
default:
uri = "@drawable/no_battery";
break;
}
int imageResource = imageView.getResources().getIdentifier(uri, null, imageView.getContext().getPackageName());
int currentImageResource = imageView.getTag()==null ? -1 : (int)imageView.getTag();
if (imageResource != currentImageResource){
Drawable res = ResourcesCompat.getDrawable(imageView.getResources(), imageResource, null);
imageView.setImageDrawable(res);
imageView.setTag(imageResource);
}
}
} }
\ No newline at end of file
package ecom.android.newparkapp.entity; package ecom.android.newparkapp.entity;
import android.bluetooth.BluetoothDevice;
import android.content.pm.PackageManager;
import android.location.Location; import android.location.Location;
import android.os.Build;
import android.os.Environment; import android.os.Environment;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Embedded; import androidx.room.Embedded;
import androidx.room.Entity; import androidx.room.Entity;
...@@ -23,7 +18,6 @@ import java.util.Locale; ...@@ -23,7 +18,6 @@ import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import ecom.android.newparkapp.Common; import ecom.android.newparkapp.Common;
import ecom.android.newparkapp.R;
import ecom.android.newparkapp.printer.CarPrinter_TPB250; import ecom.android.newparkapp.printer.CarPrinter_TPB250;
import ecom.android.newparkapp.printer.IBluetoothPrinter; import ecom.android.newparkapp.printer.IBluetoothPrinter;
...@@ -289,12 +283,16 @@ public class Case { ...@@ -289,12 +283,16 @@ public class Case {
if (Objects.equals(bluetoothDeviceName.toUpperCase().trim(), "P58C") if (Objects.equals(bluetoothDeviceName.toUpperCase().trim(), "P58C")
|| Objects.equals(bluetoothDeviceName.toUpperCase().trim(), "YH-TPB250")) { || Objects.equals(bluetoothDeviceName.toUpperCase().trim(), "YH-TPB250")) {
printer = new CarPrinter_TPB250();//汽車用TPB250型印表機版面 printer = new CarPrinter_TPB250();//汽車用TPB250型印表機版面
} else { }else if (Objects.equals(bluetoothDeviceName.toUpperCase().trim(), "SW_891D")){
// TODO: 2022/8/3 加入 CarPrinter_LK-P34L 印表機功能
printer = null;
}
else {
// TODO: 2022/8/3 實作 CarPrinter_PK105B 印表機功能 // TODO: 2022/8/3 實作 CarPrinter_PK105B 印表機功能
printer = null; printer = null;
//printer = new CarPrinter_PK105B();//汽車用PK105B型印表機版面 //printer = new CarPrinter_PK105B();//汽車用PK105B型印表機版面
} }
// TODO: 2022/8/3 加入 CarPrinter_LK-P34L 印表機功能
return printer; return printer;
} }
......
package ecom.android.newparkapp.printer;
import static ecom.android.newparkapp.printer.IBluetoothPrinter.ResultCode.NOT_CONNECT;
import static ecom.android.newparkapp.printer.IBluetoothPrinter.ResultCode.PRINT_SUCCESS;
import android.bluetooth.BluetoothSocket;
import android.graphics.Typeface;
import com.sewoo.jpos.command.CPCLConst;
import com.sewoo.jpos.command.ESCPOS;
import com.sewoo.jpos.command.ESCPOSConst;
import com.sewoo.jpos.printer.CPCLPrinter;
import com.sewoo.jpos.printer.ESCPOSPrinter;
import com.sewoo.jpos.printer.LKPrint;
import com.sewoo.port.android.BluetoothPort;
import java.io.IOException;
import java.io.OutputStream;
import ecom.android.newparkapp.entity.Case;
public class CarPrinter_LKP34L{
private ExtendCPCLPrinter cpclPrinter;
public CarPrinter_LKP34L() {
// 設置中文編碼
cpclPrinter = new ExtendCPCLPrinter("Big5");
}
public IBluetoothPrinter.ResultCode print(BluetoothPort bluetoothPort, Case thisCase) throws IOException {
Case carCase = thisCase;
if (!bluetoothPort.isConnected()) {
return NOT_CONNECT;
}
//2-inch
// 嘉義路邊停車列印測試
// 紙張 黑色標記底部 至 黑色標記頂部 距離為 14.6 cm,減去 1.5 mm 為 印表機最大高度 14.45 cm
// 紙張 寬為 5.6 cm
// LK-P34L dpi 為 203,最大高度為 1155 px,最大寬度為 448px
int nLineWidth = 448;
cpclPrinter.setForm(102, 203, 203, 1155, nLineWidth,1);
int paper_type = 0; // 0 -> Gas Paper, 1 -> Black Mark Paper, 2 -> Continuous Paper
cpclPrinter.setMedia(paper_type);
// 列印 billNumber1 Code 39
String billNumber1 = carCase.billingNumber1;
cpclPrinter.setCPCLBarcode(7,0,0);
cpclPrinter.printCPCLBarcode(CPCLConst.LK_CPCL_0_ROTATION, CPCLConst.LK_CPCL_BCS_39,1,CPCLConst.LK_CPCL_BCS_1RATIO, 60, 0, 0, billNumber1, 0);
// 列印 車號
String plateNumber = "8052-LZ";
cpclPrinter.printAndroidFont(110,100, Typeface.SANS_SERIF, plateNumber, nLineWidth, 35);
// 列印 車格編號
String spaceWithRoad = "A213 中山路A";
cpclPrinter.printAndroidFont(110,150, Typeface.SANS_SERIF, spaceWithRoad, nLineWidth, 35);
// 列印 停車日期
String parkingDate = "111年08月03日";
cpclPrinter.printAndroidFont(110,200,Typeface.SANS_SERIF, parkingDate, nLineWidth, 35);
// 列印 billNumber2 Code 39
String billNumber2 = "77B18300298";
cpclPrinter.setCPCLBarcode(7,0,0);
cpclPrinter.printCPCLBarcode(CPCLConst.LK_CPCL_0_ROTATION, CPCLConst.LK_CPCL_BCS_39,1,CPCLConst.LK_CPCL_BCS_1RATIO, 70, 0, 260, billNumber2, 0);
// 列印 繳費期限
String terminateYear = "111";
String terminateMonth = "08";
String terminateDay = "29";
cpclPrinter.printAndroidFont(103, 380, Typeface.SANS_SERIF, terminateYear, nLineWidth, 25);
cpclPrinter.printAndroidFont(203, 380, Typeface.SANS_SERIF, terminateMonth, nLineWidth, 25);
cpclPrinter.printAndroidFont(303, 380, Typeface.SANS_SERIF, terminateDay, nLineWidth, 25);
// 列印 停車時間
String parkingTime = "14:58";
String parkingFee = "25";
String userName = "系統測試";
int printYMovement = 50;
int printY = 480;
for (int i = 0; i < 4; i++){
if (i == 0){
// 繳費金額
cpclPrinter.printAndroidFont(130, printY, Typeface.SANS_SERIF, parkingFee, nLineWidth, 30);
// 簽章
cpclPrinter.printAndroidFont(240, printY, Typeface.SANS_SERIF, userName, nLineWidth, 30);
}else {
printY+=printYMovement;
}
// 停車時間
cpclPrinter.printAndroidFont(15, printY, Typeface.SANS_SERIF, parkingTime, nLineWidth, 30);
// 簽章
cpclPrinter.printAndroidFont(240, printY, Typeface.SANS_SERIF, "________", nLineWidth, 30);
}
// 開始列印
cpclPrinter.printForm();
return PRINT_SUCCESS;
}
public String getPowerPercentFull(){
return cpclPrinter.getPowerPercentFull();
}
// 基於原先 SDK CPCLPrinter 進行命令擴充
private class ExtendCPCLPrinter extends CPCLPrinter {
public ExtendCPCLPrinter(){
super();
}
public ExtendCPCLPrinter(String charset){
super(charset);
}
public String getPowerPercentFull(){
return getVar("power.PercentFull");
}
public String getPowerStatus(){
return getVar("power.status");
}
public String getPowerVoltage(){
return getVar("power.voltage");
}
private String getVar(String parameter){
String varString = "";
byte[] recivedBuffer = new byte[8];
this.clearBuffer();
StringBuffer currentStringBuffer = this.getBuffer();
currentStringBuffer.append("!");
currentStringBuffer.append(" ");
currentStringBuffer.append("U1");
currentStringBuffer.append(" ");
currentStringBuffer.append("getvar");
currentStringBuffer.append(" ");
currentStringBuffer.append(parameter);
// currentStringBuffer.append("power.PercentFull"); // 34, 57, 49, 37, 34 -> "91%"
// currentStringBuffer.append("power.status"); // 34, 111, 107, 34 -> "ok"
// currentStringBuffer.append("power.voltage"); // 34, 56, 46, 49, 34 -> "8.1"
currentStringBuffer.append(" ");
currentStringBuffer.append("\r\n");
this.requestQueue.addRequest(currentStringBuffer.toString().getBytes());
int resultCode = -1;
try {
resultCode = this.receiveByte(recivedBuffer);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (resultCode == -1){
return varString;
}
byte[] bytes = new byte[resultCode];
for (int i = 0; i < resultCode; i++){
bytes[i] = recivedBuffer[i];
}
return new String(bytes);
}
}
}
package ecom.android.newparkapp.printer;
import com.sewoo.jpos.command.ESCPOSConst;
import com.sewoo.jpos.printer.ESCPOSPrinter;
public class LKP34LCheckPrinterStatus {
private int returnValue;
public int PrinterStatus(ESCPOSPrinter posPtr)
{
returnValue = posPtr.printerCheck();
if(ESCPOSConst.LK_SUCCESS == returnValue)
{
returnValue = posPtr.status();
if(ESCPOSConst.LK_STS_NORMAL == returnValue)
{ // No errors
return returnValue;
} else {
// Cover open error.
if((ESCPOSConst.LK_STS_COVER_OPEN & returnValue) > 0)
{
return ESCPOSConst.LK_STS_COVER_OPEN;
}
// Paper empty error.
if((ESCPOSConst.LK_STS_PAPER_EMPTY & returnValue) > 0)
{
return ESCPOSConst.LK_STS_PAPER_EMPTY;
}
// Battery low warning.
if((ESCPOSConst.LK_STS_BATTERY_LOW & returnValue) > 0)
{
return ESCPOSConst.LK_STS_BATTERY_LOW;
}
}
} else { // Error(LK_FAIL, LK_STS_PRINTEROFF, LK_STS_TIMEOUT)
return returnValue;
}
return 0;
}
}
package ecom.android.newparkapp.printer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import com.sewoo.jpos.command.ESCPOS;
import com.sewoo.jpos.command.ESCPOSConst;
import com.sewoo.jpos.printer.ESCPOSPrinter;
import com.sewoo.jpos.printer.LKPrint;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Sample_Print {
private ESCPOSPrinter escposPrinter;
private final char ESC = ESCPOS.ESC;
private final char LF = ESCPOS.LF;
private LKP34LCheckPrinterStatus check_status;
private int sts;
public Sample_Print()
{
escposPrinter = new ESCPOSPrinter(); //Default = English.
//escposPrinter = new ESCPOSPrinter("EUC-KR"); // Korean.
//escposPrinter = new ESCPOSPrinter("GB2312"); //Chinese.
check_status = new LKP34LCheckPrinterStatus();
}
public int Print_Sample_2() throws UnsupportedEncodingException
{
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printNormal(ESC + "|cA" + ESC + "|bC" + ESC + "|2C" + "Receipt" + LF + LF);
escposPrinter.printNormal(ESC+"|rATEL (123)-456-7890\n\n\n");
escposPrinter.printNormal(ESC+"|cAThank you!!!\n");
escposPrinter.printNormal("Chicken $10.00\n");
escposPrinter.printNormal("Hamburger $20.00\n");
escposPrinter.printNormal("Pizza $30.00\n");
escposPrinter.printNormal("Lemons $40.00\n");
escposPrinter.printNormal("Drink $50.00\n");
escposPrinter.printNormal("Excluded tax $150.00\n");
escposPrinter.printNormal(ESC+"|uCTax(5%) $7.50\n");
escposPrinter.printNormal(ESC+"|bC"+ESC+"|2CTotal $157.50\n\n");
escposPrinter.printNormal("Payment $200.00\n");
escposPrinter.printNormal("Change $42.50\n\n");
escposPrinter.printBarCode("{Babc456789012", LKPrint.LK_BCS_Code128, 40, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW); // Print Barcode
return 0;
}
public int Print_Sample_3() throws UnsupportedEncodingException
{
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printText("Receipt\r\n\r\n\r\n", LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_2WIDTH);
escposPrinter.printText("TEL (123)-456-7890\r\n", LKPrint.LK_ALIGNMENT_RIGHT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Thank you for coming to our shop!\r\n", LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Chicken $10.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Hamburger $20.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Pizza $30.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Lemons $40.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Drink $50.00\r\n\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Excluded tax $150.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Tax(5%) $7.50\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_UNDERLINE, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Total $157.50\r\n\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_2WIDTH);
escposPrinter.printText("Payment $200.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Change $42.50\r\n\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
// Reverse print.
escposPrinter.printText("Change $42.50\r\n\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT | LKPrint.LK_FNT_REVERSE, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printBarCode("0123456789", LKPrint.LK_BCS_Code39, 60, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
return 0;
}
public int Print_Sample_3_korea() throws IOException
{
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
String data = "QRCode Test Left Alignment";
escposPrinter.printBitmap("//sdcard//temp//test//logo_m.jpg", LKPrint.LK_ALIGNMENT_CENTER);
escposPrinter.lineFeed(1);
escposPrinter.printQRCode(data, data.length(), 6, ESCPOSConst.LK_QRCODE_EC_LEVEL_L, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printText("Thermal Receipt print\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_BOLD, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_2HEIGHT);
escposPrinter.printText("[영수증 출력]\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_BOLD, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_2HEIGHT);
escposPrinter.lineFeed(1);
escposPrinter.printText("SEWOO COFFEE SHOP\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_BOLD, LKPrint.LK_TXT_2WIDTH);
escposPrinter.printText("사업장 : 경기도 오산시 가장산업동로 28-6\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("매출일 : 2020-05-30 19:25:36\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("영수증 : No. 20200530-01-0049\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.lineFeed(1);
escposPrinter.printText("------------------------------------------------\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("상품명 단가 수량 금액 \r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_BOLD, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("------------------------------------------------\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("Choco Latte 5000 1 5000\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("Iced Coffee 3500 1 3500\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("녹차라떼 4500 2 9000\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("사이다 3500 2 7000\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("------------------------------------------------\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("합계 금액[Total Amount] 24,500\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_BOLD, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printText("------------------------------------------------\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH | LKPrint.LK_TXT_1HEIGHT);
escposPrinter.printBarCode("{B1234567890", LKPrint.LK_BCS_Code128, 60, 512, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
escposPrinter.lineFeed(4);
return 0;
}
public int Print_Sample_4() throws UnsupportedEncodingException
{
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printText("Receipt\r\n\r\n\r\n", LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_2WIDTH);
escposPrinter.printText("TEL (123)-456-7890\r\n", LKPrint.LK_ALIGNMENT_RIGHT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Thank you for coming to our shop!\r\n", LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Chicken $10.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Hamburger $20.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Pizza $30.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Lemons $40.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Drink $50.00\r\n\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Excluded tax $150.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Tax(5%) $7.50\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_UNDERLINE, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Total $157.50\r\n\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_2WIDTH);
escposPrinter.printText("Payment $200.00\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printText("Change $42.50\r\n\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT, LKPrint.LK_TXT_1WIDTH);
// Reverse print.
escposPrinter.printText("Change $42.50\r\n\r\n", LKPrint.LK_ALIGNMENT_LEFT, LKPrint.LK_FNT_DEFAULT | LKPrint.LK_FNT_REVERSE, LKPrint.LK_TXT_1WIDTH);
escposPrinter.printBarCode("0123456789", LKPrint.LK_BCS_Code39, 80, 3, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
return 0;
}
public int Print_1D_Barcode() throws UnsupportedEncodingException
{
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printBarCode("1234567890", LKPrint.LK_BCS_Code39, 40, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
escposPrinter.printBarCode("0123498765", LKPrint.LK_BCS_Code93, 40, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
escposPrinter.printBarCode("0987654321", LKPrint.LK_BCS_ITF, 40, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
escposPrinter.printBarCode("{ACODE 128", LKPrint.LK_BCS_Code128, 40, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
escposPrinter.printBarCode("{BCode 128", LKPrint.LK_BCS_Code128, 40, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
escposPrinter.printBarCode("{C12345", LKPrint.LK_BCS_Code128, 40, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
escposPrinter.printBarCode("A1029384756A", LKPrint.LK_BCS_Codabar, 40, 2, LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_HRI_TEXT_BELOW);
escposPrinter.printNormal(ESC + "|cA" + ESC + "|4C" + ESC + "|bC" + "Thank you" + LF);
escposPrinter.lineFeed(3);
return 0;
}
public int Print_2D_Barcode() throws UnsupportedEncodingException
{
String data = "ABCDEFGHIJKLMN";
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printString("PDF417\r\n");
escposPrinter.printPDF417(data, data.length(), 0, 10, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printPDF417(data, data.length(), 4, 3, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printString("QRCode\r\n");
escposPrinter.printQRCode(data, data.length(), 3, ESCPOSConst.LK_QRCODE_EC_LEVEL_L, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printNormal(ESC + "|cA" + ESC + "|4C" + ESC + "|bC" + "Thank you" + LF);
escposPrinter.lineFeed(4);
return 0;
}
public int Print_Image() throws IOException
{
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printBitmap("//sdcard//temp//test//logo_s.jpg", LKPrint.LK_ALIGNMENT_CENTER);
escposPrinter.printBitmap("//sdcard//temp//test//sample_2.jpg", LKPrint.LK_ALIGNMENT_CENTER);
escposPrinter.printBitmap("//sdcard//temp//test//sample_3.jpg", LKPrint.LK_ALIGNMENT_LEFT);
escposPrinter.printBitmap("//sdcard//temp//test//sample_4.jpg", LKPrint.LK_ALIGNMENT_RIGHT);
Bitmap _bitmap = BitmapFactory.decodeFile("//sdcard//temp//test//logo_s.jpg");
escposPrinter.printBitmap(_bitmap, LKPrint.LK_ALIGNMENT_CENTER);
escposPrinter.printBitmap(_bitmap, LKPrint.LK_ALIGNMENT_CENTER, 0, 1);
escposPrinter.printBitmap(_bitmap, LKPrint.LK_ALIGNMENT_LEFT, 1);
escposPrinter.printBitmap(_bitmap, LKPrint.LK_ALIGNMENT_LEFT, 2);
escposPrinter.printBitmap(_bitmap, LKPrint.LK_ALIGNMENT_LEFT, 3);
escposPrinter.lineFeed(1);
escposPrinter.printString("<Using dithering>\r\n"); //Use Dithering when using alpha values(transparency).
escposPrinter.setDithering(ESCPOSConst.LK_BITMAP_ORDERED_DITHER);
escposPrinter.printBitmap("//sdcard//temp//test//parking.jpg", LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_PAPER_2INCH);
escposPrinter.lineFeed(1);
escposPrinter.setDithering(ESCPOSConst.LK_BITMAP_ERROR_DIFFUSION);
escposPrinter.printBitmap("//sdcard//temp//test//parking.jpg", LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_PAPER_2INCH);
escposPrinter.printNormal(ESC + "|cA" + ESC + "|bC" + ESC + "|4C" + "Thank you" + LF);
escposPrinter.lineFeed(3);
return 0;
}
public int Print_Android_Font() throws IOException
{
int nLineWidth = 384;
String data = "Receipt";
Typeface typeface = null;
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printAndroidFont(data, nLineWidth, 100, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.lineFeed(2);
escposPrinter.printAndroidFont("Left Alignment", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont("Center Alignment", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printAndroidFont("Right Alignment", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_RIGHT);
escposPrinter.lineFeed(2);
escposPrinter.printAndroidFont(Typeface.SANS_SERIF, "SANS_SERIF : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SERIF, "SERIF : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(typeface.MONOSPACE, "MONOSPACE : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.lineFeed(2);
escposPrinter.printAndroidFont(Typeface.SANS_SERIF, "SANS : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SANS_SERIF, true, "SANS BOLD : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SANS_SERIF, true, false, "SANS BOLD : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SANS_SERIF, false, true, "SANS ITALIC : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SANS_SERIF, true, true, "SANS BOLD ITALIC : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SANS_SERIF, true, true, true, "SANS B/I/U : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.lineFeed(2);
escposPrinter.printAndroidFont(Typeface.SERIF, "SERIF : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SERIF, true, "SERIF BOLD : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SERIF, true, false, "SERIF BOLD : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SERIF, false, true, "SERIF ITALIC : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SERIF, true, true, "SERIF BOLD ITALIC : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.SERIF, true, true, true, "SERIF B/I/U : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.lineFeed(2);
escposPrinter.printAndroidFont(Typeface.MONOSPACE, "MONOSPACE : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.MONOSPACE, true, "MONO BOLD : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.MONOSPACE, true, false, "MONO BOLD : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.MONOSPACE, false, true, "MONO ITALIC : 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.MONOSPACE, true, true, "MONO BOLD ITALIC: 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.printAndroidFont(Typeface.MONOSPACE, true, true, true, "MONO B/I/U: 1234iwIW", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
escposPrinter.lineFeed(4);
return 0;
}
public int Print_Multilingual() throws IOException
{
int nLineWidth = 384;
String Koreandata = "영수증";
String Turkishdata = "Turkish(İ,Ş,Ğ)";
String Russiandata = "Получение";
String Arabicdata = "الإيصال";
String Greekdata = "Παραλαβή";
String Japanesedata = "領収書";
String GB2312data = "收据";
String BIG5data = "收據";
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printAndroidFont("Korean Font", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
// Korean 100-dot size font in android device.
escposPrinter.printAndroidFont(Koreandata, nLineWidth, 100, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printAndroidFont("Turkish Font", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
// Turkish 50-dot size font in android device.
escposPrinter.printAndroidFont(Turkishdata, nLineWidth, 50, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printAndroidFont("Russian Font", 384, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
// Russian 60-dot size font in android device.
escposPrinter.printAndroidFont(Russiandata, nLineWidth, 60, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printAndroidFont("Arabic Font", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
// Arabic 100-dot size font in android device.
escposPrinter.printAndroidFont(Arabicdata, nLineWidth, 100, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printAndroidFont("Greek Font", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
// Greek 60-dot size font in android device.
escposPrinter.printAndroidFont(Greekdata, nLineWidth, 60, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printAndroidFont("Japanese Font", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
// Japanese 100-dot size font in android device.
escposPrinter.printAndroidFont(Japanesedata, nLineWidth, 100, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printAndroidFont("GB2312 Font", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
// GB2312 100-dot size font in android device.
escposPrinter.printAndroidFont(GB2312data, nLineWidth, 100, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.printAndroidFont("BIG5 Font", nLineWidth, 24, ESCPOSConst.LK_ALIGNMENT_LEFT);
// BIG5 100-dot size font in android device.
escposPrinter.printAndroidFont(BIG5data, nLineWidth, 100, ESCPOSConst.LK_ALIGNMENT_CENTER);
escposPrinter.lineFeed(4);
return 0;
}
public int Print_PDF() throws IOException
{
sts = check_status.PrinterStatus(escposPrinter);
if(sts != ESCPOSConst.LK_SUCCESS) return sts;
escposPrinter.printPDFFile("//sdcard//temp//test//PDF_Sample.pdf", LKPrint.LK_ALIGNMENT_CENTER, LKPrint.LK_PAPER_2INCH, 2);
escposPrinter.lineFeed(3);
escposPrinter.printPDFFile("//sdcard//temp//test//PDF_Sample.pdf", LKPrint.LK_ALIGNMENT_LEFT, 300, 2); //custom size
escposPrinter.lineFeed(3);
return 0;
}
}
...@@ -49,6 +49,7 @@ import ecom.android.newparkapp.entity.Space; ...@@ -49,6 +49,7 @@ import ecom.android.newparkapp.entity.Space;
import ecom.android.newparkapp.entity.User; import ecom.android.newparkapp.entity.User;
import ecom.android.newparkapp.entity.VehicleType; import ecom.android.newparkapp.entity.VehicleType;
import ecom.android.newparkapp.repository.ParkApiRepository; import ecom.android.newparkapp.repository.ParkApiRepository;
import ecom.android.newparkapp.viewModel.BlueToothPortViewModel;
import ecom.android.newparkapp.viewModel.BlueToothViewModel; import ecom.android.newparkapp.viewModel.BlueToothViewModel;
import ecom.android.newparkapp.viewModel.FusedGpsViewModel; import ecom.android.newparkapp.viewModel.FusedGpsViewModel;
import ecom.android.newparkapp.viewModel.T01SettingViewModel; import ecom.android.newparkapp.viewModel.T01SettingViewModel;
...@@ -62,7 +63,9 @@ public class T02StartActivity extends AppCompatActivity { ...@@ -62,7 +63,9 @@ public class T02StartActivity extends AppCompatActivity {
private T02StartViewModel t02StartViewModel; private T02StartViewModel t02StartViewModel;
private T01SettingViewModel t01SettingViewModel; private T01SettingViewModel t01SettingViewModel;
private FusedGpsViewModel fusedGpsViewModel; private FusedGpsViewModel fusedGpsViewModel;
private BlueToothViewModel blueToothViewModel;
//private BlueToothViewModel blueToothViewModel;
private BlueToothPortViewModel blueToothViewModel;
//藍芽相關 //藍芽相關
private ArrayAdapter<String> deviceNameArrayAdapter; private ArrayAdapter<String> deviceNameArrayAdapter;
...@@ -100,7 +103,10 @@ public class T02StartActivity extends AppCompatActivity { ...@@ -100,7 +103,10 @@ public class T02StartActivity extends AppCompatActivity {
t02StartViewModel = viewModelProvider.get(T02StartViewModel.class); t02StartViewModel = viewModelProvider.get(T02StartViewModel.class);
t01SettingViewModel = viewModelProvider.get(T01SettingViewModel.class); t01SettingViewModel = viewModelProvider.get(T01SettingViewModel.class);
fusedGpsViewModel = viewModelProvider.get(FusedGpsViewModel.class); fusedGpsViewModel = viewModelProvider.get(FusedGpsViewModel.class);
blueToothViewModel = viewModelProvider.get(BlueToothViewModel.class);
//blueToothViewModel = viewModelProvider.get(BlueToothViewModel.class);
// 測試
blueToothViewModel = viewModelProvider.get(BlueToothPortViewModel.class);
currentUser = t01SettingViewModel.getCurrentUser(); currentUser = t01SettingViewModel.getCurrentUser();
shift = getIntent().getParcelableExtra("Shift"); shift = getIntent().getParcelableExtra("Shift");
......
package ecom.android.newparkapp.viewModel;
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.widget.ArrayAdapter;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.sewoo.port.android.BluetoothPort;
import com.sewoo.request.android.RequestHandler;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import ecom.android.newparkapp.R;
import ecom.android.newparkapp.entity.Case;
import ecom.android.newparkapp.printer.CarPrinter_LKP34L;
import ecom.android.newparkapp.printer.IBluetoothPrinter;
import ecom.android.newparkapp.printer.Sample_Print;
public class BlueToothPortViewModel extends AndroidViewModel {
private ExecutorService executorService;
private BluetoothAdapter bluetoothAdapter;
private BTDeviceReceiver btDeviceReceiver;
private ArrayAdapter<String> deviceNameArrayAdapter;
private String[] bluetoothPermissions = new String[]{};
private MutableLiveData<BluetoothDevice> bluetoothDevice = new MutableLiveData<>();
private MutableLiveData<BlueToothViewModel.BlueToothStatus> blueToothStatusMutableLiveData = new MutableLiveData<>();
private MutableLiveData<Float> printerPowerPercentFull = new MutableLiveData<>();
private Timer getPrinterPowerPercentFullTimer = new Timer();
private TimerTask getPrinterPowerPercentFullTimerTask;
private BluetoothPort bluetoothPort;
private Thread bluetoothRequestThread;
private CarPrinter_LKP34L printer = new CarPrinter_LKP34L();
public BlueToothPortViewModel(@NonNull Application application) {
super(application);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12 額外檢查藍芽權限
for (String permission : bluetoothPermissions){
if(ActivityCompat.checkSelfPermission(application, permission) != PackageManager.PERMISSION_GRANTED){
return;
}
}
}
bluetoothPort = BluetoothPort.getInstance();
bluetoothPort.SetMacFilter(false);
executorService = Executors.newFixedThreadPool(application.getResources().getInteger(R.integer.number_db_thread_pool));
blueToothStatusMutableLiveData.setValue(BlueToothViewModel.BlueToothStatus.INIT);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
deviceNameArrayAdapter = new ArrayAdapter<String>(application, R.layout.list_view_device_name_item);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
btDeviceReceiver = new BTDeviceReceiver();
application.registerReceiver(btDeviceReceiver, filter);
//取得已綁定過的裝置
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
deviceNameArrayAdapter.add(device.getName() + "\n"
+ device.getAddress());
}
} else {
deviceNameArrayAdapter.add(application.getString(R.string.none_paired));
}
// 初始化電量
printerPowerPercentFull.setValue(-1.0f);
initGetprinterPowerPercentTimer();
}
private void initGetprinterPowerPercentTimer(){
getPrinterPowerPercentFullTimerTask = new TimerTask() {
@Override
public void run() {
updatePrinterPowerPercentFull();
}
};
getPrinterPowerPercentFullTimer.schedule(getPrinterPowerPercentFullTimerTask, 1000, 5000);
}
public LiveData<Float> getPrinterPowerPercentFull(){
return printerPowerPercentFull;
}
public LiveData<BlueToothViewModel.BlueToothStatus> getBlueToothStatus(){
return blueToothStatusMutableLiveData;
}
public LiveData<BluetoothDevice> getBluetoothDevice(){
return bluetoothDevice;
}
public ArrayAdapter<String> getDeviceNameArrayAdapter(){
return deviceNameArrayAdapter;
}
public BluetoothAdapter getBluetoothAdapter(){
return bluetoothAdapter;
}
/**
* 藍芽連線
* @param device 藍芽裝置
*/
private void connect(BluetoothDevice device) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12 額外檢查藍芽權限
for (String permission : bluetoothPermissions){
if(ActivityCompat.checkSelfPermission(getApplication(), permission) != PackageManager.PERMISSION_GRANTED){
return;
}
}
}
if (device == null){
blueToothStatusMutableLiveData.setValue(BlueToothViewModel.BlueToothStatus.CONNECT_FAILED);
return;
}
bluetoothDevice.setValue(device);
executorService.execute(() -> {
blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.CONNECTING);
try {
BluetoothDevice thisBluetoothDevice = bluetoothDevice.getValue();
if (thisBluetoothDevice == null){
blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.CONNECT_FAILED);
return;
}
bluetoothPort.connect(thisBluetoothDevice);
if (bluetoothPort.isConnected()){
// 啟動 requestHandler 背景續,監聽處理各項藍芽任務
RequestHandler requestHandler = new RequestHandler();
bluetoothRequestThread = new Thread(requestHandler);
bluetoothRequestThread.start();
blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.CONNECT_SUCCESS);
}else {
blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.CONNECT_FAILED);
}
} catch (IOException e) {
e.printStackTrace();
try {
if (bluetoothPort != null) {
bluetoothPort.disconnect();
}
blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.CONNECT_FAILED);
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}
});
}
public void connect(String address){
BluetoothDevice bluetoothDeviceFromAddress = bluetoothAdapter.getRemoteDevice(address);
connect(bluetoothDeviceFromAddress);
}
/**
* 中斷當前藍芽連線
*/
public void disconnect(){
if (bluetoothPort.isConnected()){
try {
bluetoothPort.disconnect();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (bluetoothRequestThread != null || bluetoothRequestThread.isAlive()){
bluetoothRequestThread.interrupt();
}
}
/**
* 開始藍芽搜尋
*/
public void startDiscovery() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12 額外檢查藍芽權限
for (String permission : bluetoothPermissions){
if(ActivityCompat.checkSelfPermission(getApplication(), permission) != PackageManager.PERMISSION_GRANTED){
return;
}
}
}
if (bluetoothAdapter != null && !bluetoothAdapter.isDiscovering()){
bluetoothAdapter.startDiscovery();
}
}
/**
* 停止藍芽搜尋
*/
public void cancelDiscovery() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12 額外檢查藍芽權限
for (String permission : bluetoothPermissions){
if(ActivityCompat.checkSelfPermission(getApplication(), permission) != PackageManager.PERMISSION_GRANTED){
return;
}
}
}
if (bluetoothAdapter != null && bluetoothAdapter.isDiscovering()){
bluetoothAdapter.cancelDiscovery();
}
}
public void printCase(Case tempCase) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12 額外檢查藍芽權限
for (String permission : bluetoothPermissions){
if(ActivityCompat.checkSelfPermission(getApplication(), permission) != PackageManager.PERMISSION_GRANTED){
return;
}
}
}
blueToothStatusMutableLiveData.setValue(BlueToothViewModel.BlueToothStatus.PRINTING);
executorService.execute(() ->{
try {
printer.print(bluetoothPort, tempCase);
} catch (IOException e) {
e.printStackTrace();
}
blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.PRINT_COMPLETE);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.CONNECT_SUCCESS);
});
/*-----取得CasePrinter-----*/
// CarPrinter_LKP34L printer = new CarPrinter_LKP34L();
// try {
// printer.print(bluetoothPort, tempCase);
// } catch (IOException e) {
// e.printStackTrace();
// blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.PRINT_FAILED);
// return;
// }
//
// blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.PRINT_COMPLETE);
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// blueToothStatusMutableLiveData.postValue(BlueToothViewModel.BlueToothStatus.CONNECT_SUCCESS);
}
/**
* 監聽藍芽裝置廣播
*/
private class BTDeviceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, @NonNull Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12 額外檢查藍芽權限
for (String permission : bluetoothPermissions){
if(ActivityCompat.checkSelfPermission(getApplication(), permission) != PackageManager.PERMISSION_GRANTED){
return;
}
}
}
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {//如果藍芽掃瞄有裝置
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device == null) {
return;
}
if (device.getBondState() != BluetoothDevice.BOND_BONDED
&& deviceNameArrayAdapter.getPosition(device.getName() + "\n" + device.getAddress()) < 0) {
deviceNameArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if (deviceNameArrayAdapter.getCount() == 0) {
String noDevices = getApplication().getString(R.string.none_found);
deviceNameArrayAdapter.add(noDevices);
}
}
}
}
private void updatePrinterPowerPercentFull(){
if (!blueToothStatusMutableLiveData.getValue().equals(BlueToothViewModel.BlueToothStatus.CONNECT_SUCCESS)){
return;
}
String powerPercentFull = printer.getPowerPercentFull();
if (powerPercentFull != null || !powerPercentFull.trim().isEmpty()){
String purePowerPercentFull = powerPercentFull.substring(1, powerPercentFull.length() - 2);
float purePowerPercentFullFloat = Float.parseFloat(purePowerPercentFull);
printerPowerPercentFull.postValue(purePowerPercentFullFloat);
}
}
public enum BlueToothStatus{
INIT,CONNECTING ,CONNECT_SUCCESS, CONNECT_FAILED,PRINTING,PRINT_COMPLETE, PRINT_FAILED
}
}
...@@ -24,6 +24,8 @@ import androidx.lifecycle.AndroidViewModel; ...@@ -24,6 +24,8 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.sewoo.port.android.BluetoothPort;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Set; import java.util.Set;
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<variable <variable
name="blueToothViewModel" name="blueToothViewModel"
type="ecom.android.newparkapp.viewModel.BlueToothViewModel" /> type="ecom.android.newparkapp.viewModel.BlueToothPortViewModel" />
</data> </data>
<androidx.core.widget.NestedScrollView <androidx.core.widget.NestedScrollView
android:id="@+id/t02_start_border" android:id="@+id/t02_start_border"
...@@ -74,23 +74,25 @@ tools:layout_editor_absoluteY="25dp"> ...@@ -74,23 +74,25 @@ tools:layout_editor_absoluteY="25dp">
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="0.5" android:layout_weight="0.5"
android:orientation="vertical"> android:orientation="vertical"
android:layout_gravity="center">
<ImageView <ImageView
android:id="@+id/imageView" android:id="@+id/imageView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="20dp"
android:layout_weight="1" android:layout_weight="1"
android:rotation="0" android:rotation="0"
app:srcCompat="@android:drawable/button_onoff_indicator_on" /> app:printerPowerPercent2Image="@{blueToothViewModel.printerPowerPercentFull}"/>
<TextView <TextView
android:id="@+id/textView22" android:id="@+id/textView22"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:text=" 98%"
android:textAlignment="center" android:textAlignment="center"
android:textSize="16sp" /> android:textSize="16sp"
app:printerPowerPercent2String="@{blueToothViewModel.printerPowerPercentFull}"/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment