flutter_blue added for ios

migrate_to_ns
error500 2022-02-01 13:22:39 +06:00
parent 7b06e4c390
commit 16ea7ead94
4 changed files with 132 additions and 28 deletions

View File

@ -1,43 +1,106 @@
import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:aman_kassa_flutter/core/base/base_service.dart'; import 'package:aman_kassa_flutter/core/base/base_service.dart';
import 'package:bluetooth_print/bluetooth_print.dart'; import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.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 { 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; BluetoothDevice? _device;
Future<void> scan() async { 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 { 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; set device(BluetoothDevice device) => _device = device;
Future<bool> connect() async { Future<bool> connect() async {
bool response = false; bool response = false;
if (_device == null) { if (_device == null) {
response = false; response = false;
} else { } else {
// try {
// await _bluetoothAndr.connect(_device!);
// await Future.delayed(Duration(seconds: 5));
// response = true;
// } catch (e) {
// print('Error connect $e');
// response = false;
// }
try { try {
await _bluetooth.connect(_device!); if (isAndroid) {
await Future.delayed(Duration(seconds: 5)); 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; response = true;
} catch (e) { _device?.connected = true;
print('Error connect $e'); return Future<bool>.value(response);
} on Exception catch (error) {
print('$runtimeType - Error $error');
response = false; response = false;
_device?.connected = false;
return Future<bool>.value(response);
} }
} }
return response; return response;
@ -46,7 +109,11 @@ class BluePrintService extends BaseService {
Future<bool> disconnect() async { Future<bool> disconnect() async {
bool response = false; bool response = false;
try { try {
await _bluetooth.disconnect(); if (isAndroid) {
await _bluetoothAndr.disconnect();
} else if (isIos) {
await _bluetoothDeviceIOS?.disconnect();
}
response = true; response = true;
} catch (e) { } catch (e) {
print('Error $e'); print('Error $e');
@ -55,21 +122,37 @@ class BluePrintService extends BaseService {
return response; return response;
} }
Future<bool> printBytes(Uint8List bytes, { int chunkSizeBytes = 20, int queueSleepTimeMs = 20 }) async { Future<bool> printBytes(Uint8List bytes,
Map<String, dynamic> config = Map(); {int chunkSizeBytes = 20, int queueSleepTimeMs = 20}) async {
final len = bytes.length; if (isAndroid) {
List<List<int>> chunks = []; Map<String, dynamic> config = Map();
for (var i = 0; i < len; i += chunkSizeBytes) { final len = bytes.length;
var end = (i + chunkSizeBytes < len) ? i + chunkSizeBytes : len; List<List<int>> chunks = [];
chunks.add(bytes.sublist(i, end)); for (var i = 0; i < len; i += chunkSizeBytes) {
} var end = (i + chunkSizeBytes < len) ? i + chunkSizeBytes : len;
chunks.add(bytes.sublist(i, end));
}
for (var i = 0; i < chunks.length; i += 1) { 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)); 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; return true;
} }
} }

View File

@ -49,7 +49,13 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
try { try {
final SettingState state = Redux.store!.state.settingState!; final SettingState state = Redux.store!.state.settingState!;
printerManager.device = state.printerBT!; 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; bool isIos = Platform.isIOS;
int chunkSizeBytes = 3096; int chunkSizeBytes = 3096;
int queueSleepTimeMs = 100; int queueSleepTimeMs = 100;

View File

@ -78,6 +78,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.15.0" version: "1.15.0"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
crypto: crypto:
dependency: transitive dependency: transitive
description: description:
@ -160,6 +167,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" 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: flutter_lock_screen:
dependency: "direct main" dependency: "direct main"
description: description:
@ -491,7 +505,7 @@ packages:
name: rxdart name: rxdart
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.27.3" version: "0.26.0"
shared_preferences: shared_preferences:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -40,6 +40,7 @@ dependencies:
permission_handler: ^8.3.0 permission_handler: ^8.3.0
bluetooth_print: bluetooth_print:
path: ../bluetooth_print path: ../bluetooth_print
flutter_blue: ^0.8.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter