MainActivity.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. package com.example.logisticsclient;
  2. import android.bluetooth.BluetoothAdapter;
  3. import android.bluetooth.BluetoothDevice;
  4. import android.bluetooth.BluetoothManager;
  5. import android.bluetooth.BluetoothSocket;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import HPRTAndroidSDKA300.HPRTPrinterHelper;
  11. import io.flutter.app.FlutterActivity;
  12. import io.flutter.plugin.common.EventChannel;
  13. import io.flutter.plugins.GeneratedPluginRegistrant;
  14. import android.content.ContextWrapper;
  15. import android.content.Intent;
  16. import android.content.IntentFilter;
  17. import android.util.Log;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.UUID;
  26. import io.flutter.plugin.common.MethodCall;
  27. import io.flutter.plugin.common.MethodChannel;
  28. import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
  29. import io.flutter.plugin.common.MethodChannel.Result;
  30. public class MainActivity extends FlutterActivity {
  31. private static final String CHANNEL = "io.fivefire/method_channel";
  32. private static final String EVENT_CHANNEL = "io.fivefire/event_channel";
  33. private BluetoothAdapter mBluetoothAdapter;
  34. private BluetoothManager mBluetoothManager;
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. GeneratedPluginRegistrant.registerWith(this);
  39. new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
  40. new MethodCallHandler() {
  41. @Override
  42. public void onMethodCall(MethodCall call, Result result) {
  43. if (call.method.equals("isBTDevice")) {
  44. boolean isBTDevice = isBTDevice();
  45. if (isBTDevice) {
  46. result.success(isBTDevice);
  47. } else {
  48. result.error("UNAVAILABLE", "没有发现蓝牙模块", null);
  49. }
  50. } else if (call.method.equals("isEnableBluetooth")) {
  51. boolean isEnableBluetooth = isEnableBluetooth();
  52. if (isEnableBluetooth) {
  53. result.success(isEnableBluetooth);
  54. } else {
  55. result.error("UNAVAILABLE", "蓝牙未打开", null);
  56. }
  57. } else if (call.method.equals("scanBT")) {
  58. boolean isScan = scanBT();
  59. if (isScan) {
  60. result.success(isScan);
  61. } else {
  62. result.error("UNAVAILABLE", "蓝牙扫描出错", null);
  63. }
  64. } else if (call.method.equals("connectBT")) {
  65. boolean isConn = connnectBT((String) call.argument("address"));
  66. if (isConn) {
  67. result.success(isConn);
  68. } else {
  69. result.error("UNAVAILABLE", "连接蓝牙出错", null);
  70. }
  71. } else if (call.method.equals("disConnectBT")) {
  72. boolean isConn = disConnnectBT();
  73. if (isConn) {
  74. result.success(isConn);
  75. } else {
  76. result.error("UNAVAILABLE", "关闭蓝牙出错", null);
  77. }
  78. } else if (call.method.equals("printOrder")) {
  79. boolean isPrint = printOrder((Map < String, String > ) call.argument("pum"), (Integer) call.argument("type"));
  80. if (isPrint) {
  81. result.success(isPrint);
  82. } else {
  83. result.error("UNAVAILABLE", "打印出错", null);
  84. }
  85. } else {
  86. result.notImplemented();
  87. }
  88. }
  89. });
  90. new EventChannel(getFlutterView(), EVENT_CHANNEL).setStreamHandler(
  91. new EventChannel.StreamHandler() {
  92. private BroadcastReceiver scanBlueReceiver;
  93. @Override
  94. public void onListen(Object o, EventChannel.EventSink eventSink) {
  95. scanBlueReceiver = createScanBlueReceiver(eventSink);
  96. IntentFilter filter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
  97. IntentFilter filter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
  98. IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  99. registerReceiver(scanBlueReceiver, filter1);
  100. registerReceiver(scanBlueReceiver, filter2);
  101. registerReceiver(scanBlueReceiver, filter3);
  102. registerReceiver(scanBlueReceiver, new IntentFilter(Intent.ACTION_DATE_CHANGED));
  103. System.out.println("**********************************BT EEEEESCAN*******************************");
  104. }
  105. @Override
  106. public void onCancel(Object o) {
  107. unregisterReceiver(scanBlueReceiver);
  108. scanBlueReceiver = null;
  109. }
  110. }
  111. );
  112. }
  113. /**
  114. * 判断当前设备蓝牙模块是否可用
  115. *
  116. * @return
  117. */
  118. private boolean isBTDevice() {
  119. boolean flag = true;
  120. try {
  121. mBluetoothManager = (BluetoothManager) getSystemService(ContextWrapper.BLUETOOTH_SERVICE);
  122. mBluetoothAdapter = mBluetoothManager.getAdapter();
  123. if (mBluetoothAdapter == null) {
  124. flag = false;
  125. System.out.println("设备未发现蓝牙设备!");
  126. }
  127. } catch (Exception e) {
  128. System.out.println("判断蓝牙模块异常" + e.getMessage());
  129. }
  130. return flag;
  131. }
  132. /**
  133. * 判断师傅打开蓝牙
  134. *
  135. * @return
  136. */
  137. private boolean isEnableBluetooth() {
  138. System.out.println("**********************************BT*******************************");
  139. mBluetoothAdapter.enable();
  140. return mBluetoothAdapter.isEnabled();
  141. }
  142. /**
  143. * 扫描获取蓝牙设备列表
  144. *
  145. * @return
  146. */
  147. private boolean scanBT() {
  148. boolean flag = false;
  149. System.out.println("**********************************BT SCAN*******************************");
  150. try {
  151. if (mBluetoothAdapter.isDiscovering()) {
  152. mBluetoothAdapter.cancelDiscovery();
  153. }
  154. System.out.println("**********************************BT1111 SCAN*******************************");
  155. mBluetoothAdapter.startDiscovery();
  156. flag = true;
  157. } catch (Exception e) {
  158. System.out.println(e.getMessage());
  159. }
  160. return flag;
  161. }
  162. private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  163. private BluetoothSocket socket;
  164. /**
  165. * 扫描广播接收类
  166. * Created by zqf on 2018/7/6.
  167. */
  168. private BroadcastReceiver createScanBlueReceiver(final EventChannel.EventSink eventSink) {
  169. return new BroadcastReceiver() {
  170. final String TAG = "RCV";
  171. //广播接收器,当远程蓝牙设备被发现时,回调函数onReceiver()会被执行
  172. @Override
  173. public void onReceive(Context context, Intent intent) {
  174. String action = intent.getAction();
  175. Log.d(TAG, "action:" + action);
  176. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  177. switch (action) {
  178. case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
  179. eventSink.success("开始扫描...");
  180. break;
  181. case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
  182. eventSink.success("结束扫描..");
  183. break;
  184. case BluetoothDevice.ACTION_FOUND:
  185. if (device.getName() != null) {
  186. eventSink.success(device.getName() + "||" + device.getAddress());
  187. }
  188. if ("HM-A320-2920".equals(device.getName())) {
  189. try {
  190. // int sdk = Build.VERSION.SDK_INT;
  191. // if (sdk >= 10) {
  192. // socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
  193. // } else {
  194. // socket = device.createRfcommSocketToServiceRecord(SPP_UUID);
  195. // }
  196. //
  197. // System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  198. // int i = HPRTPrinterHelper.PortOpen("Bluetooth,"+device.getAddress());
  199. // System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+i);
  200. connnectBT(device.getAddress());
  201. // ZTexpress();
  202. Map < String, String > tmap = new HashMap < > ();
  203. // printOrder(tmap,1);
  204. } catch (Exception e) {
  205. System.out.println("sss" + e.getMessage());
  206. e.printStackTrace();
  207. }
  208. }
  209. break;
  210. }
  211. }
  212. };
  213. }
  214. /**
  215. * 关闭蓝牙
  216. * @return
  217. */
  218. private boolean disConnnectBT() {
  219. boolean flag = false;
  220. try {
  221. flag = HPRTPrinterHelper.PortClose();
  222. } catch (Exception e) {
  223. }
  224. return flag;
  225. }
  226. private boolean connnectBT(final String address) {
  227. boolean flag = false;
  228. final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
  229. try {
  230. int sdk = Build.VERSION.SDK_INT;
  231. if (sdk >= 10) {
  232. socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
  233. } else {
  234. socket = device.createRfcommSocketToServiceRecord(SPP_UUID);
  235. }
  236. int i = HPRTPrinterHelper.PortOpen("Bluetooth," + device.getAddress());
  237. System.out.println("连接状态" + i);
  238. flag = true;
  239. } catch (Exception e) {
  240. System.out.println("sss" + e.getMessage());
  241. e.printStackTrace();
  242. }
  243. return flag;
  244. }
  245. private byte[] InputStreamToByte(InputStream is) throws IOException {
  246. ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
  247. int ch;
  248. while ((ch = is.read()) != -1) {
  249. bytestream.write(ch);
  250. }
  251. byte imgdata[] = bytestream.toByteArray();
  252. bytestream.close();
  253. return imgdata;
  254. }
  255. /**
  256. * 打印
  257. * @param pum 打印内容
  258. * @param type 打印类型 1:全部 2:配件商:3:修理厂 4:司机 5:货物 6:货物外
  259. */
  260. private boolean printOrder(Map < String, String > pum, int type) {
  261. if (pum == null) {
  262. pum = new HashMap < > ();
  263. }
  264. boolean flag = true;
  265. try {
  266. switch (type) {
  267. case 1:
  268. printSender(pum);
  269. printGoods(pum);
  270. printSender(pum);
  271. printDriver(pum);
  272. break;
  273. case 2:
  274. printSender(pum);
  275. break;
  276. case 3:
  277. printSender(pum);
  278. break;
  279. case 4:
  280. printDriver(pum);
  281. break;
  282. case 5:
  283. printGoods(pum);
  284. break;
  285. case 6:
  286. printSender(pum);
  287. printSender(pum);
  288. printDriver(pum);
  289. break;
  290. default:
  291. System.out.println("参数不正确");
  292. break;
  293. }
  294. } catch (Exception e) {
  295. Log.e("配件商或修理厂打印异常", (new StringBuilder("Activity_Main --> PrintSampleReceipt ")).append(e.getMessage()).toString());
  296. flag = false;
  297. }
  298. return flag;
  299. }
  300. private void printSender(Map < String, String > pum) throws Exception {
  301. // pum.put("[logisticsNo]", "1111232321321");
  302. // pum.put("[createTime]", "2019");
  303. // pum.put("[sendStationName]", "五方");
  304. // pum.put("[dealerCode]", "WF1112");
  305. // pum.put("[dealerName]", "五方配件商");
  306. // pum.put("[dealerTel]", "13439395969");
  307. // pum.put("[repairName]", "NB修理厂");
  308. // pum.put("[repairTel]", "13826514987");
  309. // pum.put("[endStationName]", "丰台");
  310. // pum.put("[goodsLineCode]", "F12");
  311. // pum.put("[goodsLine]", "马驹桥");
  312. // pum.put("[repairAddress]", "NB修车厂地址)");
  313. // pum.put("[goodsQuantity]", "1");
  314. // pum.put("[goodsInsurance]", "12");
  315. // pum.put("[goodsFlatCost]", "14");
  316. // pum.put("[goodsPaymentMethod]", "200");
  317. // pum.put("[goodsAgencyFund]", "18");
  318. // pum.put("[dealerBankAccount]", "1234512312YH");
  319. // pum.put("[goodsRemark]", "帽子");
  320. // pum.put("[realName]", "xg");
  321. // pum.put("[currentTime]", "21111yyymmm");
  322. Set < String > keySet = pum.keySet();
  323. Iterator < String > iterator = keySet.iterator();
  324. InputStream afis = this.getResources().getAssets().open("sender.txt"); //打印模版放在assets文件夹里
  325. String path = new String(InputStreamToByte(afis), "utf-8"); //打印模版以utf-8无bom格式保存
  326. while (iterator.hasNext()) {
  327. String string = (String) iterator.next();
  328. path = path.replace(string, pum.get(string));
  329. }
  330. HPRTPrinterHelper.printText(path);
  331. HPRTPrinterHelper.Form();
  332. HPRTPrinterHelper.Print();
  333. }
  334. private void printGoods(Map < String, String > pum) throws Exception {
  335. // pum.put("[logisticsNo]", "1111232321321");
  336. // pum.put("[createTime]", "2019");
  337. // pum.put("[sendStationName]", "五方");
  338. // pum.put("[dealerCode]", "WF1112");
  339. // pum.put("[dealerName]", "五方配件商");
  340. // pum.put("[dealerTel]", "13439395969");
  341. // pum.put("[repairName]", "NB修理厂");
  342. // pum.put("[repairTel]", "13826514987");
  343. // pum.put("[endStationName]", "丰台");
  344. // pum.put("[goodsLineCode]", "F12");
  345. // pum.put("[goodsLine]", "马驹桥");
  346. // pum.put("[repairAddress]", "NB修车厂地址)");
  347. // pum.put("[goodsQuantity]", "1");
  348. // pum.put("[goodsInsurance]", "12");
  349. // pum.put("[goodsFlatCost]", "14");
  350. // pum.put("[goodsPaymentMethod]", "200");
  351. // pum.put("[goodsAgencyFund]", "18");
  352. // pum.put("[dealerBankAccount]", "1234512312YH");
  353. // pum.put("[goodsRemark]", "帽子");
  354. // pum.put("[realName]", "xg");
  355. // pum.put("[currentTime]", "21111yyymmm");
  356. Set < String > keySet = pum.keySet();
  357. Iterator < String > iterator = keySet.iterator();
  358. InputStream afis = this.getResources().getAssets().open("goods.txt"); //打印模版放在assets文件夹里
  359. String path = new String(InputStreamToByte(afis), "utf-8"); //打印模版以utf-8无bom格式保存
  360. while (iterator.hasNext()) {
  361. String string = (String) iterator.next();
  362. path = path.replace(string, pum.get(string));
  363. }
  364. HPRTPrinterHelper.printText(path);
  365. HPRTPrinterHelper.Form();
  366. HPRTPrinterHelper.Print();
  367. }
  368. private void printDriver(Map < String, String > pum) throws Exception {
  369. // pum.put("[logisticsNo]", "1111232321321");
  370. // pum.put("[createTime]", "2019");
  371. // pum.put("[sendStationName]", "五方");
  372. // pum.put("[dealerCode]", "WF1112");
  373. // pum.put("[dealerName]", "五方配件商");
  374. // pum.put("[dealerTel]", "13439395969");
  375. // pum.put("[repairName]", "NB修理厂");
  376. // pum.put("[repairTel]", "13826514987");
  377. // pum.put("[endStationName]", "丰台");
  378. // pum.put("[goodsLineCode]", "F12");
  379. // pum.put("[goodsLine]", "马驹桥");
  380. // pum.put("[repairAddress]", "NB修车厂地址)");
  381. // pum.put("[goodsQuantity]", "1");
  382. // pum.put("[goodsInsurance]", "12");
  383. // pum.put("[goodsFlatCost]", "14");
  384. // pum.put("[goodsPaymentMethod]", "200");
  385. // pum.put("[goodsAgencyFund]", "18");
  386. // pum.put("[dealerBankAccount]", "1234512312YH");
  387. // pum.put("[goodsRemark]", "帽子");
  388. // pum.put("[realName]", "xg");
  389. // pum.put("[currentTime]", "21111yyymmm");
  390. Set < String > keySet = pum.keySet();
  391. Iterator < String > iterator = keySet.iterator();
  392. InputStream afis = this.getResources().getAssets().open("driver.txt"); //打印模版放在assets文件夹里
  393. String path = new String(InputStreamToByte(afis), "utf-8"); //打印模版以utf-8无bom格式保存
  394. while (iterator.hasNext()) {
  395. String string = (String) iterator.next();
  396. path = path.replace(string, pum.get(string));
  397. }
  398. HPRTPrinterHelper.printText(path);
  399. HPRTPrinterHelper.Form();
  400. HPRTPrinterHelper.Print();
  401. }
  402. private void ZTexpress() {
  403. try {
  404. Map < String, String > pum = new HashMap < String, String > ();
  405. pum.put("[logisticsNo]", "1111232321321");
  406. pum.put("[createTime]", "2019");
  407. pum.put("[sendStationName]", "五方");
  408. pum.put("[dealerCode]", "WF1112");
  409. pum.put("[dealerName]", "五方配件商");
  410. pum.put("[dealerTel]", "13439395969");
  411. pum.put("[repairName]", "NB修理厂");
  412. pum.put("[repairTel]", "13826514987");
  413. pum.put("[endStationName]", "丰台");
  414. pum.put("[goodsLineCode]", "F12");
  415. pum.put("[goodsLine]", "马驹桥");
  416. pum.put("[repairAddress]", "NB修车厂地址)");
  417. pum.put("[goodsQuantity]", "1");
  418. pum.put("[goodsInsurance]", "12");
  419. pum.put("[goodsFlatCost]", "14");
  420. pum.put("[goodsPaymentMethod]", "200");
  421. pum.put("[goodsAgencyFund]", "18");
  422. pum.put("[dealerBankAccount]", "1234512312YH");
  423. pum.put("[goodsRemark]", "帽子");
  424. pum.put("[realName]", "xg");
  425. pum.put("[currentTime]", "21111yyymmm");
  426. Set < String > keySet = pum.keySet();
  427. Iterator < String > iterator = keySet.iterator();
  428. InputStream afis = this.getResources().getAssets().open("goods.txt"); //打印模版放在assets文件夹里
  429. String path = new String(InputStreamToByte(afis), "utf-8"); //打印模版以utf-8无bom格式保存
  430. while (iterator.hasNext()) {
  431. String string = (String) iterator.next();
  432. path = path.replace(string, pum.get(string));
  433. }
  434. HPRTPrinterHelper.printText(path);
  435. HPRTPrinterHelper.Form();
  436. HPRTPrinterHelper.Print();
  437. } catch (Exception e) {
  438. Log.e("打印异常", (new StringBuilder("Activity_Main --> PrintSampleReceipt ")).append(e.getMessage()).toString());
  439. }
  440. }
  441. }