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]), ); }, ), ), ], ), ); } } |

相关推荐
恋猫de小郭21 小时前
Android CLI 支持 AI Agent 通过 Device Streaming 调试云真机
android·前端·flutter
m0_738185822 天前
Flutter 鸿蒙化实战:flutter_scankit 适配 OpenHarmony,华为 ScanKit 扫码
flutter·华为·harmonyos·鸿蒙
事圆则缓2 天前
Flutter 开发鸿蒙实战:从 OpenHarmony 适配版到 HAP 构建与插件接入
flutter·华为·harmonyos
天空之城--2 天前
Android一周动态:Android 18首次官宣、Compose Material3 1.4转正(5趋势+5资讯)
android·人工智能·flutter·架构·android jetpack
恋猫de小郭2 天前
Meta 分享怎么用 AI 迁移 Compose 项目不烧心
android·前端·flutter
EatFan2 天前
Flutter 鸿蒙适配实践 2026:从环境搭建、混合渲染到 OpenTelemetry 性能基建
flutter·华为·harmonyos·openharmony·opentelemetry·鸿蒙适配·impeller
m0_738185822 天前
Flutter 鸿蒙化实战:flutter_secure_storage 适配 OpenHarmony,敏感数据安全存储
安全·flutter·华为·harmonyos·鸿蒙
传奇开心果编程3 天前
【现代声明式UI学与练】第1课 从命令式UI到声明式UI
学习·flutter·ui·swiftui·react·android jetpack
m0_738185823 天前
Flutter 鸿蒙化实战:flutter_nfc_kit 适配 OpenHarmony,NFC 读写一步到位
flutter·华为·harmonyos·鸿蒙
恋猫de小郭3 天前
KMP 又改了编译流程, Separate Compilation 禁止了 `commonMain` 的依赖穿透
android·前端·flutter