writeBytes method

  1. @override
Future<ConnectionResponse> writeBytes(
  1. List<int> data, {
  2. bool isDisconnect = true,
  3. Duration? timeout = const Duration(milliseconds: 20),
})
override

writeBytes let you write raw list int data into socket

Implementation

@override
Future<ConnectionResponse> writeBytes(List<int> data,
    {bool isDisconnect = true,
    Duration? timeout = const Duration(milliseconds: 20)}) async {
  try {
    if (!isConnected) {
      await connect();
    }
    if (Platform.isAndroid || Platform.isIOS) {
      if ((await bluetooth.isConnected) ?? false) {
        if (timeout != null) {
          await Future.delayed(timeout, () => null);
        }
        Uint8List message = Uint8List.fromList(data);
        await bluetooth.writeBytes(message);
        if (isDisconnect) {
          await disconnect();
        }
        return ConnectionResponse.success;
      }
      return ConnectionResponse.printerNotConnected;
    }
    //  else if (Platform.isIOS) {
    //   // var services = (await fbdevice.discoverServices());
    //   // var service = services.firstWhere((e) => e.isPrimary);
    //   // var charactor =
    //   //     service.characteristics.firstWhere((e) => e.properties.write);
    //   // await charactor?.write(data, withoutResponse: true);
    //   return ConnectionResponse.success;
    // }
    return ConnectionResponse.unsupport;
  } catch (e) {
    print("Error : $e");
    return ConnectionResponse.unknown;
  }
}