Flutter 開發(fā)之藍(lán)牙通信

本文包含:

藍(lán)牙簡(jiǎn)介;

Flutter 中藍(lán)牙開發(fā)步驟;

Flutter 插件 flutter_blue 介紹;

Flutter 插件 flutter_blue 詳細(xì)使用步驟;

Flutter 插件 flutter_blue 的二次封裝,以便簡(jiǎn)潔調(diào)用;

Flutter 完整 藍(lán)牙通訊 含:搜索,連接,匹配特征值,發(fā)送數(shù)據(jù),接收數(shù)據(jù) 下載地址;

Flutter 完整 藍(lán)牙通訊項(xiàng)目實(shí)例修改 下位機(jī)參數(shù) 特征值可直接使用 (暫無);

藍(lán)牙簡(jiǎn)介;

藍(lán)牙是設(shè)備近距離通信的一種方便手段,現(xiàn)在很多藍(lán)牙設(shè)備都是指藍(lán)牙4.0,4.0以其低功耗著稱。
通過藍(lán)牙進(jìn)行通訊交互分為兩方,一方為中心設(shè)備central(手機(jī)),一方為外設(shè)peripheral(下位機(jī)硬件設(shè)備),外設(shè)通過廣播的方式向外發(fā)送信息,中心設(shè)備檢索到外設(shè)發(fā)的廣播信息,可以進(jìn)行配對(duì)連接,進(jìn)而進(jìn)行數(shù)據(jù)交互。

Flutter 中藍(lán)牙開發(fā)步驟;

簡(jiǎn)單概括為:
1.添加藍(lán)牙權(quán)限
2.掃描藍(lán)牙設(shè)備
3.連接到設(shè)備并顯示具有特征的服務(wù)
4.匹配對(duì)應(yīng)權(quán)限特征。例如:有讀取,寫入權(quán)限的特征值
5.根據(jù)協(xié)議向下位機(jī)設(shè)備寫入數(shù)據(jù)
6.手機(jī)端接收到下位機(jī)返回的數(shù)據(jù),并相應(yīng)處理

Flutter 插件 flutter_blue 介紹;

FlutterBlue是一款flutter對(duì)藍(lán)牙插件,旨在提供來自兩個(gè)平臺(tái)(iOS和Android)的最大功能。 使用FlutterBlue實(shí)例,您可以掃描并連接到附近的設(shè)備(BluetoothDevice)。一旦連接到設(shè)備,BluetoothDevice對(duì)象就可以發(fā)現(xiàn)服務(wù)(BluetoothService),特征(BluetoothCharacteristic)和描述符(BluetoothDescriptor)。然后,BluetoothDevice對(duì)象用于直接與特征和描述符交互。

flutter_blue 官方介紹以及官方示例下載地址 小聲逼逼:我覺得示例不咋地...

Flutter 插件 flutter_blue 詳細(xì)使用步驟;

1.添加藍(lán)牙權(quán)限

Android 端權(quán)限添加:

//文件名:AndroidManifest.xml
        <uses-permission android:name="android.permission.BLUETOOTH"/>
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Android 端權(quán)限添加地址

iOS 端權(quán)限添加:

//文件名:Info.plist
    <key>NSBluetoothAlwaysUsageDescription</key>
    <string>App需要您的同意,才能訪問藍(lán)牙,進(jìn)行設(shè)備連接,數(shù)據(jù)通訊服務(wù)</string>
    <key>NSBluetoothPeripheralUsageDescription</key>
    <string>App需要您的同意,才能訪問藍(lán)牙,進(jìn)行設(shè)備連接,數(shù)據(jù)通訊服務(wù)</string>
iOS 端權(quán)限添加地址

2.添加flutter_blue庫(kù)

//文件名:pubspec.yaml
    flutter_blue: ^0.7.2

3.創(chuàng)建實(shí)例 FlutterBlue

//在你需要連接操作藍(lán)牙的xxxx.dart 界面中 導(dǎo)入
import 'package:flutter_blue/flutter_blue.dart';

//可能用到的異步操作庫(kù)
import 'dart:async';

FlutterBlue flutterBlue = FlutterBlue.instance;

/*
提示
個(gè)人建議 實(shí)例創(chuàng)建 在 initState方法中 ,并且FlutterBlue flutterBlue 聲明出來,因?yàn)楹竺娌僮饕玫絝lutterBlue
*/

4.掃描藍(lán)牙設(shè)備

// 開始掃描 
              flutterBlue.startScan();
              // 監(jiān)聽掃描結(jié)果
              flutterBlue.scanResults.listen((results) {
                // 掃描結(jié)果 可掃描到的所有藍(lán)牙設(shè)備
                for (ScanResult r in results) {
                  scanResults[r.device.name] = r;
                  if (r.device.name.length > 0) {
                    print('${r.device.name} found! rssi: ${r.rssi}');
                    allBlueNameAry.add(r.device.name);
                  }
                }
              });
/*
備注

scanResults  是我在前面有聲明的一個(gè) 所有搜索結(jié)果數(shù)據(jù)集
allBlueNameAry  是我在前面聲明的一個(gè) 所有搜索到藍(lán)牙名字的數(shù)組
mCharacteristic 是我在前面聲明的一個(gè)特征 因?yàn)閷懭霐?shù)據(jù),接收數(shù)據(jù)回調(diào)時(shí)需要用到,所以我聲明在前面 

Map<String, ScanResult> scanResults = new Map();
List allBlueNameAry = [];
BluetoothCharacteristic mCharacteristic;

*/

5.連接到設(shè)備并顯示具有特征的服務(wù)

List distinctIds = allBlueNameAry.toSet().toList();
              print("我是過濾后的 藍(lán)牙名字 $distinctIds");
              for (var i = 0; i < distinctIds.length; i++) {
                bool isEquipment = distinctIds[i].contains("需要連接的設(shè)備名");
                if (isEquipment) {
                  ScanResult r = scanResults[distinctIds[i]];
                  device = r.device;
                  
                  // 停止掃描
                  flutterBlue.stopScan();
                  
                  // 連接藍(lán)牙設(shè)備 以及掃描特征值
                  _BleDiscoverServices();
                }
              }

6.匹配對(duì)應(yīng)權(quán)限特征。例如:有讀取,寫入權(quán)限的特征值

//_BleDiscoverServices 方法在5.連接到設(shè)備并顯示具有特征的服務(wù)中調(diào)用
 _BleDiscoverServices() async {
    print("連接上GTRS設(shè)備...延遲連接");
    await device.connect(autoConnect: false, timeout: Duration(seconds: 10));
    List<BluetoothService> services = await device.discoverServices();
    services.forEach((service) {
      var value = service.uuid.toString();
      print("所有服務(wù)值 --- $value");
      if (service.uuid.toString().toUpperCase().substring(4, 8) == "FFF0") {
        List<BluetoothCharacteristic> characteristics = service.characteristics;
        characteristics.forEach((characteristic) {
          var valuex = characteristic.uuid.toString();
          print("所有特征值 --- $valuex");
          if (characteristic.uuid.toString() ==
              "0000fff1-0000-1000-8000-XXXXXXXXXX") {
            print("匹配到正確的特征值");
            mCharacteristic = characteristic;

            const timeout = const Duration(seconds: 30);
            Timer(timeout, () {
              //收到下位機(jī)返回藍(lán)牙數(shù)據(jù)回調(diào)監(jiān)聽
              _BleDataCallback();
            });
          }
        });
      }
    });
  }

7.根據(jù)協(xié)議向下位機(jī)設(shè)備寫入數(shù)據(jù)

//mCharacteristic   4.掃描藍(lán)牙設(shè)備備注有介紹、6.匹配對(duì)應(yīng)權(quán)限特征中給它賦值
mCharacteristic.write(
                    [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);

8.手機(jī)端接收到下位機(jī)返回的數(shù)據(jù),并相應(yīng)處理

//mCharacteristic   4.掃描藍(lán)牙設(shè)備備注有介紹、6.匹配對(duì)應(yīng)權(quán)限特征中給它賦值
//_BleDataCallback 方法在6.匹配對(duì)應(yīng)權(quán)限特征中 調(diào)用
  _BleDataCallback() async {
    await mCharacteristic.setNotifyValue(true);
    mCharacteristic.value.listen((value) {
      // do something with new value
      // print("我是藍(lán)牙返回?cái)?shù)據(jù) - $value");
      if (value == null) {
        print("我是藍(lán)牙返回?cái)?shù)據(jù) - 空??!");
        return;
      }
      List data = [];
      for (var i = 0; i < value.length; i++) {
        String dataStr = value[i].toRadixString(16);
        if (dataStr.length < 2) {
          dataStr = "0" + dataStr;
        }
        String dataEndStr = "0x" + dataStr;
        data.add(dataEndStr);
      }
      print("我是藍(lán)牙返回?cái)?shù)據(jù) - $data");
    });
  }

9.斷開藍(lán)牙連接

device.disconnect();

Flutter 插件 flutter_blue 的二次封裝,以便簡(jiǎn)潔調(diào)用:

//創(chuàng)建 一個(gè)叫  -- ble_mannager.dart 文件
/*以下全部復(fù)制進(jìn)文件*/
import 'dart:math';
import 'package:flutter_blue/flutter_blue.dart';
import 'dart:async';

class ble_data_model {
  /*
  藍(lán)牙參數(shù)
  */
  FlutterBlue flutterBlue;
  BluetoothDevice device;
  Map<String, ScanResult> scanResults;
  List allBleNameAry;
  BluetoothCharacteristic mCharacteristic;
}

//藍(lán)牙數(shù)據(jù)模型
ble_data_model model = new ble_data_model();

void initBle() {
  BluetoothDevice device;
  Map<String, ScanResult> scanResults = new Map();
  List allBleNameAry = [];
  BluetoothCharacteristic mCharacteristic;

  model.flutterBlue = FlutterBlue.instance;
  model.device = device;
  model.scanResults = scanResults;
  model.allBleNameAry = allBleNameAry;
  model.mCharacteristic = mCharacteristic;
}

void startBle() async {
  // 開始掃描
  model.flutterBlue.startScan(timeout: Duration(seconds: 4));
  // 監(jiān)聽掃描結(jié)果
  model.flutterBlue.scanResults.listen((results) {
    // 掃描結(jié)果 可掃描到的所有藍(lán)牙設(shè)備
    for (ScanResult r in results) {
      model.scanResults[r.device.name] = r;
      if (r.device.name.length > 0) {
        // print('${r.device.name} found! rssi: ${r.rssi}');
        model.allBleNameAry.add(r.device.name);
        getBleScanNameAry();
      }
    }
  });
}

List getBleScanNameAry() {
  //更新過濾藍(lán)牙名字
  List distinctIds = model.allBleNameAry.toSet().toList();
  model.allBleNameAry = distinctIds;
  return model.allBleNameAry;
}

void connectionBle(int chooseBle) {
  for (var i = 0; i < model.allBleNameAry.length; i++) {
    bool isBleName = model.allBleNameAry[i].contains("GTRS");
    if (isBleName) {
      ScanResult r = model.scanResults[model.allBleNameAry[i]];
      model.device = r.device;

      // 停止掃描
      model.flutterBlue.stopScan();

      discoverServicesBle();
    }
  }
}

void discoverServicesBle() async {
  print("連接上藍(lán)牙設(shè)備...延遲連接");
  await model.device
      .connect(autoConnect: false, timeout: Duration(seconds: 10));
  List<BluetoothService> services = await model.device.discoverServices();
  services.forEach((service) {
    var value = service.uuid.toString();
    print("所有服務(wù)值 --- $value");
    if (service.uuid.toString().toUpperCase().substring(4, 8) == "FFF0") {
      List<BluetoothCharacteristic> characteristics = service.characteristics;
      characteristics.forEach((characteristic) {
        var valuex = characteristic.uuid.toString();
        print("所有特征值 --- $valuex");
        if (characteristic.uuid.toString() ==
            "0000fff1-0000-1000-8000-xxxxxxxxx") {
          print("匹配到正確的特征值");
          model.mCharacteristic = characteristic;

          const timeout = const Duration(seconds: 30);
          Timer(timeout, () {
            dataCallbackBle();
          });
        }
      });
    }
    // do something with service
  });
}

dataCallsendBle(List<int> value) {
  model.mCharacteristic.write(value);
}

dataCallbackBle() async {
  await model.mCharacteristic.setNotifyValue(true);
  model.mCharacteristic.value.listen((value) {
    // do something with new value
    // print("我是藍(lán)牙返回?cái)?shù)據(jù) - $value");
    if (value == null) {
      print("我是藍(lán)牙返回?cái)?shù)據(jù) - 空??!");
      return;
    }
    List data = [];
    for (var i = 0; i < value.length; i++) {
      String dataStr = value[i].toRadixString(16);
      if (dataStr.length < 2) {
        dataStr = "0" + dataStr;
      }
      String dataEndStr = "0x" + dataStr;
      data.add(dataEndStr);
    }
    print("我是藍(lán)牙返回?cái)?shù)據(jù) - $data");
  });
}

void endBle() {
  model.device.disconnect();
}

Flutter 完整 藍(lán)牙通訊 含:搜索,連接,匹配特征值,發(fā)送數(shù)據(jù),接收數(shù)據(jù);

自行封裝 flutter_ble_mannager 暫缺實(shí)例

Flutter 完整 藍(lán)牙通訊項(xiàng)目實(shí)例修改 下位機(jī)參數(shù) 特征值可直接使用;

//未完待續(xù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容