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

相关推荐
好的佩奇13 小时前
Dart 之 JSON
android·flutter·dart
恋猫de小郭1 天前
站起来,Flutter 要支持真正的 3D Texture 了
android·前端·flutter
黑科技iOS上架2 天前
iOS4.3合规自测手段:二进制分析
flutter·ios·cocoa·审核
好的佩奇3 天前
蓝牙之 GATT 数据模型
android·flutter·蓝牙
王二蛋与他的张大花4 天前
后台内存 827MB,我一行业务逻辑都没改就砍到 100MB
android·flutter·性能优化
恋猫de小郭4 天前
Flutter 的FFI/JNI 插件终于能区分不同的 FlutterEngine 了
android·前端·flutter
晚霞的不甘5 天前
Flutter/OpenHarmony 三方库适配实战:google_maps_flutter 地图
flutter·macos·华为·cocoa·harmonyos·1024程序员节
恋猫de小郭5 天前
Flutter HC 旋转 ANR,而且还和之前的 HCPP 问题类似
android·前端·flutter
恋猫de小郭6 天前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
程序员老刘7 天前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端