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包