Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added printRawBytes methods #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/** BluetoothPrintPlugin */
public class BluetoothPrintPlugin implements FlutterPlugin, ActivityAware, MethodCallHandler, RequestPermissionsResultListener {
Expand Down Expand Up @@ -200,6 +201,10 @@ public void onMethodCall(MethodCall call, Result result) {
case "destroy":
result.success(destroy());
break;
case "rawBytes":
Log.i("NATIVE","rawBytes");
printRawBytes(result,args);
break;
case "print":
print(result, args);
break;
Expand Down Expand Up @@ -424,6 +429,40 @@ public void run() {

}

private void printRawBytes(Result result, Map<String, Object> args) {
if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] == null ||
!DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getConnState()) {

result.error("not connect", "state not right", null);
}

if (args.containsKey("config") && args.containsKey("data")) {
final Map<String,Object> config = (Map<String,Object>)args.get("config");
// final List<Map<String,Object>> list = (List<Map<String,Object>>)args.get("data");
final byte [] bytes = (byte [])args.get("data");
if(bytes == null){
return;
}

threadPool = ThreadPool.getInstantiation();
threadPool.addSerialTask(new Runnable() {
@Override
public void run() {
if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getCurrentPrinterCommand() == PrinterCommand.ESC) {
DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].sendByteDataImmediately(bytes);
}else if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getCurrentPrinterCommand() == PrinterCommand.TSC) {
DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].sendByteDataImmediately(bytes);
}else if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getCurrentPrinterCommand() == PrinterCommand.CPCL) {
DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].sendByteDataImmediately(bytes);
}
}
});
}else{
result.error("please add config or data", "", null);
}

}

@Override
public boolean onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

Expand Down
22 changes: 16 additions & 6 deletions ios/Classes/BluetoothPrintPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,17 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
} @catch(FlutterError *e) {
result(e);
}
} else if([@"printLabel" isEqualToString:call.method]) {
} else if([@"rawBytes" isEqualToString:call.method]) {
@try {
NSDictionary *args = [call arguments];
NSDictionary *config = [args objectForKey:@"config"];
FlutterStandardTypedData *list = [args objectForKey:@"data"];
[Manager write:list.data];
result(nil);
} @catch(FlutterError *e) {
result(e);
}
} else if([@"printLabel" isEqualToString:call.method]) {
@try {
NSDictionary *args = [call arguments];
[Manager write:[self mapToTscCommand:args]];
Expand Down Expand Up @@ -250,23 +260,23 @@ -(void)updateConnectState:(ConnectState)state {
NSNumber *ret = @0;
switch (state) {
case CONNECT_STATE_CONNECTING:
NSLog(@"status -> %@", @"连接状态:连接中....");
NSLog(@"status -> %@", @"Connection status: Connecting....");
ret = @0;
break;
case CONNECT_STATE_CONNECTED:
NSLog(@"status -> %@", @"连接状态:连接成功");
NSLog(@"status -> %@", @"Connection status: successfully connected");
ret = @1;
break;
case CONNECT_STATE_FAILT:
NSLog(@"status -> %@", @"连接状态:连接失败");
NSLog(@"status -> %@", @"Connection status: connection failed");
ret = @0;
break;
case CONNECT_STATE_DISCONNECT:
NSLog(@"status -> %@", @"连接状态:断开连接");
NSLog(@"status -> %@", @"Connection status: disconnected");
ret = @0;
break;
default:
NSLog(@"status -> %@", @"连接状态:连接超时");
NSLog(@"status -> %@", @"Connection status: connection timed out");
ret = @0;
break;
}
Expand Down
21 changes: 15 additions & 6 deletions lib/bluetooth_print.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:rxdart/rxdart.dart';
Expand All @@ -20,10 +21,9 @@ class BluetoothPrint {
StreamController.broadcast();

BluetoothPrint._() {
_channel.setMethodCallHandler((MethodCall call) {
_channel.setMethodCallHandler((MethodCall call) async {
_methodStreamController.add(call);
return;
} as Future<dynamic> Function(MethodCall)?);
});
}

static BluetoothPrint _instance = new BluetoothPrint._();
Expand All @@ -44,15 +44,15 @@ class BluetoothPrint {
Stream<bool> get isScanning => _isScanning.stream;

BehaviorSubject<List<BluetoothDevice>> _scanResults =
BehaviorSubject.seeded([]);
BehaviorSubject.seeded(<BluetoothDevice>[]);

Stream<List<BluetoothDevice>> get scanResults => _scanResults.stream;

PublishSubject _stopScanPill = new PublishSubject();

/// Gets the current state of the Bluetooth module
Stream<int> get state async* {
yield await _channel.invokeMethod('state').then((s) => s);
yield (await _channel.invokeMethod('state') ?? -1);

yield* _stateChannel.receiveBroadcastStream().map((s) => s);
}
Expand Down Expand Up @@ -94,7 +94,8 @@ class BluetoothPrint {
.doOnDone(stopScan)
.map((map) {
final device = BluetoothDevice.fromJson(Map<String, dynamic>.from(map));
final List<BluetoothDevice> list = _scanResults.value;
final List<BluetoothDevice> list =
_scanResults.value ?? <BluetoothDevice>[];
int newIndex = -1;
list.asMap().forEach((index, e) {
if (e.address == device.address) {
Expand Down Expand Up @@ -131,6 +132,14 @@ class BluetoothPrint {

Future<dynamic> disconnect() => _channel.invokeMethod('disconnect');

Future<dynamic> rawBytes(Map<String, dynamic> config, List<int> data) {
Map<String, Object> args = Map();
args['config'] = config;
args['data'] = Uint8List.fromList(data);
_channel.invokeMethod('rawBytes', args);
return Future.value(true);
}

Future<dynamic> destroy() => _channel.invokeMethod('destroy');

Future<dynamic> printReceipt(
Expand Down