// package com.example;
|
|
// import android.content.Context;
|
// import io.dcloud.feature.uniapp.common.UniModule;
|
// import io.dcloud.feature.uniapp.annotation.UniJSMethod;
|
// import android.widget.Toast;
|
// import com.gprinter.command.GpCom;
|
// import com.gprinter.io.GpDevice;
|
|
// public class PrintPlugin extends UniModule {
|
// private GpDevice printer;
|
|
// @UniJSMethod(uiThread = true)
|
// public void initPrinter(String ipAddress) {
|
// try {
|
// printer = new GpDevice(getContext(), ipAddress);
|
// printer.connect();
|
// Toast.makeText(getContext(), "打印机连接成功", Toast.LENGTH_SHORT).show();
|
// } catch (Exception e) {
|
// Toast.makeText(getContext(), "打印机连接失败", Toast.LENGTH_SHORT).show();
|
// }
|
// }
|
|
// @UniJSMethod(uiThread = true)
|
// public void printData(String data) {
|
// try {
|
// GpCom.ERROR_CODE result = printer.sendEscCommand(data);
|
// if (result == GpCom.ERROR_CODE.SUCCESS) {
|
// Toast.makeText(getContext(), "打印成功", Toast.LENGTH_SHORT).show();
|
// } else {
|
// Toast.makeText(getContext(), "打印失败", Toast.LENGTH_SHORT).show();
|
// }
|
// } catch (Exception e) {
|
// Toast.makeText(getContext(), "打印失败", Toast.LENGTH_SHORT).show();
|
// }
|
// }
|
// }
|
|
|
package com.example;
|
|
import android.util.Log;
|
import io.dcloud.feature.uniapp.common.UniModule;
|
import io.dcloud.common.DHInterface.IWebview;
|
import io.dcloud.common.DHInterface.JsResponse;
|
import io.dcloud.common.DHInterface.MessageListener;
|
import io.dcloud.common.util.JSONUtil;
|
import org.json.JSONException;
|
import org.json.JSONObject;
|
|
public class PrintPlugin extends UniModule {
|
|
public void initPrinter(IWebview webview, JsResponse response) {
|
try {
|
// 初始化打印机的逻辑
|
Log.d("PrintPlugin", "初始化打印机");
|
String result = "USB 打印机已初始化";
|
response.success(webview, JSONUtil.toJson(result));
|
} catch (Exception e) {
|
response.error(webview, JSONUtil.toJson(e.getMessage()));
|
}
|
}
|
|
public void printData(IWebview webview, JsResponse response, String data) {
|
try {
|
// 打印机打印数据的逻辑
|
Log.d("PrintPlugin", "打印数据: " + data);
|
String result = "打印成功";
|
response.success(webview, JSONUtil.toJson(result));
|
} catch (Exception e) {
|
response.error(webview, JSONUtil.toJson(e.getMessage()));
|
}
|
}
|
}
|