witsystem_device_plugin 0.0.1 copy "witsystem_device_plugin: ^0.0.1" to clipboard
witsystem_device_plugin: ^0.0.1 copied to clipboard

outdated

A new Flutter project.

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:witsystem_device_plugin/device/Unlock.dart';
import 'package:witsystem_device_plugin/device/BleDevice.dart';
import 'package:witsystem_device_plugin/device/AddDevice.dart';
import 'package:witsystem_device_plugin/command/PasswordType.dart';
import 'package:witsystem_device_plugin/command/MarkType.dart';
import 'package:witsystem_device_plugin/command/CardType.dart';
import 'package:witsystem_device_plugin/witsystem_device_plugin.dart';

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
    init();
  }

  init() async {
    var millisecondsSinceEpoch = DateTime.now().millisecondsSinceEpoch;
    var initSDK = await WitsystemDevicePlugin.initSDK(
        "XXXX",
        "XXXX",
        "XXXXXX",
        "");

    var millisecondsSinceEpoch2 = DateTime.now().millisecondsSinceEpoch;
    print(
        "初始化返回的<><><><><><><><><><><><>数据${millisecondsSinceEpoch2 - millisecondsSinceEpoch} ");
    //print("返回的<><><><><><><><><><><><>数据 $initSDK ");
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await WitsystemDevicePlugin.platformVersion ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  late StreamSubscription listen;

  //扫描
  scan() {
    listen = AddDevice.instance.scanAddBleDevice().listen((event) {
      print("返回的信息$event");
    });
    listen.onError((err) {
      var err2 = err as PlatformException;
      print("异常信息${err2}");
      listen.cancel();
    });
  }

  stopScan() {
    AddDevice.instance.stopAddBleDevice(listen);
  }

  addPwd() async {
    try {
      var addPassword = await BleDevice.instance.addPassword(
          "Slock383B26345B28",
          PasswordType.ADMIN_PASSWORD,
          "654321",
          "flutter添加密码");
      print("添加成功$addPassword");
    } catch (e) {
      print("错误信息${e as PlatformException}");
    }
  }

  addCard() async {
    try {
      var addPassword = await BleDevice.instance
          .addCard("Slock383B26345B28", CardType.ADMIN_CARD, "flutter添加卡");
      print("添加成功$addPassword");
    } catch (e) {
      print("错误信息${e as PlatformException}");
    }
  }

  addMark() async {
    BleDevice.instance
        .addMark("Slock383B26345B28", MarkType.ADMIN_MARK, "flutter添加指纹",
            progress: (info) {
      print("添加指纹进度$info");
    }, commandError: (error) {
      print("指令异常$error");
    }, fail: (error) {
      print("设备错误信息$error");
    }, sendSuccess: () {
      print("发送成功");
    }, success: (msg) {
      print("添加成功$msg");
    });
    // BleDevice.instance.addMark("Slock383B26345B28", MarkType.ADMIN_MARK, "flutter添加指纹").listen((event) {
    //   print("添加指纹返回$event");
    //
    // }).onError((err){
    //   print("添加指纹错误信息${err as PlatformException}");
    // });
  }

  closeBleDevice() {
    BleDevice.instance.closeBleDevice("Slock383B26345B28");
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Center(
              child: Text('Running on: $_platformVersion\n'),
            ),
            Center(
              child: MaterialButton(
                textColor: Colors.red,
                child: const Text("开锁"),
                onPressed: () {
                  var microsecondsSinceEpoch =
                      DateTime.now().millisecondsSinceEpoch;
                  //var map = await WitsystemDevicePlugin.bleUnlock();
                  Unlock.instance.bleUnlocks(success: (map) {
                    print("开门成功$map");
                  }, foundMultipleDevices: (map) {
                    print("发现附近有多个设备$map");
                  }, fail: (e) {
                    print("开启设备失败$e");
                  });
                  var microsecondsSinceEpoch2 =
                      DateTime.now().millisecondsSinceEpoch;
                  print(
                      "开锁耗时${microsecondsSinceEpoch2 - microsecondsSinceEpoch}");
                },
              ),
            ),
            MaterialButton(
              onPressed: scan,
              child: const Text("扫描可以添加的设备"),
            ),
            MaterialButton(
              onPressed: stopScan,
              child: const Text("停止扫描附近设备"),
            ),
            MaterialButton(
              onPressed: addPwd,
              child: const Text("添加密码123456"),
            ),
            MaterialButton(
              onPressed: addCard,
              child: const Text("添加卡"),
            ),
            MaterialButton(
              onPressed: addMark,
              child: const Text("添加指纹"),
            ),
            MaterialButton(
              onPressed: closeBleDevice,
              child: const Text("断开设备蓝牙操作"),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
0
points
4
downloads

Publisher

unverified uploader

Weekly Downloads

A new Flutter project.

License

unknown (license)

Dependencies

flutter

More

Packages that depend on witsystem_device_plugin

Packages that implement witsystem_device_plugin