flutter_blue added for ios
parent
7b06e4c390
commit
16ea7ead94
|
|
@ -1,43 +1,106 @@
|
|||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:aman_kassa_flutter/core/base/base_service.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
||||
|
||||
|
||||
import 'package:flutter_blue/flutter_blue.dart' as flutter_blue;
|
||||
import 'package:flutter_blue/gen/flutterblue.pb.dart' as proto;
|
||||
|
||||
class BluePrintService extends BaseService {
|
||||
BluetoothPrint _bluetooth = BluetoothPrint.instance;
|
||||
final BluetoothPrint _bluetoothAndr = BluetoothPrint.instance;
|
||||
flutter_blue.BluetoothDevice? _bluetoothDeviceIOS;
|
||||
final flutter_blue.FlutterBlue _bluetoothIOS =
|
||||
flutter_blue.FlutterBlue.instance;
|
||||
|
||||
final bool isAndroid = Platform.isAndroid;
|
||||
final bool isIos = Platform.isIOS;
|
||||
// final bool isAndroid = false;
|
||||
// final bool isIos = true;
|
||||
|
||||
BluetoothDevice? _device;
|
||||
|
||||
Future<void> scan() async {
|
||||
await _bluetooth.startScan(timeout: Duration(seconds: 4));
|
||||
if (isAndroid) {
|
||||
await _bluetoothAndr.startScan(timeout: Duration(seconds: 4));
|
||||
} else if (isIos) {
|
||||
await _bluetoothIOS.startScan(timeout: const Duration(seconds: 5));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> stopScan() async {
|
||||
await _bluetooth.stopScan();
|
||||
if (isAndroid) {
|
||||
await _bluetoothAndr.stopScan();
|
||||
} else if (isIos) {
|
||||
await _bluetoothIOS.stopScan();
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<BluetoothDevice>> get scanResult => _bluetooth.scanResults;
|
||||
Stream<List<BluetoothDevice>> get scanResult {
|
||||
if (isAndroid)
|
||||
return _bluetoothAndr.scanResults;
|
||||
else
|
||||
return _bluetoothIOS.scanResults.asyncMap<List<BluetoothDevice>>(
|
||||
(event) =>
|
||||
Future.wait(event.toList().map((e) async => BluetoothDevice()
|
||||
..name = e.device.name
|
||||
..type = e.device.type.index
|
||||
..address = e.device.id.id)));
|
||||
}
|
||||
|
||||
Stream<bool> get isScanning =>
|
||||
isAndroid ? _bluetoothAndr.isScanning : _bluetoothIOS.isScanning;
|
||||
|
||||
Stream<int> get state => isAndroid
|
||||
? _bluetoothAndr.state
|
||||
: _bluetoothIOS.state.asyncMap<int>((event) => event.index);
|
||||
|
||||
Stream<bool> get isScanning => _bluetooth.isScanning;
|
||||
Stream<int> get state => _bluetooth.state;
|
||||
set device(BluetoothDevice device) => _device = device;
|
||||
|
||||
|
||||
Future<bool> connect() async {
|
||||
bool response = false;
|
||||
if (_device == null) {
|
||||
response = false;
|
||||
} else {
|
||||
// try {
|
||||
// await _bluetoothAndr.connect(_device!);
|
||||
// await Future.delayed(Duration(seconds: 5));
|
||||
// response = true;
|
||||
// } catch (e) {
|
||||
// print('Error connect $e');
|
||||
// response = false;
|
||||
// }
|
||||
|
||||
try {
|
||||
await _bluetooth.connect(_device!);
|
||||
await Future.delayed(Duration(seconds: 5));
|
||||
if (isAndroid) {
|
||||
await _bluetoothAndr.connect(_device!);
|
||||
} else if (isIos) {
|
||||
_bluetoothDeviceIOS = flutter_blue.BluetoothDevice.fromProto(
|
||||
proto.BluetoothDevice(
|
||||
name: _device?.name ?? '',
|
||||
remoteId: _device?.address ?? '',
|
||||
type: proto.BluetoothDevice_Type.valueOf(_device?.type ?? 0),
|
||||
),
|
||||
);
|
||||
final List<flutter_blue.BluetoothDevice> connectedDevices =
|
||||
await _bluetoothIOS.connectedDevices;
|
||||
final int deviceConnectedIndex = connectedDevices
|
||||
.indexWhere((flutter_blue.BluetoothDevice bluetoothDevice) {
|
||||
return bluetoothDevice.id == _bluetoothDeviceIOS?.id;
|
||||
});
|
||||
if (deviceConnectedIndex < 0) {
|
||||
await _bluetoothDeviceIOS?.connect();
|
||||
}
|
||||
}
|
||||
|
||||
response = true;
|
||||
} catch (e) {
|
||||
print('Error connect $e');
|
||||
_device?.connected = true;
|
||||
return Future<bool>.value(response);
|
||||
} on Exception catch (error) {
|
||||
print('$runtimeType - Error $error');
|
||||
response = false;
|
||||
_device?.connected = false;
|
||||
return Future<bool>.value(response);
|
||||
}
|
||||
}
|
||||
return response;
|
||||
|
|
@ -46,7 +109,11 @@ class BluePrintService extends BaseService {
|
|||
Future<bool> disconnect() async {
|
||||
bool response = false;
|
||||
try {
|
||||
await _bluetooth.disconnect();
|
||||
if (isAndroid) {
|
||||
await _bluetoothAndr.disconnect();
|
||||
} else if (isIos) {
|
||||
await _bluetoothDeviceIOS?.disconnect();
|
||||
}
|
||||
response = true;
|
||||
} catch (e) {
|
||||
print('Error $e');
|
||||
|
|
@ -55,7 +122,9 @@ class BluePrintService extends BaseService {
|
|||
return response;
|
||||
}
|
||||
|
||||
Future<bool> printBytes(Uint8List bytes, { int chunkSizeBytes = 20, int queueSleepTimeMs = 20 }) async {
|
||||
Future<bool> printBytes(Uint8List bytes,
|
||||
{int chunkSizeBytes = 20, int queueSleepTimeMs = 20}) async {
|
||||
if (isAndroid) {
|
||||
Map<String, dynamic> config = Map();
|
||||
final len = bytes.length;
|
||||
List<List<int>> chunks = [];
|
||||
|
|
@ -65,10 +134,24 @@ class BluePrintService extends BaseService {
|
|||
}
|
||||
|
||||
for (var i = 0; i < chunks.length; i += 1) {
|
||||
await _bluetooth.rawBytes(config, chunks[i]);
|
||||
await _bluetoothAndr.rawBytes(config, chunks[i]);
|
||||
await Future.delayed(Duration(milliseconds: queueSleepTimeMs));
|
||||
}
|
||||
|
||||
} else if (isIos) {
|
||||
final List<flutter_blue.BluetoothService> bluetoothServices =
|
||||
await _bluetoothDeviceIOS?.discoverServices() ??
|
||||
<flutter_blue.BluetoothService>[];
|
||||
final flutter_blue.BluetoothService bluetoothService =
|
||||
bluetoothServices.firstWhere(
|
||||
(flutter_blue.BluetoothService service) => service.isPrimary,
|
||||
);
|
||||
final flutter_blue.BluetoothCharacteristic characteristic =
|
||||
bluetoothService.characteristics.firstWhere(
|
||||
(flutter_blue.BluetoothCharacteristic bluetoothCharacteristic) =>
|
||||
bluetoothCharacteristic.properties.write,
|
||||
);
|
||||
await characteristic.write(bytes, withoutResponse: true);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,13 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
|||
try {
|
||||
final SettingState state = Redux.store!.state.settingState!;
|
||||
printerManager.device = state.printerBT!;
|
||||
await printerManager.connect();
|
||||
bool isConnected = await printerManager.connect();
|
||||
if(isConnected == false) {
|
||||
await Future.delayed(Duration(seconds: 3));
|
||||
await printerManager.disconnect();
|
||||
return;
|
||||
}
|
||||
await Future.delayed(Duration(seconds: 3));
|
||||
bool isIos = Platform.isIOS;
|
||||
int chunkSizeBytes = 3096;
|
||||
int queueSleepTimeMs = 100;
|
||||
|
|
|
|||
16
pubspec.lock
16
pubspec.lock
|
|
@ -78,6 +78,13 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.15.0"
|
||||
convert:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: convert
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
crypto:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
@ -160,6 +167,13 @@ packages:
|
|||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
flutter_blue:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: flutter_blue
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.8.0"
|
||||
flutter_lock_screen:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
|
@ -491,7 +505,7 @@ packages:
|
|||
name: rxdart
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.27.3"
|
||||
version: "0.26.0"
|
||||
shared_preferences:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ dependencies:
|
|||
permission_handler: ^8.3.0
|
||||
bluetooth_print:
|
||||
path: ../bluetooth_print
|
||||
flutter_blue: ^0.8.0
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
|
|
|||
Loading…
Reference in New Issue