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
- cordova蓝牙打印插件
- Cordova插件
- cordova插件
- cordova插件
- 蓝牙打印
- Cordova-----3、Cordova使用插件
- Cordova-----4、Cordova自定义插件
- admob cordova插件 / cordova admob 插件 教程
- Cordova插件开发
- AngularJS Cordova插件介绍
- AngularJS Cordova插件介绍
- Cordova插件开发
- Cordova文件传输插件fileTransfer
- cordova 插件代码
- cordova 自定义插件
- Cordova+ ios 插件开发
- Cordova插件开发
- cordova 报插件找不到
- gdb没有调试信息
- HDU 3091 Necklace (状态压缩dp)
- HDU-ACM-2012
- su-sudo
- PyCharm 修改新建文件时的头部模板
- cordova蓝牙打印插件
- Android InsertHelper解决插入速度慢的问题
- codeforces 397B On Corruption and Numbers
- HDU-ACM-2015
- Qt之进程间通信(QProcess)
- Autosqli——一个sqlmapapi的web管理应用
- 预装oemwin8/8.1升级win10ISO文件
- 摄影师挨饿受冻拍下的三清山雪景美图
- HDU-ACM-2023