cordova蓝牙打印插件

package com.rensanning.cordova.bluetoothprinter;



import java.util.List;


import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.rensanning.cordova.bluetoothprinter.helper.BtService;
import com.rensanning.cordova.bluetoothprinter.helper.Device;


import android.util.Log;


public class BluetoothPrinter extends CordovaPlugin {


private BtService btService = null;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
btService = new BtService(cordova, webView);


}


@Override
public void onDestroy() {
super.onDestroy();
btService.disconnect();

}


@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
if (action.equals("list")) {
listBT(callbackContext);
return true;
} else if (action.equals("open")) {
String name = args.getString(0);
//btService.connect(name);
btService.open(callbackContext);
if(btService.getState()==BtService.SUCCESS_CONNECT){
btService.write(new byte[] { 0x1b, 0x2b });
}
callbackContext.success("蓝牙打开:" + name);
return true;
} else if (action.equals("print")) {
String msg = args.getString(0);
btService.printText(msg);
callbackContext.success("打印数据:" + msg);
return true;
} else if (action.equals("close")) {
//btService.close(callbackContext);
return true;
} else if (action.equals("connect")) {
String name = args.getString(0);
btService.connect(name);
return true;

return false;
}

void listBT(CallbackContext callbackContext) {
try {

List<Device> pairedDevices = btService.getDeviceList();
if (pairedDevices.size() > 0) {
JSONArray json = new JSONArray();
for (Device device : pairedDevices) {
JSONObject jObj = new JSONObject();


jObj.put("name", device.deviceName);
jObj.put("address", device.deviceAddress);
jObj.put("id", device.deviceAddress);


json.put(jObj);
}
callbackContext.success(json);
} else {
callbackContext.error("没有找到已配对蓝牙设备");
}
// 寻找蓝牙设备,android会将查找到的设备以广播形式发出去
btService.scan();
} catch (Exception e) {
Log.e("BuletoothPrinter", "listBT fail:", e);
callbackContext.error("搜索蓝牙设备过程中出现错误");
}
}

}


________________________________________________________________________________________

__________________________________________________________________________________________

cordova.define("com.rensanning.cordova.bluetoothprinter.bluetoothPrinter", function(require, exports, module) { var exec = require('cordova/exec');


var printer = {
   list: function(fnSuccess, fnError){
      exec(fnSuccess, fnError, "BluetoothPrinter", "list", []);
   },
   open: function(fnSuccess, fnError, name){
      exec(fnSuccess, fnError, "BluetoothPrinter", "open", [name]);
   },
   close: function(fnSuccess, fnError){
      exec(fnSuccess, fnError, "BluetoothPrinter", "close", []);
   },
   print: function(fnSuccess, fnError, str){
      exec(fnSuccess, fnError, "BluetoothPrinter", "print", [str]);
   },
   connect: function (success, failure, name) {
      exec(success, failure, "BluetoothPrinter", "connect", [name]);
   }
};


module.exports = printer;


});



加入2个JAR包

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值