这是部分代码
获取代码 ->发送标题作为验证信息到该邮箱 itperson@foxmail.com
/** js spi 蓝牙连接 接收 mac address ;返回mac address */
@UniJSMethod
public void bluetoothConnect(String macAddress, UniJSCallback callback) {
checkPermission();
/* 连接前先关闭之前连接的打印机 */
closePort();
new DeviceConnFactoryManager.Build().setId(id).setConnMethod(DeviceConnFactoryManager.CONN_METHOD.BLUETOOTH).setMacAddress(macAddress).build();
//配置完信息,就可以打开端口连接了
Log.i("TAG", ":连接蓝牙"+id);
threadPool = ThreadPool.getInstantiation();
threadPool.addTask(new Runnable() {
@Override
public void run() {
DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].openPort();
}
});
callback.invoke(getConnDeviceInfo());
}
/** js spi 获取usb设备列表 返回usb列表数据*/
@UniJSMethod(uiThread = false)
public void getUsbDeviceList(UniJSCallback callback){
//获取USB管理器
usbManager = (UsbManager) Application.getApplication().getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
ArrayList<UsbDevice> usbDeviceList = new ArrayList<>();
int count = deviceList.size();
Log.d("设备数量----", "count " + count);
if (count>0){
// TODO 需要过滤掉出入设备
usbDeviceList.addAll(deviceList.values());
}
callback.invokeAndKeepAlive(usbDeviceList);
}
/** js spi usb连接 接收USB设备名称 ;返回连接设备名称*/
@UniJSMethod
public void usbConnect(String usbName, JSCallback callback) throws JSONException {
Log.i("TAG", ":USB连接"+usbName);
// String usbName = jsonObject.getString("deviceName");
/* 注册连接广播 */
initBroadcast();
/* 连接前先关闭之前连接的打印机 */
closePort();
Log.i("TAG", ":USB连接---"+usbName);
//通过USB设备名找到USB设备
UsbDevice usbDevice = MqUtils.getUsbDeviceFromName(Application.getApplication(), usbName);
//判断USB设备是否有权限
if (usbManager.hasPermission(usbDevice)) {
usbConn(usbDevice);
} else {//请求权限
mPermissionIntent = PendingIntent.getBroadcast(Application.getApplication(), 0, new Intent(ACTION_USB_PERMISSION), 0);
usbManager.requestPermission(usbDevice, mPermissionIntent);
}
callback.invoke(getConnDeviceInfo());
}
/** js api 打印 测试不用传json值 */
@UniJSMethod
public void printerContent(JSONObject json) {
Log.i("TAG", "准备打印");
threadPool = ThreadPool.getInstantiation();
threadPool.addTask(new Runnable() {
@Override
public void run() {
//先判断打印机是否连接
if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] == null || !DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getConnState()
) {
mHandler.obtainMessage(CONN_PRINTER).sendToTarget();
}
if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getCurrentPrinterCommand() == PrinterCommand.ESC) {
DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].sendDataImmediately(PrintContent.printer());
} else {
mHandler.obtainMessage(PRINTER_COMMAND_ERROR).sendToTarget();
}
}
});
}