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

打印安卓二维码浓度低,无法使用和连接异常和断开连接异常的问题 #45

Closed
DeanNode opened this issue Jan 27, 2021 · 2 comments

Comments

@DeanNode
Copy link

DeanNode commented Jan 27, 2021

我的蓝牙打印机在打印QRCODE(二维码)中安卓打印的清晰度太低,导致无法正常使用,后来在printContext中把打印浓度的枚举类从7(好像是7)改成了15,之后可以安卓打印二维码就可以非常清晰了。但是现在存在以下问题:
1:无法判断当前蓝牙设备是否已经正常连接,判断结果往往是错的bluetoothPrint.isConnected和bluetoothPrint.state.listen监听的结果汪汪是错的。
2:bluetoothPrint.connect(device)方法中,往往device不是null反而会出现NullException。
3:bluetoothPrint.disconnect()这个方法经常使用时会导致应用直接崩溃掉(闪退)
`import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:guards/bean/BlueJurisdiction.dart';
import 'package:guards/utils/interface/BluetoothFactory.dart';

/// @description BluetoothFactoryImpl
/// @author Activation
/// @Date 2021/1/26 11:14
class BluetoothFactoryImpl implements BluetoothFactory {
BluetoothPrint bluetoothPrint = BluetoothPrint.instance;
bool isConnFlag = false;

@OverRide
Future disconnect() async {
await bluetoothPrint.stopScan();
await bluetoothPrint.disconnect();
await bluetoothPrint.destroy();
}

@OverRide
Future checkBluetoothAccess() async {
bool openFlag = await bluetoothPrint.isOn;

if (!openFlag) {
  //DialogUtil().showMsg(context, "请您先开启蓝牙");
  return BlueJurisdiction(false, "请您先开启蓝牙");
}
bool authorityFlag = await bluetoothPrint.isAvailable;
if (!authorityFlag) {
  //DialogUtil().showMsg(context, "请您先授予蓝牙的使用权限");
  return BlueJurisdiction(false, "请您先授予蓝牙的使用权限");
}
return BlueJurisdiction(true, "可以连接蓝牙");
//bluetoothPrint.isConnected.then((value) => print(value));

}

@OverRide
Future joinBluetooth() async {
EasyLoading.show(status: "连接蓝牙中。。");
try {
await bluetoothPrint.startScan(timeout: Duration(seconds: 4));
Stream<List> tempStream = bluetoothPrint.scanResults;
print(tempStream);
bool tempFlag = false;
//执行自动连接
tempStream.listen((event) async {
for (BluetoothDevice device in event) {
if (device.name.startsWith(r"Printer_")) {
tempFlag = true;
if (tempFlag && device != null) {
// await bluetoothPrint.disconnect();
print("这里是连接的设备信息$device");
await bluetoothPrint.connect(device);
await bluetoothPrint.stopScan();
}
}
}
});
await bluetoothPrint.isConnected.then((value) {
print("这里是对应的当前值是否已经连接的返回值$value");
});
bluetoothPrint.state.listen((state) {
print("这里是正在监听值的变化$state");
switch (state) {
case BluetoothPrint.CONNECTED:
print("蓝牙已经连接了");
isConnFlag = true;
break;
case BluetoothPrint.DISCONNECTED:
isConnFlag = false;
print("蓝牙还没有连接");
break;
default:
break;
}
});
} catch (e) {
print("这里是对应的异常信息");
print("${e.runtimeType}${e.toString()}");
} finally {
EasyLoading.dismiss();
}
}

@OverRide
Future executePrint(List tempLists) async {
print("这里已经执行了$isConnFlag");
if (isConnFlag) {
await bluetoothPrint.printLabel(config, tempLists);
} else {
throw Exception("当前设备未连接,请关闭蓝牙之后再重试");
// await joinBluetooth().then((value) {
// bluetoothPrint.printLabel(config, tempLists);
// });
}
}

@OverRide
Map<String, dynamic> config = {'width': 300, 'height': 220, 'gap': 30};
}
`
然后我希望您能联系我,我可以协助您完善这个组件,因为我也需要使用这个组件,并且希望他可以成熟。
我的打印机型号为:GP-M322
我的QQ:1067252205
还有楼主的群号重新发下吧。。。。。 我也想进群交流讨论

@DeanNode DeanNode changed the title 关于蓝牙打印机打印和关闭蓝牙打印机的事情 打印安卓二维码浓度低,无法使用和连接异常和断开连接异常的问题 Jan 27, 2021
@DeanNode
Copy link
Author

DeanNode commented Feb 7, 2021

对于判断是否成功连接的代码补充。通过查看源码发现:如果根据PrinterReader中的 readDataImmediately(buffer) 如果返回结果值不为-1似乎就可以判断为已经连接蓝牙打印机设备。
`
class PrinterReader extends Thread {
private boolean isRun = false;

    private byte[] buffer = new byte[100];

    public PrinterReader() {
        isRun = true;
    }

    @Override
    public void run() {
        try {
            while (isRun) {
                //读取打印机返回信息,打印机没有返回纸返回-1
                Log.e(TAG,"wait read ");
                **int len = readDataImmediately(buffer);**
                Log.e(TAG," read "+len);
                if (len > 0) {
                    Message message = Message.obtain();
                    message.what = READ_DATA;
                    Bundle bundle = new Bundle();
                    bundle.putInt(READ_DATA_CNT, len); //数据长度
                    bundle.putByteArray(READ_BUFFER_ARRAY, buffer); //数据
                    message.setData(bundle);
                    mHandler.sendMessage(message);
                }
            }
        } catch (Exception e) {//异常断开
            if (deviceConnFactoryManagers[id] != null) {
                closePort(id);
                mHandler.obtainMessage(Constant.abnormal_Disconnection).sendToTarget();
            }
        }
    }

    public void cancel() {
        isRun = false;
    }
}

`

@thon-ju thon-ju pinned this issue May 12, 2021
@thon-ju
Copy link
Owner

thon-ju commented May 12, 2021

非常感谢,你可以推送下你的修改,我合并进去

@thon-ju thon-ju closed this as completed Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants