添加is connect方法
This commit is contained in:
parent
50c40589c9
commit
9a78c731de
@ -38,9 +38,7 @@ import java.util.Set;
|
|||||||
public class JcPrinter extends CordovaPlugin {
|
public class JcPrinter extends CordovaPlugin {
|
||||||
|
|
||||||
|
|
||||||
private static final String TAG = "MainActivity";
|
private static final String TAG = "JcPrinter";
|
||||||
private static final String RB_THERMAL = "热转印";
|
|
||||||
private Context context;
|
|
||||||
|
|
||||||
private Method[] methods = Util.getInstance().getClass().getDeclaredMethods();
|
private Method[] methods = Util.getInstance().getClass().getDeclaredMethods();
|
||||||
|
|
||||||
@ -82,50 +80,55 @@ public class JcPrinter extends CordovaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||||
System.out.println("action = " + action);
|
try{
|
||||||
switch (action) {
|
Log.d(TAG,"action = " + action);
|
||||||
case "print":
|
switch (action) {
|
||||||
this.print(args, callbackContext);
|
case "print":
|
||||||
return true;
|
this.print(args, callbackContext);
|
||||||
|
return true;
|
||||||
|
|
||||||
case "getList":
|
case "getList":
|
||||||
this.getList(callbackContext);
|
this.getList(callbackContext);
|
||||||
return true;
|
return true;
|
||||||
case "connectPrinter":
|
case "connectPrinter":
|
||||||
this.connectPrinter(args, callbackContext);
|
this.connectPrinter(args, callbackContext);
|
||||||
return true;
|
return true;
|
||||||
case "isConnection":
|
case "isConnection":
|
||||||
this.isConnection(callbackContext);
|
this.isConnection(callbackContext);
|
||||||
return true;
|
return true;
|
||||||
case "action":
|
case "action":
|
||||||
String methodName = args.optString(0);
|
String methodName = args.optString(0);
|
||||||
JSONArray methodJsonArgs = args.getJSONArray(1);
|
JSONArray methodJsonArgs = args.getJSONArray(1);
|
||||||
Object[] methodArgs = new Object[methodJsonArgs.length()];
|
Object[] methodArgs = new Object[methodJsonArgs.length()];
|
||||||
for(int i=0;i<methodJsonArgs.length();i++){
|
for(int i=0;i<methodJsonArgs.length();i++){
|
||||||
Object obj = methodJsonArgs.opt(i);
|
Object obj = methodJsonArgs.opt(i);
|
||||||
if(obj instanceof JSONArray){
|
if(obj instanceof JSONArray){
|
||||||
Object[] array = new Object[((JSONArray) obj).length()];
|
Object[] array = new Object[((JSONArray) obj).length()];
|
||||||
for(int j=0;j<((JSONArray) obj).length();j++){
|
for(int j=0;j<((JSONArray) obj).length();j++){
|
||||||
array[i] = ((JSONArray) obj).opt(j);
|
array[i] = ((JSONArray) obj).opt(j);
|
||||||
}
|
}
|
||||||
obj = array;
|
obj = array;
|
||||||
}
|
}
|
||||||
methodArgs[i] = obj;
|
methodArgs[i] = obj;
|
||||||
}
|
}
|
||||||
for(Method m:this.methods){
|
for(Method m:this.methods){
|
||||||
if(m.getName() == methodName){
|
String name = m.getName();
|
||||||
try {
|
if(m.getName().equals(methodName)){
|
||||||
m.invoke(Util.getInstance(),methodArgs);
|
try {
|
||||||
callbackContext.success();
|
m.invoke(Util.getInstance(),methodArgs);
|
||||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
callbackContext.success();
|
||||||
callbackContext.error(e.getMessage());
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||||
}
|
callbackContext.error(e.getMessage());
|
||||||
return true;
|
}
|
||||||
}
|
return true;
|
||||||
}
|
}
|
||||||
case "generateLabelJson":
|
}
|
||||||
callbackContext.success(new String(Util.getInstance().generateLabelJson()));
|
case "generateLabelJson":
|
||||||
return true;
|
callbackContext.success(new String(Util.getInstance().generateLabelJson()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
callbackContext.error(e.getMessage());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -136,11 +139,9 @@ public class JcPrinter extends CordovaPlugin {
|
|||||||
*/
|
*/
|
||||||
private void print(JSONArray args, CallbackContext callbackContext) {
|
private void print(JSONArray args, CallbackContext callbackContext) {
|
||||||
JSONArray data = args.optJSONArray(0);
|
JSONArray data = args.optJSONArray(0);
|
||||||
JSONArray jsonInfo = args.optJSONArray(1);
|
String infoStr = args.optString(1);
|
||||||
List<String>info = new ArrayList<>();
|
List<String> info = new ArrayList<>();
|
||||||
for(int i=0;i<jsonInfo.length();i++){
|
info.add(infoStr);
|
||||||
info.add(jsonInfo.optString(i));
|
|
||||||
}
|
|
||||||
//重置错误状态变量
|
//重置错误状态变量
|
||||||
isError = false;
|
isError = false;
|
||||||
//重置取消打印状态变量
|
//重置取消打印状态变量
|
||||||
@ -178,6 +179,7 @@ public class JcPrinter extends CordovaPlugin {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(int i) {
|
public void onError(int i) {
|
||||||
callbackContext.error(i);
|
callbackContext.error(i);
|
||||||
@ -344,6 +346,9 @@ public class JcPrinter extends CordovaPlugin {
|
|||||||
private void connectPrinter(JSONArray args, CallbackContext callbackContext) throws JSONException {
|
private void connectPrinter(JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||||
JSONObject obj = args.getJSONObject(0);
|
JSONObject obj = args.getJSONObject(0);
|
||||||
String address = obj.getString("address");
|
String address = obj.getString("address");
|
||||||
|
if(Util.isConnection() == 0){
|
||||||
|
Util.close();
|
||||||
|
}
|
||||||
int connectResult = Util.openPrinter(address);
|
int connectResult = Util.openPrinter(address);
|
||||||
cordova.getActivity().runOnUiThread(() -> {
|
cordova.getActivity().runOnUiThread(() -> {
|
||||||
String hint = "";
|
String hint = "";
|
||||||
@ -378,10 +383,10 @@ public class JcPrinter extends CordovaPlugin {
|
|||||||
int blueTooth_admin_permission = ContextCompat.checkSelfPermission(cordova.getContext(), Manifest.permission.BLUETOOTH_ADMIN);
|
int blueTooth_admin_permission = ContextCompat.checkSelfPermission(cordova.getContext(), Manifest.permission.BLUETOOTH_ADMIN);
|
||||||
int blueTooth_privileged_permission = ContextCompat.checkSelfPermission(cordova.getContext(), Manifest.permission.BLUETOOTH_PRIVILEGED);
|
int blueTooth_privileged_permission = ContextCompat.checkSelfPermission(cordova.getContext(), Manifest.permission.BLUETOOTH_PRIVILEGED);
|
||||||
int blueTooth_connect_permission = ContextCompat.checkSelfPermission(cordova.getContext(), "android.permission.BLUETOOTH_CONNECT");
|
int blueTooth_connect_permission = ContextCompat.checkSelfPermission(cordova.getContext(), "android.permission.BLUETOOTH_CONNECT");
|
||||||
System.out.println("blueTooth_permission = " + blueTooth_permission);
|
Log.d(TAG,"blueTooth_permission = " + blueTooth_permission);
|
||||||
System.out.println("blueTooth_admin_permission = " + blueTooth_admin_permission);
|
Log.d(TAG,"blueTooth_admin_permission = " + blueTooth_admin_permission);
|
||||||
System.out.println("blueTooth_privileged_permission = " + blueTooth_privileged_permission);
|
Log.d(TAG,"blueTooth_privileged_permission = " + blueTooth_privileged_permission);
|
||||||
System.out.println("blueTooth_connect_permission = " + blueTooth_connect_permission);
|
Log.d(TAG,"blueTooth_connect_permission = " + blueTooth_connect_permission);
|
||||||
if (blueTooth_permission == PERMISSION_GRANTED) {
|
if (blueTooth_permission == PERMISSION_GRANTED) {
|
||||||
ActivityCompat.requestPermissions(cordova.getActivity(), new String[]{Manifest.permission.BLUETOOTH}, 1001);
|
ActivityCompat.requestPermissions(cordova.getActivity(), new String[]{Manifest.permission.BLUETOOTH}, 1001);
|
||||||
}
|
}
|
||||||
@ -414,7 +419,7 @@ public class JcPrinter extends CordovaPlugin {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(e.getMessage());
|
System.err.println(e.getMessage());
|
||||||
}
|
}
|
||||||
System.out.println("printers = " + printers);
|
Log.d(TAG,"printers = " + printers);
|
||||||
callbackContext.success(printers);
|
callbackContext.success(printers);
|
||||||
return printers;
|
return printers;
|
||||||
}
|
}
|
||||||
|
@ -26,22 +26,22 @@ var exec = require('cordova/exec');
|
|||||||
* @param {*} success
|
* @param {*} success
|
||||||
* @param {*} error
|
* @param {*} error
|
||||||
*/
|
*/
|
||||||
exports.print = function (data,info, success, error) {
|
exports.print = async function (data,info) {
|
||||||
exec(success, error, 'JcPrinter', 'print', [data,info]);
|
return new Promise((resolve, reject) => exec(resolve,reject, 'JcPrinter', 'print', [data,info]));
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getList = function (arg0, success, error) {
|
exports.getList = function (arg0) {
|
||||||
exec(success, error, 'JcPrinter', 'getList', [arg0]);
|
return new Promise((resolve, reject) => exec(resolve,reject, 'JcPrinter', 'getList', [arg0]));
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.connectPrinter = function (arg0, success, error) {
|
exports.connectPrinter = function (arg0) {
|
||||||
exec(success, error, 'JcPrinter', 'connectPrinter', [arg0]);
|
return new Promise((resolve, reject) => exec(resolve,reject,'JcPrinter', 'connectPrinter', [arg0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.connectPrinter = function (success, error) {
|
|
||||||
exec(success, error, 'JcPrinter', 'connectPrinter');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
exports.isConnection = function (arg0) {
|
||||||
|
return new Promise((resolve, reject) => exec(resolve,reject,'JcPrinter', 'isConnection', [arg0]));
|
||||||
|
}
|
||||||
|
|
||||||
exports.Instance = {
|
exports.Instance = {
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user