import { Platform } from 'react-native'; import Builder from './builder'; let devices = [ //sprt打印机设备 { vendorId: '1155', productId: '22304' }, // { vendorId: '2965', productId: '30507' }, { vendorId: '0483', productId: '5720' }, { vendorId: '0483', productId: '7540' }, { vendorId: '0483', productId: '1A86' }, //中奇 { vendorId: '1317', productId: '42754' }, { vendorId: '0525', productId: 'A702' }, { vendorId: '0416', productId: '5011' }, ]; export const getUSB = () => { var ExtSerial = require('qm-usbserial'); return 'ios' === Platform.OS && ExtSerial ? null : ExtSerial.default.UsbSerial; }; export const create = () => { let UsbSerial = getUSB(); if (null == UsbSerial) return; // UsbSerial.scan(); }; export const destroy = () => { let UsbSerial = getUSB(); if (null == UsbSerial) return; setTimeout(() => { devices.map(device => { UsbSerial.disconnect(device.vendorId, device.productId); }); }, 1500); }; export let connectUsbs = () => { let UsbSerial = getUSB(); if (null == UsbSerial) return; let array = []; // UsbSerial.scan(); for (var device of devices) { let data = { name: `USB.print.${device.vendorId}.${device.productId}`, address: device.productId + '_' + device.vendorId, from: 'print.usb.58', bondState: 1, }; if (device.vendorId == '0483' && device.productId == '7540') { // 80 data.from = 'print.usb.80'; UsbSerial.connect(device.vendorId, device.productId, 0, 8, 1, 0).then(r => { if (r && r.value) array.push(data); }); } else { UsbSerial.connect(device.vendorId, device.productId, 0, 8, 1, 0).then(r => { if (r && r.value) array.push(data); }); } } setTimeout(() => { Builder.add_array(r => console.log(r))(array); }, 2000); UsbSerial.attached(_ => { let data = { name: `USB.print.${device.vendorId}.${device.productId}`, address: device.productId + '_' + device.vendorId, from: 'print.usb.58', bondState: 1, }; if (device.vendorId == '0483' && device.productId == '7540') { // 80 data.from = 'print.usb.80'; } UsbSerial.connect(device.vendorId, device.productId, 0, 8, 1, 0).then(r => { // if (r && r.value) array.push(data); if (r && r.value) Builder.add(r => console.log(r))(data); }); // if (r) { // devices.map((device) => { // if (device.vendorId === r.vendorId && device.productId === r.productId) { // let device = { // name: `USB.print.${r.vendorId}.${r.productId}`, // from: 'print.usb.58', // address: r.productId + '_' + r.vendorId, // bondState: 1 // }; // Builder.add((r) => console.log(r))(device); // UsbSerial.connect(r.vendorId, r.productId, 0, 8, 1, 0).then((_) => {}); // } // }); // } }); UsbSerial.detached(_ => { // if (r) { // devices.map((device) => { // if (device.vendorId === r.vendorId && device.productId === r.productId) { // let device = { // name: `USB.print.${r.vendorId}.${r.productId}`, // address: r.productId + '_' + r.vendorId, // from: 'print.usb.58', // bondState: 0 // }; // Builder.remove((r) => console.log(r))(device); // } // }); // } }); }; export const write = (vendorId, productId, text) => { let UsbSerial = getUSB(); if (null == UsbSerial) return; UsbSerial.send(vendorId, productId, text); }; export const barcode = (vendorId, productId, text) => { let UsbSerial = getUSB(); if (null == UsbSerial) return; UsbSerial.barCode(vendorId, productId, text); }; export const qrcode = (vendorId, productId, text) => { let UsbSerial = getUSB(); if (null == UsbSerial) return; UsbSerial.qrCode(vendorId, productId, text); }; export const cmd = (vendorId, productId, text) => { let UsbSerial = getUSB(); if (null == UsbSerial) return; UsbSerial.sendCMD(vendorId, productId, text); }; export const center = (vendorId, productId) => { let UsbSerial = getUSB(); if (null == UsbSerial) return; UsbSerial.printCenter(vendorId, productId); }; export const right = (vendorId, productId) => { let UsbSerial = getUSB(); if (null == UsbSerial) return; UsbSerial.printRight(vendorId, productId); }; export const left = (vendorId, productId) => { let UsbSerial = getUSB(); if (null == UsbSerial) return; UsbSerial.printLeft(vendorId, productId); };