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

本文包含:

藍牙簡介;

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

Flutter 插件 flutter_blue 介紹;

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

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

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

Flutter 完整 藍牙通訊項目實例修改 下位機參數(shù) 特征值可直接使用 (暫無);

藍牙簡介;

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

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

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

Flutter 插件 flutter_blue 介紹;

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

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

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

1.添加藍牙權(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需要您的同意,才能訪問藍牙,進行設(shè)備連接,數(shù)據(jù)通訊服務(wù)</string>
    <key>NSBluetoothPeripheralUsageDescription</key>
    <string>App需要您的同意,才能訪問藍牙,進行設(shè)備連接,數(shù)據(jù)通訊服務(wù)</string>
iOS 端權(quán)限添加地址

2.添加flutter_blue庫

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

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

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

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

FlutterBlue flutterBlue = FlutterBlue.instance;

/*
提示
個人建議 實例創(chuàng)建 在 initState方法中 ,并且FlutterBlue flutterBlue 聲明出來,因為后面操作要用到flutterBlue
*/

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

// 開始掃描 
              flutterBlue.startScan();
              // 監(jiān)聽掃描結(jié)果
              flutterBlue.scanResults.listen((results) {
                // 掃描結(jié)果 可掃描到的所有藍牙設(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  是我在前面有聲明的一個 所有搜索結(jié)果數(shù)據(jù)集
allBlueNameAry  是我在前面聲明的一個 所有搜索到藍牙名字的數(shù)組
mCharacteristic 是我在前面聲明的一個特征 因為寫入數(shù)據(jù),接收數(shù)據(jù)回調(diào)時需要用到,所以我聲明在前面 

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

*/

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

List distinctIds = allBlueNameAry.toSet().toList();
              print("我是過濾后的 藍牙名字 $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();
                  
                  // 連接藍牙設(shè)備 以及掃描特征值
                  _BleDiscoverServices();
                }
              }

6.匹配對應(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, () {
              //收到下位機返回藍牙數(shù)據(jù)回調(diào)監(jiān)聽
              _BleDataCallback();
            });
          }
        });
      }
    });
  }

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

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

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

//mCharacteristic   4.掃描藍牙設(shè)備備注有介紹、6.匹配對應(yīng)權(quán)限特征中給它賦值
//_BleDataCallback 方法在6.匹配對應(yīng)權(quán)限特征中 調(diào)用
  _BleDataCallback() async {
    await mCharacteristic.setNotifyValue(true);
    mCharacteristic.value.listen((value) {
      // do something with new value
      // print("我是藍牙返回數(shù)據(jù) - $value");
      if (value == null) {
        print("我是藍牙返回數(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("我是藍牙返回數(shù)據(jù) - $data");
    });
  }

9.斷開藍牙連接

device.disconnect();

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

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

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

//藍牙數(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é)果 可掃描到的所有藍牙設(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() {
  //更新過濾藍牙名字
  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("連接上藍牙設(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("我是藍牙返回數(shù)據(jù) - $value");
    if (value == null) {
      print("我是藍牙返回數(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("我是藍牙返回數(shù)據(jù) - $data");
  });
}

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

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

自行封裝 flutter_ble_mannager 暫缺實例

Flutter 完整 藍牙通訊項目實例修改 下位機參數(shù) 特征值可直接使用;

//未完待續(xù)

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

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