bluetooth_plugin.dart 837 B

1234567891011121314151617181920212223242526272829303132333435
  1. import 'dart:io';
  2. import 'package:flutter/services.dart';
  3. class BluetoothPlugin {
  4. factory BluetoothPlugin() => _instance;
  5. BluetoothPlugin._();
  6. static final BluetoothPlugin _instance = BluetoothPlugin._();
  7. static BluetoothPlugin get instance => _instance;
  8. final MethodChannel _channel = const MethodChannel("io.flutter.plugins/bluetooth");
  9. //开始扫描
  10. Future<int> startScan() async {
  11. if (Platform.isIOS) {
  12. return 0;
  13. }
  14. return _channel.invokeMethod('startScan').then<int>((d) => d);
  15. }
  16. //设备是否支持蓝牙
  17. Future<bool> isSupportBle() async {
  18. if (Platform.isIOS) {
  19. return true;
  20. }
  21. return _channel.invokeMethod('isSupportBle').then<bool>((d) => d);
  22. }
  23. //打开蓝牙
  24. Future open() async {
  25. if (Platform.isAndroid) {
  26. _channel.invokeMethod('open');
  27. }
  28. }
  29. }