flutter plugin for zebra multiplatform sdk

flutter_zsdk_method_channel.dart 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_blue_plus/flutter_blue_plus.dart';
  6. import 'package:flutter_zsdk/src/models/bluetooth_printer.dart';
  7. import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
  8. import 'package:permission_handler/permission_handler.dart';
  9. import 'flutter_zsdk_platform_interface.dart';
  10. /// An implementation of [FlutterZsdkPlatform] that uses method channels.
  11. class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
  12. static const bluetoothDiscoveryEventChannel = EventChannel('id.kalanusa.flutter_zsdk.channel_events/bluetooth_discovery');
  13. /// The method channel used to interact with the native platform.
  14. @visibleForTesting
  15. final methodChannel = const MethodChannel('flutter_zsdk');
  16. @override
  17. Future<String?> getPlatformVersion() async {
  18. final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
  19. return version;
  20. }
  21. @override
  22. Future<Stream<dynamic>> findBluetoothPrinters() async {
  23. // methodChannel.invokeMethod("findBluetoothPrinters");
  24. // final bluetoothPrinters = await methodChannel.invokeMethod<List<BluetoothPrinter>>('findBluetoothPrinters');
  25. // print(bluetoothPrinters);
  26. // return bluetoothPrinters ?? [];
  27. // StreamSubscription streamSubscription = bluetoothDiscoveryEventChannel.receiveBroadcastStream().listen((event) {
  28. // print(event);
  29. // });
  30. if (!await FlutterBluePlus.isSupported) {
  31. throw FlutterZsdkException("Bluetooth is not supported on this device.");
  32. }
  33. if (await FlutterBluePlus.adapterState.first != BluetoothAdapterState.on) {
  34. if (Platform.isAndroid) {
  35. try {
  36. await FlutterBluePlus.turnOn();
  37. } on FlutterBluePlusException catch (e) {
  38. throw FlutterZsdkException("Failed to turn on bluetooth: ${e.description}");
  39. }
  40. } else {
  41. throw FlutterZsdkException("Bluetooth is not turned on on this device.");
  42. }
  43. }
  44. var bluetoothPermissionStatus = await Permission.bluetooth.status;
  45. var bluetoothConnectPermissionStatus = await Permission.bluetoothConnect.status;
  46. var bluetoothScanPermissionStatus = await Permission.bluetoothScan.status;
  47. var locationPermissionStatus = await Permission.location.status;
  48. if (!bluetoothPermissionStatus.isGranted || !bluetoothConnectPermissionStatus.isGranted || !bluetoothScanPermissionStatus.isGranted || !locationPermissionStatus.isGranted) {
  49. Map<Permission, PermissionStatus> permissionStatuses = await [
  50. Permission.bluetooth,
  51. Permission.bluetoothConnect,
  52. Permission.bluetoothScan,
  53. Permission.location
  54. ].request();
  55. print(permissionStatuses[Permission.bluetooth]);
  56. print(permissionStatuses[Permission.bluetoothConnect]);
  57. print(permissionStatuses[Permission.bluetoothScan]);
  58. print(permissionStatuses[Permission.location]);
  59. if (permissionStatuses[Permission.bluetooth] == PermissionStatus.granted && permissionStatuses[Permission.bluetoothConnect] == PermissionStatus.granted && permissionStatuses[Permission.bluetoothScan] == PermissionStatus.granted && permissionStatuses[Permission.location] == PermissionStatus.granted) {
  60. return bluetoothDiscoveryEventChannel.receiveBroadcastStream();
  61. } else {
  62. // throw Exception("Permissions not granted, please allow permission to discover bluetooth printers.");
  63. throw FlutterZsdkException("Permissions not granted, please allow permission to discover bluetooth printers.");
  64. }
  65. } else {
  66. return bluetoothDiscoveryEventChannel.receiveBroadcastStream();
  67. }
  68. }
  69. }