Flutter蓝牙通信插件moyoung_ble_plugin的使用

发布于 1周前 作者 h691938207 来自 Flutter

Flutter蓝牙通信插件moyoung_ble_plugin的使用

欢迎使用此插件。这是一个用于与手表进行通信的Flutter插件。

1 引言

欢迎使用此插件。这是一个Flutter插件,用于与手表进行通信。

1.1 平台支持

Android iOS
✔️ ✔️

1.2 使用

要使用此插件,在你的pubspec.yaml文件中添加以下依赖项:

dependencies:
  moyoung_ble_plugin: ^latestVersion

1.3 项目配置

Android

  1. 在生成apk时进行代码混淆。请在你的proguard-rules.pro文件中添加以下配置:

    -keep class com.crrepa.ble.** { *; }
    -keep class com.moyoung.moyoung_ble_plugin.** { *; }
    
  2. 在你的App模块中修改以下配置:

    android {
        ...
        defaultConfig {
            ...
            minSdkVersion 18
            ...
        }
        ...
    }
    

iOS

  1. 需要在info.plist中添加以下内容:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Use the location to sync weather from your phone to your watch.</string>
    <key>NSBluetoothAlwaysUsageDescription</key>
    <string>Use the Bluetooth to connect to your Nokia Watch</string>
    <key>NSCalendarsUsageDescription</key>
    <string>Use the calendar to sync events from your phone to your watch.</string>
    <key>NSCameraUsageDescription</key>
    <string>Use the camera to scan the QR code on your watch screen.</string>
    <key>NSContactsUsageDescription</key>
    <string>The selected contacts will be added to your Watch contacts.</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>Use the Photo to change your watch face.</string>
    <key>UIApplicationSupportsIndirectInputEvents</key>
    <true/>
    <key>UIBackgroundModes</key>
    <array>
        <string>bluetooth-central</string>
    </array>
    
  2. 在你的项目路径中,使用终端命令pod install导入库。

  3. 检查你的podfile.lock,如果文件中的库版本与pubspec.yaml不同,则需要同步podfile.lock中的库版本到pubspec.yaml,然后执行pod install

例如:

podfile.lock中:

- flutter_contacts (0.0.1) :

pubspec.yaml中:

flutter_contacts: ^1.1.4

你必须将podfile.lock中的flutter_contacts (0.0.1)改为flutter_contacts (1.1.4)

1.4 示例

第一步

导入包:

import 'package:moyoung_ble_plugin/moyoung_ble.dart';

第二步

申请一些权限:[location, storage]

提示1:你需要自行申请这些权限。

提示2:当然你可以使用示例项目中的示例来申请权限。但请注意AndroidManifest.xml文件中的权限配置。

第三步

在页面中使用:

class _MyAppState extends State<MyApp> {
  final _streamSubscriptions = <StreamSubscription<dynamic>>[];
  final MoYoungBle _blePlugin = MoYoungBle();
  final List<BleScanBean> _deviceList = [];

  [@override](/user/override)
  void initState() {
    super.initState();
    subscriptStream();
  }

  void subscriptStream() {
    _streamSubscriptions.add(
      _blePlugin.bleScanEveStm.listen(
        (BleScanBean event) async {
          setState(() {
            if (event.isCompleted) {
              // 扫描完成,做些事情
            } else {
              _deviceList.add(event);
            }
          });
        },
      ),
    );
  }

  [@override](/user/override)
  void dispose() {
    super.dispose();
    for (final subscription in _streamSubscriptions) {
      subscription.cancel();
    }
  }

  // 开始扫描蓝牙设备
  void startScan() {
    if (!mounted) return;
    _blePlugin.startScan(10 * 1000).then((value) => {
          setState(() {
            // 做些事情
            print(value ? "Scanning" : "Have not started");
          })
        }).onError((error, stackTrace) => {
          // 通常是一些权限未请求
          print(error.toString())
        });
  }
}

1.5 详细使用文档

点击访问Wiki

1.6 完整示例项目

示例项目包含如何使用代码的详细信息。

点击访问示例

1.7 GNU GENERAL PUBLIC LICENSE 许可证

点击访问许可证


示例代码

import 'dart:async';

import 'package:bluetooth_enable_fork/bluetooth_enable_fork.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:moyoung_ble_plugin/moyoung_ble.dart';
import 'package:moyoung_ble_plugin_example/utils/toast_util.dart';

import 'Global.dart';
import 'components/base/CustomGestureDetector.dart';
import 'modules/contact_list_page.dart';
import 'device.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
import 'package:permission_handler/permission_handler.dart';

import 'modules/demo.dart';

void main() {
  runApp(const MaterialApp(
    title: "moyoung ble demo",
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _streamSubscriptions = <StreamSubscription<dynamic>>[];

  final MoYoungBle _blePlugin = Global.blePlugin;
  String _permissionTxt = "requestPermissions()";
  String _scanBtnTxt = "startScan(10*1000)";
  String _cancelScanResult = "cancelScan()";
  String _contactInfo = 'skip 2 contacts';
  bool enableBluetooth = false;
  final List<BleScanBean> _deviceList = [];
  bool isQuit = false;
  CrossFadeState displayState1 = CrossFadeState.showSecond;
  CrossFadeState displayState2 = CrossFadeState.showSecond;

  [@override](/user/override)
  void initState() {
    super.initState();
    subscriptStream();
  }

  void subscriptStream() {
    _streamSubscriptions.add(
      _blePlugin.bleScanEveStm.listen(
        (BleScanBean event) async {
          setState(() {
            if (event.isCompleted) {
              _scanBtnTxt = "Scan completed";
            } else {
              _deviceList.add(event);
            }
          });
        },
      ),
    );
  }

  [@override](/user/override)
  void dispose() {
    super.dispose();
    for (final subscription in _streamSubscriptions) {
      subscription.cancel();
    }
  }

  /// 退出app并且断开连接
  Future<bool> exitAppWithDisconnect() {
    if (!isQuit) {
      ToastUtil.show("再按一次退出app", Toast.LENGTH_SHORT);
      isQuit = true;
      return Future.value(false);
    }

    SystemChannels.platform.invokeMethod('SystemNavigator.pop');
    return Future.value(true);
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: WillPopScope(
        onWillPop: () => exitAppWithDisconnect(),
        child: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              CustomGestureDetector(
                  title: 'Permission',
                  childrenBCallBack: (CrossFadeState newDisplayState) {
                    setState(() {
                      displayState1 = newDisplayState;
                    });
                  },
                  displayState: displayState1,
                  children: <Widget>[
                    ElevatedButton(
                        onPressed: () {
                          Navigator.push(context, MaterialPageRoute(builder: (context) {
                            return const Demo();
                          }));
                        },
                        child: const Text("Demo", style: TextStyle(fontSize: 14))),
                    ElevatedButton(child: Text(_permissionTxt, style: const TextStyle(fontSize: 14)), onPressed: requestPermissions),
                    ElevatedButton(
                        onPressed: checkBluetoothEnable,
                        child: Text("checkBluetoothPermission: $enableBluetooth", style: const TextStyle(fontSize: 14))),
                    ElevatedButton(child: Text(_contactInfo, style: const TextStyle(fontSize: 14)), onPressed: selectContact),
                  ]),
              Padding(
                  padding: const EdgeInsets.only(left: 20, right: 20),
                  child: ElevatedButton(child: Text(_scanBtnTxt, style: const TextStyle(fontSize: 14)), onPressed: startScan)),
              Padding(
                  padding: const EdgeInsets.only(left: 20, right: 20),
                  child: ElevatedButton(child: Text(_cancelScanResult, style: const TextStyle(fontSize: 14)), onPressed: cancelScan)),
              Expanded(
                child: ListView.separated(
                    itemBuilder: (BuildContext context, int index) {
                      return InkWell(
                          onTap: () {
                            cancelScan();
                            Navigator.push(context, MaterialPageRoute(builder: (context) {
                              return DevicePage(
                                device: _deviceList[index],
                              );
                            }));
                          },
                          child: Container(
                            width: double.maxFinite,
                            padding: const EdgeInsets.only(right: 20, left: 20, top: 5, bottom: 5),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(_deviceList[index].name, style: const TextStyle(fontSize: 16)),
                                Text(_deviceList[index].address, style: const TextStyle(height: 2, fontSize: 14, color: Colors.grey)),
                              ],
                            ),
                          ));
                    },
                    separatorBuilder: (BuildContext context, int index) {
                      return const Divider(
                        color: Colors.blue,
                      );
                    },
                    itemCount: _deviceList.length),
              )
            ],
          ),
        ),
      ),
    ));
  }

  void checkBluetoothEnable() async {
    if (!mounted) return;
    bool _enableBluetooth = await _blePlugin.checkBluetoothEnable;
    if (!_enableBluetooth) {
      BluetoothEnable.enableBluetooth.then((value) {
        if (value == "true") {
          setState(() {
            enableBluetooth = true;
          });
        }
      });
    }
    setState(() {
      enableBluetooth = _enableBluetooth;
    });
  }

  void requestPermissions() {
    [
      Permission.location,
      Permission.storage,
      Permission.manageExternalStorage,
      Permission.bluetoothConnect,
      Permission.bluetoothScan,
      Permission.bluetoothAdvertise
    ].request().then((value) => {
          setState(() {
            Map<Permission, PermissionStatus> statuses = value;
            if (statuses[Permission.location] == PermissionStatus.denied) {
              String permissionName = Permission.location.toString();
              _permissionTxt = "$permissionName is denied";
              return;
            }
            if (statuses[Permission.storage] == PermissionStatus.denied) {
              String permissionName = Permission.storage.toString();
              _permissionTxt = "$permissionName is denied";
              return;
            }

            _permissionTxt = "Permission is granted.";
          })
        });
  }

  void startScan() {
    if (!mounted) return;
    setState(() {
      _deviceList.clear();
    });

    _blePlugin
        .startScan(10 * 1000)
        .then((value) => {
              setState(() {
                _scanBtnTxt = value ? "Scanning" : "Scan filed";
              })
            })
        .onError((error, stackTrace) => {print(error)});
  }

  Future<void> cancelScan() async {
    await _blePlugin.cancelScan;
    if (!mounted) return;
    setState(() {
      _cancelScanResult = 'cancelScan()';
    });
  }

  Future<void> selectContact() async {
    final Contact contact = await Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => FlutterContactsExample(pageContext: context),
        ));

    if (!mounted) return;

    setState(() {
      String name = contact.name.toString();
      String phone = contact.phones.first.toString();
      _contactInfo = '$name, $phone';
    });
  }
}

更多关于Flutter蓝牙通信插件moyoung_ble_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter蓝牙通信插件moyoung_ble_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个使用Flutter蓝牙通信插件moyoung_ble_plugin的代码示例。该示例展示了如何扫描设备、连接到设备、读取和写入蓝牙特征值(Characteristic)。

首先,确保你已经在pubspec.yaml文件中添加了moyoung_ble_plugin依赖:

dependencies:
  flutter:
    sdk: flutter
  moyoung_ble_plugin: ^最新版本号  # 替换为实际的最新版本号

然后,运行flutter pub get来获取依赖。

接下来是一个完整的Flutter应用示例:

import 'package:flutter/material.dart';
import 'package:moyoung_ble_plugin/moyoung_ble_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final MoyoungBlePlugin _ble = MoyoungBlePlugin();
  List<BluetoothDevice> devices = [];
  BluetoothDevice? connectedDevice;
  BluetoothCharacteristic? characteristic;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Bluetooth Communication'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              ElevatedButton(
                onPressed: scanDevices,
                child: Text('Scan Devices'),
              ),
              SizedBox(height: 16),
              Expanded(
                child: ListView.builder(
                  itemCount: devices.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(devices[index].name ?? 'Unknown'),
                      subtitle: Text(devices[index].id.toString()),
                      onTap: () async {
                        if (connectedDevice == null) {
                          await connectToDevice(devices[index]);
                        }
                      },
                    );
                  },
                ),
              ),
              SizedBox(height: 16),
              if (connectedDevice != null)
                Column(
                  children: [
                    ElevatedButton(
                      onPressed: discoverServices,
                      child: Text('Discover Services'),
                    ),
                    SizedBox(height: 8),
                    ElevatedButton(
                      onPressed: () async {
                        if (characteristic != null) {
                          await readCharacteristic(characteristic!);
                        }
                      },
                      child: Text('Read Characteristic'),
                    ),
                    SizedBox(height: 8),
                    ElevatedButton(
                      onPressed: () async {
                        if (characteristic != null) {
                          await writeCharacteristic(characteristic!, Uint8List.fromList([0x01]));
                        }
                      },
                      child: Text('Write Characteristic'),
                    ),
                  ],
                ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> scanDevices() async {
    devices = await _ble.scanDevices();
    setState(() {});
  }

  Future<void> connectToDevice(BluetoothDevice device) async {
    connectedDevice = await _ble.connectToDevice(device.id);
    setState(() {});
  }

  Future<void> discoverServices() async {
    if (connectedDevice != null) {
      var services = await _ble.discoverServices(connectedDevice!.id);
      // 处理服务列表,找到你需要的特征值
      // 这里假设我们获取第一个服务的第一个特征值作为示例
      if (services.isNotEmpty && services.first.characteristics.isNotEmpty) {
        characteristic = services.first.characteristics.first;
      }
      setState(() {});
    }
  }

  Future<void> readCharacteristic(BluetoothCharacteristic characteristic) async {
    var value = await _ble.readCharacteristic(characteristic.uuid);
    print('Characteristic value: $value');
  }

  Future<void> writeCharacteristic(BluetoothCharacteristic characteristic, Uint8List value) async {
    await _ble.writeCharacteristic(characteristic.uuid, value);
    print('Characteristic written');
  }
}

说明:

  1. 扫描设备scanDevices()函数扫描附近的蓝牙设备,并将结果存储在devices列表中。
  2. 连接到设备connectToDevice(BluetoothDevice device)函数连接到指定的蓝牙设备。
  3. 发现服务discoverServices()函数发现连接设备的服务,并假设我们获取第一个服务的第一个特征值。
  4. 读取特征值readCharacteristic(BluetoothCharacteristic characteristic)函数读取指定特征值。
  5. 写入特征值writeCharacteristic(BluetoothCharacteristic characteristic, Uint8List value)函数向指定特征值写入数据。

注意事项:

  • 确保你的应用有适当的权限来访问蓝牙功能。
  • 在实际使用中,你可能需要更复杂的错误处理和状态管理。
  • 蓝牙操作通常是异步的,因此代码中使用了很多await关键字。

这个示例展示了如何使用moyoung_ble_plugin进行基本的蓝牙通信操作。根据具体需求,你可能需要扩展或修改这些功能。

回到顶部