flutter蓝牙开发 获取服务等

获取服务后跳转页面

class WriteNotifyDeviceUtils {
  /// modelId,device,characteristicWrite,characteristicNotify  && get.to
  static void writeNotifyDeviceUtils(
      GlobalWiFiBluetooth globalWiFiBluetooth, BluetoothDevice device,
      {Function? callback, Function? getThen}) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    // bool isFirstGetIn = preferences.getBool("genkinno_wifi_setting") ?? false;
    if (device != globalWiFiBluetooth.device) {
      globalWiFiBluetooth.blueDeviceVoid(device);
    }
    if (globalWiFiBluetooth.modelId != "" && globalWiFiBluetooth.productType.value != "") {
      List<BluetoothService> services = [];
      try {
        if (globalWiFiBluetooth.device!.isDisconnected) {
          await globalWiFiBluetooth.device!
              .connect(timeout: const Duration(milliseconds: 2500));
        }
        services = await globalWiFiBluetooth.device!.discoverServices();
      } catch (e) {
        //
      }
      if (services.isEmpty) {
        if (globalWiFiBluetooth.device!.isConnected) {
          await globalWiFiBluetooth.device!.disconnect();
        }

        // EasyLoading.showToast("bluetooth_connect_failed".tr,
        //     dismissOnTap: true);
        return;
      }
      await preferences.remove('list');
      BluetoothCharacteristic? characteristicWrite;
      BluetoothCharacteristic? characteristicNotify;
      for (var element in services) {
        if (element.uuid.toString() == "asdfghjkl") {
          for (BluetoothCharacteristic characteristic
              in element.characteristics) {
            if (characteristic.properties.write) {
              characteristicWrite = characteristic;
              globalWiFiBluetooth.characteristicWriteVoid(characteristicWrite);
            }
            if (characteristic.properties.notify) {
              characteristicNotify = characteristic;
              globalWiFiBluetooth
                  .characteristicNotifyVoid(characteristicNotify);
              if (characteristic.properties.notify ||
                  characteristic.properties.indicate) {
                try {
                  characteristic.setNotifyValue(true);
                } catch (e) {
                  //
                }
              }
            }
            if (characteristicWrite != null && characteristicNotify != null) {
              EasyLoading.dismiss();
              globalWiFiBluetooth.isHaveDeviceVoid(false);
              globalWiFiBluetooth.deviceBottomIndexVoid(0);
              globalWiFiBluetooth.isDevicePageVoid(true);
              if (globalWiFiBluetooth.productType.value != "2" &&
                  globalWiFiBluetooth.productType.value != "") {
                // if (isFirstGetIn) {
                //   Get.to(() => const WifiSettingPage())?.then((value) => {
                //         globalWiFiBluetooth.isDevicePageVoid(false),
                //         if (getThen != null) {getThen()}
                //       });
                // } else {
                  Get.to(() => const DeviceMain())?.then((value) => {
                        globalWiFiBluetooth.isDevicePageVoid(false),
                        if (getThen != null) {getThen()}
                      });
                // }
                return;
              }
              if (globalWiFiBluetooth.productType.value == "2") {
                Get.to(() => const DeviceScreen())?.then((value) => {
                      globalWiFiBluetooth.isDevicePageVoid(false),
                      if (getThen != null) {getThen()}
                    });
                return;
              }
            }
          }
        }
      }
    } else {
      if (callback != null) {
        callback();
      }
    }
  }



获取服务

  /// device,characteristicWrite,characteristicNotify
  static void writeNotifyUtils(
    GlobalWiFiBluetooth globalWiFiBluetooth,
    BluetoothDevice device, {
    Function? callback,
    Function? onceCallback,
  }) async {
    globalWiFiBluetooth.blueDeviceVoid(device);
    List<BluetoothService> services = [];
    SharedPreferences preferences = await SharedPreferences.getInstance();

    try {
      if (globalWiFiBluetooth.device!.isDisconnected) {
        await globalWiFiBluetooth.device!
            .connect(timeout: const Duration(milliseconds: 2500));
      }
      services = await globalWiFiBluetooth.device!.discoverServices();
    } catch (e) {
      //
    }
    if (services.isEmpty) {
      if (globalWiFiBluetooth.device!.isConnected) {
        await globalWiFiBluetooth.device!.disconnect();
      }

      // EasyLoading.showToast("bluetooth_connect_failed".tr, dismissOnTap: true);
      return;
    }
    await preferences.remove('list');
    for (var element in services) {
      if (element.uuid.toString() == "ca9e") {
        for (BluetoothCharacteristic characteristic
            in element.characteristics) {
          if (characteristic.properties.write) {
            globalWiFiBluetooth.characteristicWriteVoid(characteristic);
          }
          if (characteristic.properties.notify) {
            globalWiFiBluetooth.characteristicNotifyVoid(characteristic);
            if (characteristic.properties.notify ||
                characteristic.properties.indicate) {
              try {
                characteristic.setNotifyValue(true);
              } catch (e) {
                EasyLoading.showToast("");
              }
            }
          }

          if (globalWiFiBluetooth.characteristicWrite != null &&
              globalWiFiBluetooth.characteristicNotify != null) {
            EasyLoading.dismiss();
            if (callback != null) {
              callback();
            }
          }
        }
      }
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值