cordova蓝牙打印插件

来源:互联网 发布:定义变量不赋值js 编辑:程序博客网 时间:2024/04/30 22:21

插件目录




BluetoothPrinter.java

package com.rensanning.cordova.bluetoothprinter;import java.util.Iterator;import java.util.List;import java.util.Set;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.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.util.Log;public class BluetoothPrinter extends CordovaPlugin {private BtService btService = null;@Overridepublic void initialize(CordovaInterface cordova, CordovaWebView webView) {super.initialize(cordova, webView);btService = new BtService(cordova, webView);}@Overridepublic void onDestroy() {super.onDestroy();btService.disconnect();}@Overridepublic 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("搜索蓝牙设备过程中出现错误");}}}
bluetoothPrinter.js
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;
plugin.xml
<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"        id="com.rensanning.cordova.bluetoothprinter"        version="0.0.1">   <name>BluetoothPrinter</name>   <description>Bluetooth Printer Plugin</description>   <author>rensanning</author>   <license>Apache 2.0 License</license>    <engines>        <engine name="cordova" version=">=3.0.0" />    </engines>   <js-module src="www/bluetoothPrinter.js" name="bluetoothPrinter">      <clobbers target="printer" />   </js-module>      <!-- android -->   <platform name="android">  <source-file src="src/android/BluetoothPrinter.java" target-dir="src/com/rensanning/cordova/bluetoothprinter" />      <config-file target="res/xml/config.xml" parent="/*">         <feature name="BluetoothPrinter">            <param name="android-package" value="com.rensanning.cordova.bluetoothprinter.BluetoothPrinter"/>         </feature>      </config-file>      <config-file target="AndroidManifest.xml" parent="/*">         <uses-permission android:name="android.permission.BLUETOOTH" />         <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />      </config-file>   </platform>      </plugin>


加入下面链接插件

http://pan.baidu.com/s/1qX5lBdi


0 0
原创粉丝点击