| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- package com.example.logisticsclient;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothManager;
- import android.bluetooth.BluetoothSocket;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.os.Build;
- import android.os.Bundle;
- import HPRTAndroidSDKA300.HPRTPrinterHelper;
- import io.flutter.app.FlutterActivity;
- import io.flutter.plugin.common.EventChannel;
- import io.flutter.plugins.GeneratedPluginRegistrant;
- import android.content.ContextWrapper;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.util.Log;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.UUID;
- import io.flutter.plugin.common.MethodCall;
- import io.flutter.plugin.common.MethodChannel;
- import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
- import io.flutter.plugin.common.MethodChannel.Result;
- public class MainActivity extends FlutterActivity {
- private static final String CHANNEL = "io.fivefire/method_channel";
- private static final String EVENT_CHANNEL = "io.fivefire/event_channel";
- private BluetoothAdapter mBluetoothAdapter;
- private BluetoothManager mBluetoothManager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- GeneratedPluginRegistrant.registerWith(this);
- new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
- new MethodCallHandler() {
- @Override
- public void onMethodCall(MethodCall call, Result result) {
- if (call.method.equals("isBTDevice")) {
- boolean isBTDevice = isBTDevice();
- if (isBTDevice) {
- result.success(isBTDevice);
- } else {
- result.error("UNAVAILABLE", "没有发现蓝牙模块", null);
- }
- } else if (call.method.equals("isEnableBluetooth")) {
- boolean isEnableBluetooth = isEnableBluetooth();
- if (isEnableBluetooth) {
- result.success(isEnableBluetooth);
- } else {
- result.error("UNAVAILABLE", "蓝牙未打开", null);
- }
- } else if (call.method.equals("scanBT")) {
- boolean isScan = scanBT();
- if (isScan) {
- result.success(isScan);
- } else {
- result.error("UNAVAILABLE", "蓝牙扫描出错", null);
- }
- } else if (call.method.equals("connectBT")) {
- boolean isConn = connnectBT((String) call.argument("address"));
- if (isConn) {
- result.success(isConn);
- } else {
- result.error("UNAVAILABLE", "连接蓝牙出错", null);
- }
- } else if (call.method.equals("disConnectBT")) {
- boolean isConn = disConnnectBT();
- if (isConn) {
- result.success(isConn);
- } else {
- result.error("UNAVAILABLE", "关闭蓝牙出错", null);
- }
- } else if (call.method.equals("printOrder")) {
- boolean isPrint = printOrder((Map < String, String > ) call.argument("pum"), (Integer) call.argument("type"));
- if (isPrint) {
- result.success(isPrint);
- } else {
- result.error("UNAVAILABLE", "打印出错", null);
- }
- } else {
- result.notImplemented();
- }
- }
- });
- new EventChannel(getFlutterView(), EVENT_CHANNEL).setStreamHandler(
- new EventChannel.StreamHandler() {
- private BroadcastReceiver scanBlueReceiver;
- @Override
- public void onListen(Object o, EventChannel.EventSink eventSink) {
- scanBlueReceiver = createScanBlueReceiver(eventSink);
- IntentFilter filter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
- IntentFilter filter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
- IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- registerReceiver(scanBlueReceiver, filter1);
- registerReceiver(scanBlueReceiver, filter2);
- registerReceiver(scanBlueReceiver, filter3);
- registerReceiver(scanBlueReceiver, new IntentFilter(Intent.ACTION_DATE_CHANGED));
- System.out.println("**********************************BT EEEEESCAN*******************************");
- }
- @Override
- public void onCancel(Object o) {
- unregisterReceiver(scanBlueReceiver);
- scanBlueReceiver = null;
- }
- }
- );
- }
- /**
- * 判断当前设备蓝牙模块是否可用
- *
- * @return
- */
- private boolean isBTDevice() {
- boolean flag = true;
- try {
- mBluetoothManager = (BluetoothManager) getSystemService(ContextWrapper.BLUETOOTH_SERVICE);
- mBluetoothAdapter = mBluetoothManager.getAdapter();
- if (mBluetoothAdapter == null) {
- flag = false;
- System.out.println("设备未发现蓝牙设备!");
- }
- } catch (Exception e) {
- System.out.println("判断蓝牙模块异常" + e.getMessage());
- }
- return flag;
- }
- /**
- * 判断师傅打开蓝牙
- *
- * @return
- */
- private boolean isEnableBluetooth() {
- System.out.println("**********************************BT*******************************");
- mBluetoothAdapter.enable();
- return mBluetoothAdapter.isEnabled();
- }
- /**
- * 扫描获取蓝牙设备列表
- *
- * @return
- */
- private boolean scanBT() {
- boolean flag = false;
- System.out.println("**********************************BT SCAN*******************************");
- try {
- if (mBluetoothAdapter.isDiscovering()) {
- mBluetoothAdapter.cancelDiscovery();
- }
- System.out.println("**********************************BT1111 SCAN*******************************");
- mBluetoothAdapter.startDiscovery();
- flag = true;
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return flag;
- }
- private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- private BluetoothSocket socket;
- /**
- * 扫描广播接收类
- * Created by zqf on 2018/7/6.
- */
- private BroadcastReceiver createScanBlueReceiver(final EventChannel.EventSink eventSink) {
- return new BroadcastReceiver() {
- final String TAG = "RCV";
- //广播接收器,当远程蓝牙设备被发现时,回调函数onReceiver()会被执行
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- Log.d(TAG, "action:" + action);
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- switch (action) {
- case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
- eventSink.success("开始扫描...");
- break;
- case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
- eventSink.success("结束扫描..");
- break;
- case BluetoothDevice.ACTION_FOUND:
- if (device.getName() != null) {
- eventSink.success(device.getName() + "||" + device.getAddress());
- }
- if ("HM-A320-2920".equals(device.getName())) {
- try {
- // int sdk = Build.VERSION.SDK_INT;
- // if (sdk >= 10) {
- // socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
- // } else {
- // socket = device.createRfcommSocketToServiceRecord(SPP_UUID);
- // }
- //
- // System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
- // int i = HPRTPrinterHelper.PortOpen("Bluetooth,"+device.getAddress());
- // System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+i);
- connnectBT(device.getAddress());
- // ZTexpress();
- Map < String, String > tmap = new HashMap < > ();
- // printOrder(tmap,1);
- } catch (Exception e) {
- System.out.println("sss" + e.getMessage());
- e.printStackTrace();
- }
- }
- break;
- }
- }
- };
- }
- /**
- * 关闭蓝牙
- * @return
- */
- private boolean disConnnectBT() {
- boolean flag = false;
- try {
- flag = HPRTPrinterHelper.PortClose();
- } catch (Exception e) {
- }
- return flag;
- }
- private boolean connnectBT(final String address) {
- boolean flag = false;
- final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
- try {
- int sdk = Build.VERSION.SDK_INT;
- if (sdk >= 10) {
- socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
- } else {
- socket = device.createRfcommSocketToServiceRecord(SPP_UUID);
- }
- int i = HPRTPrinterHelper.PortOpen("Bluetooth," + device.getAddress());
- System.out.println("连接状态" + i);
- flag = true;
- } catch (Exception e) {
- System.out.println("sss" + e.getMessage());
- e.printStackTrace();
- }
- return flag;
- }
- private byte[] InputStreamToByte(InputStream is) throws IOException {
- ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
- int ch;
- while ((ch = is.read()) != -1) {
- bytestream.write(ch);
- }
- byte imgdata[] = bytestream.toByteArray();
- bytestream.close();
- return imgdata;
- }
- /**
- * 打印
- * @param pum 打印内容
- * @param type 打印类型 1:全部 2:配件商:3:修理厂 4:司机 5:货物 6:货物外
- */
- private boolean printOrder(Map < String, String > pum, int type) {
- if (pum == null) {
- pum = new HashMap < > ();
- }
- boolean flag = true;
- try {
- switch (type) {
- case 1:
- printSender(pum);
- printGoods(pum);
- printSender(pum);
- printDriver(pum);
- break;
- case 2:
- printSender(pum);
- break;
- case 3:
- printSender(pum);
- break;
- case 4:
- printDriver(pum);
- break;
- case 5:
- printGoods(pum);
- break;
- case 6:
- printSender(pum);
- printSender(pum);
- printDriver(pum);
- break;
- default:
- System.out.println("参数不正确");
- break;
- }
- } catch (Exception e) {
- Log.e("配件商或修理厂打印异常", (new StringBuilder("Activity_Main --> PrintSampleReceipt ")).append(e.getMessage()).toString());
- flag = false;
- }
- return flag;
- }
- private void printSender(Map < String, String > pum) throws Exception {
- // pum.put("[logisticsNo]", "1111232321321");
- // pum.put("[createTime]", "2019");
- // pum.put("[sendStationName]", "五方");
- // pum.put("[dealerCode]", "WF1112");
- // pum.put("[dealerName]", "五方配件商");
- // pum.put("[dealerTel]", "13439395969");
- // pum.put("[repairName]", "NB修理厂");
- // pum.put("[repairTel]", "13826514987");
- // pum.put("[endStationName]", "丰台");
- // pum.put("[goodsLineCode]", "F12");
- // pum.put("[goodsLine]", "马驹桥");
- // pum.put("[repairAddress]", "NB修车厂地址)");
- // pum.put("[goodsQuantity]", "1");
- // pum.put("[goodsInsurance]", "12");
- // pum.put("[goodsFlatCost]", "14");
- // pum.put("[goodsPaymentMethod]", "200");
- // pum.put("[goodsAgencyFund]", "18");
- // pum.put("[dealerBankAccount]", "1234512312YH");
- // pum.put("[goodsRemark]", "帽子");
- // pum.put("[realName]", "xg");
- // pum.put("[currentTime]", "21111yyymmm");
- Set < String > keySet = pum.keySet();
- Iterator < String > iterator = keySet.iterator();
- InputStream afis = this.getResources().getAssets().open("sender.txt"); //打印模版放在assets文件夹里
- String path = new String(InputStreamToByte(afis), "utf-8"); //打印模版以utf-8无bom格式保存
- while (iterator.hasNext()) {
- String string = (String) iterator.next();
- path = path.replace(string, pum.get(string));
- }
- HPRTPrinterHelper.printText(path);
- HPRTPrinterHelper.Form();
- HPRTPrinterHelper.Print();
- }
- private void printGoods(Map < String, String > pum) throws Exception {
- // pum.put("[logisticsNo]", "1111232321321");
- // pum.put("[createTime]", "2019");
- // pum.put("[sendStationName]", "五方");
- // pum.put("[dealerCode]", "WF1112");
- // pum.put("[dealerName]", "五方配件商");
- // pum.put("[dealerTel]", "13439395969");
- // pum.put("[repairName]", "NB修理厂");
- // pum.put("[repairTel]", "13826514987");
- // pum.put("[endStationName]", "丰台");
- // pum.put("[goodsLineCode]", "F12");
- // pum.put("[goodsLine]", "马驹桥");
- // pum.put("[repairAddress]", "NB修车厂地址)");
- // pum.put("[goodsQuantity]", "1");
- // pum.put("[goodsInsurance]", "12");
- // pum.put("[goodsFlatCost]", "14");
- // pum.put("[goodsPaymentMethod]", "200");
- // pum.put("[goodsAgencyFund]", "18");
- // pum.put("[dealerBankAccount]", "1234512312YH");
- // pum.put("[goodsRemark]", "帽子");
- // pum.put("[realName]", "xg");
- // pum.put("[currentTime]", "21111yyymmm");
- Set < String > keySet = pum.keySet();
- Iterator < String > iterator = keySet.iterator();
- InputStream afis = this.getResources().getAssets().open("goods.txt"); //打印模版放在assets文件夹里
- String path = new String(InputStreamToByte(afis), "utf-8"); //打印模版以utf-8无bom格式保存
- while (iterator.hasNext()) {
- String string = (String) iterator.next();
- path = path.replace(string, pum.get(string));
- }
- HPRTPrinterHelper.printText(path);
- HPRTPrinterHelper.Form();
- HPRTPrinterHelper.Print();
- }
- private void printDriver(Map < String, String > pum) throws Exception {
- // pum.put("[logisticsNo]", "1111232321321");
- // pum.put("[createTime]", "2019");
- // pum.put("[sendStationName]", "五方");
- // pum.put("[dealerCode]", "WF1112");
- // pum.put("[dealerName]", "五方配件商");
- // pum.put("[dealerTel]", "13439395969");
- // pum.put("[repairName]", "NB修理厂");
- // pum.put("[repairTel]", "13826514987");
- // pum.put("[endStationName]", "丰台");
- // pum.put("[goodsLineCode]", "F12");
- // pum.put("[goodsLine]", "马驹桥");
- // pum.put("[repairAddress]", "NB修车厂地址)");
- // pum.put("[goodsQuantity]", "1");
- // pum.put("[goodsInsurance]", "12");
- // pum.put("[goodsFlatCost]", "14");
- // pum.put("[goodsPaymentMethod]", "200");
- // pum.put("[goodsAgencyFund]", "18");
- // pum.put("[dealerBankAccount]", "1234512312YH");
- // pum.put("[goodsRemark]", "帽子");
- // pum.put("[realName]", "xg");
- // pum.put("[currentTime]", "21111yyymmm");
- Set < String > keySet = pum.keySet();
- Iterator < String > iterator = keySet.iterator();
- InputStream afis = this.getResources().getAssets().open("driver.txt"); //打印模版放在assets文件夹里
- String path = new String(InputStreamToByte(afis), "utf-8"); //打印模版以utf-8无bom格式保存
- while (iterator.hasNext()) {
- String string = (String) iterator.next();
- path = path.replace(string, pum.get(string));
- }
- HPRTPrinterHelper.printText(path);
- HPRTPrinterHelper.Form();
- HPRTPrinterHelper.Print();
- }
- private void ZTexpress() {
- try {
- Map < String, String > pum = new HashMap < String, String > ();
- pum.put("[logisticsNo]", "1111232321321");
- pum.put("[createTime]", "2019");
- pum.put("[sendStationName]", "五方");
- pum.put("[dealerCode]", "WF1112");
- pum.put("[dealerName]", "五方配件商");
- pum.put("[dealerTel]", "13439395969");
- pum.put("[repairName]", "NB修理厂");
- pum.put("[repairTel]", "13826514987");
- pum.put("[endStationName]", "丰台");
- pum.put("[goodsLineCode]", "F12");
- pum.put("[goodsLine]", "马驹桥");
- pum.put("[repairAddress]", "NB修车厂地址)");
- pum.put("[goodsQuantity]", "1");
- pum.put("[goodsInsurance]", "12");
- pum.put("[goodsFlatCost]", "14");
- pum.put("[goodsPaymentMethod]", "200");
- pum.put("[goodsAgencyFund]", "18");
- pum.put("[dealerBankAccount]", "1234512312YH");
- pum.put("[goodsRemark]", "帽子");
- pum.put("[realName]", "xg");
- pum.put("[currentTime]", "21111yyymmm");
- Set < String > keySet = pum.keySet();
- Iterator < String > iterator = keySet.iterator();
- InputStream afis = this.getResources().getAssets().open("goods.txt"); //打印模版放在assets文件夹里
- String path = new String(InputStreamToByte(afis), "utf-8"); //打印模版以utf-8无bom格式保存
- while (iterator.hasNext()) {
- String string = (String) iterator.next();
- path = path.replace(string, pum.get(string));
- }
- HPRTPrinterHelper.printText(path);
- HPRTPrinterHelper.Form();
- HPRTPrinterHelper.Print();
- } catch (Exception e) {
- Log.e("打印异常", (new StringBuilder("Activity_Main --> PrintSampleReceipt ")).append(e.getMessage()).toString());
- }
- }
- }
|