smart_device_core 0.3.7 copy "smart_device_core: ^0.3.7" to clipboard
smart_device_core: ^0.3.7 copied to clipboard

Flutter Plugin SDK named smart_device_core

example/lib/main.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:smart_device_core/bind_core.dart';
import 'package:smart_device_core/smart_device_core.dart';
import 'package:smart_device_core/models/callback.dart';
import 'package:smart_device_core/models/smart_device_core.dart';
import 'package:smart_device_core_example/pages/login/login_page.dart';
import 'package:smart_device_core_example/services/auth_service.dart';
import 'home.dart';

/// 全局 navigatorKey,用于在非 widget 上下文中跳转(如 token 过期)
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

/// SDK初始化完成的 Completer,用于等待 initSDK 回调
final Completer<bool> sdkInitCompleter = Completer<bool>();

/// SDK login 完成后才响应 onAccountInfoError
/// login 内部会先打 US 再 queryNode 到 EU,过程中会触发 onAccountInfoError,需要忽略
bool sdkLoginComplete = false;

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

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

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      home: const SplashPage(),
      routes: {
        '/login': (context) => const LoginPage(),
        '/home': (context) => const HomePage(),
      },
      builder: EasyLoading.init(),
    );
  }
}

/// 启动页:初始化SDK(等回调完成) → 检查token → 导航
class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    _initAndCheck();
  }

  Future<void> _initAndCheck() async {
    // 初始化SDK并等待回调完成
    _initSDK();
    await sdkInitCompleter.future;
    debugPrint('[Splash] SDK初始化完成,检查token');

    // 检查token
    final hasToken = await AuthService.instance.restoreToken();
    if (!mounted) return;

    if (hasToken) {
      Navigator.pushReplacementNamed(context, '/home');
    } else {
      Navigator.pushReplacementNamed(context, '/login');
    }
  }

  void _initSDK() {
    final callback = Callback(
      onSuccess: (code, msg, data) {
        debugPrint('[SDK] 初始化成功');
        if (!sdkInitCompleter.isCompleted) {
          sdkInitCompleter.complete(true);
        }
      },
      onError: (code, message) {
        debugPrint('[SDK] 初始化失败 $message');
        if (!sdkInitCompleter.isCompleted) {
          sdkInitCompleter.complete(false);
        }
      },
    );

    InitSDKConfig config = InitConfigBuilder()
        .setTenantId('vicoo')
        .setCountryNo('GB')
        .setLanguage('zh-Hans')
        .setIsDebug(true)
        .setLoggerDelegate(LoggerDelegate(
            debug: (tag, message) => {
                  debugPrint('[SDK] debug $tag: $message'),
                },
            info: (tag, message) => {
                  debugPrint('[SDK] info $tag: $message'),
                },
            warning: (tag, message) => {
                  debugPrint('[SDK] warning $tag: $message'),
                },
            error: (tag, message) => {
                  debugPrint('[SDK] error $tag: $message'),
                }))
        .setAccountChangeListener(AccountChangeListener(
            onAccountInfoError: (status) => {
                  debugPrint('[SDK] onAccountInfoError status: $status, sdkLoginComplete=$sdkLoginComplete'),
                  if (sdkLoginComplete) { handleTokenExpired() },
                }))
        .build();

    SmartDeviceCore.getInstance()?.initSDK(config, callback);

    // 提前调用蓝牙初始化
    BindCore.getInstance()?.bleInit();
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(child: CircularProgressIndicator()),
    );
  }
}

/// 全局token过期处理
void handleTokenExpired() async {
  sdkLoginComplete = false;
  await AuthService.instance.clearToken();
  EasyLoading.showError('登录已过期,请重新登录');
  navigatorKey.currentState?.pushNamedAndRemoveUntil(
    '/login',
    (route) => false,
  );
}
1
likes
85
points
114
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter Plugin SDK named smart_device_core

Homepage

License

unknown (license)

Dependencies

flutter, json_annotation, plugin_platform_interface, uuid

More

Packages that depend on smart_device_core

Packages that implement smart_device_core