Flutter-发现局域网中的设备

前言

现在有一个需求:要能够获取到局域网中的遮阳帘设备。通过搜索发现flutter_mdns_plugin可以满足这个需求

Pub:flutter_mdns_plugin | Flutter package

GitHub:https://github.com/terrabythia/flutter_mdns_plugin

MDNS服务类型

要根据不同的MDNS服务类型来发现对应的设备

服务类型参考:mDNS的服务类型

全部代码

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| import 'package:flutter/material.dart'; import 'package:flutter_mdns_plugin/flutter_mdns_plugin.dart'; class MyApp1 extends StatefulWidget { const MyApp1({super.key}); @override State<MyApp1> createState() => _MyApp1State(); } class _MyApp1State extends State<MyApp1> { List<String> devices = []; bool isScanning = false; List<String> messageLog = <String>[]; //设备扫描函数 Future<void> scanDevices() async { setState(() { isScanning = true; devices.clear(); }); const String serviceType = '_http._tcp'; DiscoveryCallbacks discoveryCallbacks = DiscoveryCallbacks( onDiscovered: (ServiceInfo info) { print("Discovered ${info.toString()}"); }, onDiscoveryStarted: () { print("Discovery started"); }, onDiscoveryStopped: () { print("Discovery stopped"); }, onResolved: (ServiceInfo info) { print("Resolved Service ${info.toString()}"); setState(() { devices.add(info.toString()); }); }, ); final mdnsPlugin = FlutterMdnsPlugin(discoveryCallbacks: discoveryCallbacks); try { await mdnsPlugin.startDiscovery(serviceType); await Future.delayed(const Duration(seconds: 1)); // 扫描5秒钟 await mdnsPlugin.stopDiscovery(); } catch (e) { print('Error during device scan: $e'); } setState(() { isScanning = false; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Device Scanner'), ), body: Column( children: [ ElevatedButton( onPressed: isScanning ? null : scanDevices, child: const Text('Scan Devices'), ), const SizedBox(height: 16), if (isScanning) const CircularProgressIndicator() else if (devices.isEmpty) Text('No devices found.') else Expanded( child: ListView.builder( itemCount: devices.length, itemBuilder: (context, index) { return ListTile( title: Text(devices[index]), ); }, ), ), ], ), ); } } |

相关推荐
_阿南_5 小时前
flutter 展示的字体突然奔放了
android·flutter·ios
GitLqr8 小时前
玩转 Dart typedef:不仅是函数别名那么简单
flutter·面试·dart
年小个大1 天前
受 go-zero 启发,我给 Flutter 写了一套 CLI 脚手架
android·flutter·架构
GitLqr1 天前
Flutter 居然能直接跑在 Apple Watch 上了?玩转 flutter-watchos
flutter·全栈·apple watch
你听得到111 天前
排查 App 问题:别只盯着报错,把前后发生的事情串起来
android·前端·flutter
天空之城--2 天前
近一周Android Flutter行业动态与实用参考
android·flutter
GitLqr2 天前
玩转 Flutter 项目依赖:从 pubspec.yaml 到实战包管理
flutter·面试·全栈
程序员老刘2 天前
Qwen 3.8 max干了20分钟没干完,免费模型3分17秒搞定,问题出在哪?
flutter·ai编程
飞凌嵌入式2 天前
告别多端重复开发:Buildroot+Flutter实现全平台UI风格统一
flutter·ui
FungLeo2 天前
Flutter 带 TTL 的多级缓存设计:内存+磁盘+网络三层实战
网络·flutter·缓存·性能优化