|
@@ -10,6 +10,8 @@ import android.content.Intent;
|
10
|
10
|
import android.content.IntentFilter;
|
11
|
11
|
import android.content.SharedPreferences;
|
12
|
12
|
import android.content.pm.PackageManager;
|
|
13
|
+import android.graphics.Bitmap;
|
|
14
|
+import android.graphics.BitmapFactory;
|
13
|
15
|
import android.location.LocationManager;
|
14
|
16
|
import android.os.Build;
|
15
|
17
|
import android.text.TextUtils;
|
|
@@ -21,10 +23,13 @@ import androidx.annotation.NonNull;
|
21
|
23
|
import androidx.core.app.ActivityCompat;
|
22
|
24
|
import androidx.fragment.app.FragmentActivity;
|
23
|
25
|
|
|
26
|
+import com.gengcon.www.jcprintersdk.callback.PrintCallback;
|
24
|
27
|
import com.permissionx.guolindev.PermissionX;
|
25
|
28
|
import com.szls.lszlgl.MainActivity;
|
26
|
29
|
|
27
|
30
|
import java.security.Permission;
|
|
31
|
+import java.util.ArrayList;
|
|
32
|
+import java.util.HashMap;
|
28
|
33
|
import java.util.List;
|
29
|
34
|
import java.util.concurrent.ExecutorService;
|
30
|
35
|
import java.util.concurrent.LinkedBlockingDeque;
|
|
@@ -154,6 +159,17 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
154
|
159
|
Log.d("ble", "endBluetoothConnect :"+methodCall.arguments);
|
155
|
160
|
endBluetoothConnect(methodCall, result);
|
156
|
161
|
break;
|
|
162
|
+ case "hasBluetoothConnectDevice":
|
|
163
|
+ Log.d("ble", "hasBluetoothConnectDevice :"+methodCall.arguments);
|
|
164
|
+ hasBluetoothConnectDevice(methodCall, result);
|
|
165
|
+ break;
|
|
166
|
+ case "startBluetoothPrint":
|
|
167
|
+ Log.d("ble", "startBluetoothPrint :"+methodCall.arguments);
|
|
168
|
+ startBluetoothPrint(methodCall, result);
|
|
169
|
+ break;
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
157
|
173
|
|
158
|
174
|
|
159
|
175
|
|
|
@@ -280,6 +296,125 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
280
|
296
|
result.success(0);
|
281
|
297
|
}
|
282
|
298
|
|
|
299
|
+ void hasBluetoothConnectDevice(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
|
|
300
|
+ if (PrintUtil.isConnection() == 0 && PrintUtil.getConnectedType()==0) {
|
|
301
|
+ Log.d("ble", "测试:配对状态改变:判断连接状态 2" );
|
|
302
|
+ result.success(true);
|
|
303
|
+ }else {
|
|
304
|
+ Log.d("ble", "测试:配对状态改变:判断连接状态4 " );
|
|
305
|
+ result.success(false);
|
|
306
|
+ }
|
|
307
|
+ }
|
|
308
|
+
|
|
309
|
+ void startBluetoothPrint(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
|
|
310
|
+
|
|
311
|
+ byte[] imageByte = (byte[]) methodCall.arguments;
|
|
312
|
+
|
|
313
|
+ Log.d("ble", "imageByte:" + imageByte);
|
|
314
|
+ Bitmap bitmap = Bytes2Bimap(imageByte);
|
|
315
|
+
|
|
316
|
+ PrintUtil.getInstance().drawEmptyLabel(40, 60, 0, "");
|
|
317
|
+ if (PrintUtil.isConnection() != 0) {
|
|
318
|
+// handler.post(() -> Toast.makeText(MyApplication.getInstance(), "未连接打印机", Toast.LENGTH_SHORT).show());
|
|
319
|
+ return;
|
|
320
|
+ }
|
|
321
|
+
|
|
322
|
+// fragment = new MyDialogLoadingFragment("打印中");
|
|
323
|
+// fragment.show(getSupportFragmentManager(), "PRINT");
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+ //重置错误状态变量
|
|
327
|
+ boolean isError = false;
|
|
328
|
+ //重置取消打印状态变量
|
|
329
|
+ boolean isCancel = false;
|
|
330
|
+
|
|
331
|
+ int orientation = 0;
|
|
332
|
+
|
|
333
|
+ int pageCount = 1;
|
|
334
|
+ int quantity = 1;
|
|
335
|
+ int printMultiple = 8;
|
|
336
|
+ final int[] generatedPrintDataPageCount = {0};
|
|
337
|
+ int totalQuantity = pageCount * quantity;
|
|
338
|
+
|
|
339
|
+ //setTotalQuantityOfPrints已废弃,使用方法含义更明确的setTotalPrintQuantity
|
|
340
|
+ PrintUtil.getInstance().setTotalPrintQuantity(totalQuantity);
|
|
341
|
+ /*
|
|
342
|
+ * 参数1:打印浓度 ,参数2:纸张类型 参数3:打印模式
|
|
343
|
+ * 打印浓度 B50/B50W/T6/T7/T8 建议设置6或8,Z401/B32建议设置8,B3S/B21/B203/B1建议设置3
|
|
344
|
+ */
|
|
345
|
+ PrintUtil.getInstance().startPrintJob(3, 3, 1, new PrintCallback() {
|
|
346
|
+ @Override
|
|
347
|
+ public void onProgress(int pageIndex, int quantityIndex, HashMap<String, Object> hashMap) {
|
|
348
|
+// handler.post(() -> fragment.setStateStr("打印进度:已打印到第" + pageIndex + "页,第" + quantityIndex + "份"));
|
|
349
|
+ Log.d("ble", "测试:打印进度:已打印到第: " + pageIndex);
|
|
350
|
+ //打印进度回调
|
|
351
|
+ if (pageIndex == pageCount && quantityIndex == quantity) {
|
|
352
|
+ Log.d("ble", "测试:onProgress: 结束打印");
|
|
353
|
+ //endJob已废弃,使用方法含义更明确的endPrintJob
|
|
354
|
+ if (PrintUtil.getInstance().endPrintJob()) {
|
|
355
|
+ Log.d("ble", "结束打印成功");
|
|
356
|
+ } else {
|
|
357
|
+ Log.d("ble", "结束打印失败");
|
|
358
|
+ }
|
|
359
|
+// handlePrintResult(fragment, "打印成功");
|
|
360
|
+ }
|
|
361
|
+ }
|
|
362
|
+
|
|
363
|
+ @Override
|
|
364
|
+ public void onError(int i) {
|
|
365
|
+
|
|
366
|
+ }
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+ @Override
|
|
370
|
+ public void onError(int errorCode, int printState) {
|
|
371
|
+ Log.d("ble", "测试:onError");
|
|
372
|
+// isError = true;
|
|
373
|
+// String errorMsg = ERROR_MESSAGES.getOrDefault(errorCode, "");
|
|
374
|
+// handlePrintResult(fragment, errorMsg);
|
|
375
|
+ }
|
|
376
|
+
|
|
377
|
+ @Override
|
|
378
|
+ public void onCancelJob(boolean isSuccess) {
|
|
379
|
+ //取消打印成功回调
|
|
380
|
+// isCancel = true;
|
|
381
|
+ }
|
|
382
|
+
|
|
383
|
+ @Override
|
|
384
|
+ public void onBufferFree(int pageIndex, int bufferSize) {
|
|
385
|
+ if (isError || isCancel || pageIndex > pageCount) {
|
|
386
|
+ return;
|
|
387
|
+ }
|
|
388
|
+
|
|
389
|
+ if (generatedPrintDataPageCount[0] < pageCount) {
|
|
390
|
+// ArrayList<Dish> dishList = new ArrayList<>();
|
|
391
|
+// dishList.add(new Dish("辣椒炒肉", "中辣", 29.9, 1));
|
|
392
|
+// dishList.add(new Dish("土豆牛腩", "中辣", 49.9, 1));
|
|
393
|
+//
|
|
394
|
+// Bitmap bitmap = ImgUtil.Companion.generatePosReceiptImage(dishList);
|
|
395
|
+
|
|
396
|
+ int bitmapWidth = bitmap.getWidth();
|
|
397
|
+ int bitmapHeight = bitmap.getHeight();
|
|
398
|
+ Log.d("ble", "bitmapWidth:"+bitmapWidth+",bitmapHeight:"+bitmapHeight);
|
|
399
|
+ PrintUtil.getInstance().commitImageData(orientation, bitmap, (int) (bitmapWidth / printMultiple), (int) (bitmapHeight / printMultiple), 1, 0, 0, 0, 0, "");
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+ }
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+ }
|
|
406
|
+ });
|
|
407
|
+
|
|
408
|
+ }
|
|
409
|
+
|
|
410
|
+ public Bitmap Bytes2Bimap(byte[] b) {
|
|
411
|
+ if (b.length != 0) {
|
|
412
|
+ return BitmapFactory.decodeByteArray(b, 0, b.length);
|
|
413
|
+ } else {
|
|
414
|
+ return null;
|
|
415
|
+ }
|
|
416
|
+ }
|
|
417
|
+
|
283
|
418
|
private final BroadcastReceiver receiver = new BroadcastReceiver() {
|
284
|
419
|
|
285
|
420
|
@SuppressLint("MissingPermission")
|