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

相关推荐
小蜜蜂嗡嗡10 小时前
【flutter列表播放器】
flutter
AiFlutter10 小时前
Flutter Web部署到子路径的打包指令
flutter
有趣的杰克10 小时前
Flutter InkWell组件去掉灰色遮罩
开发语言·javascript·flutter
Python私教10 小时前
Flutter动画容器
flutter
wills7771 天前
Flutter 状态管理框架Get
flutter·react native
阳仔_1001 天前
动态下发字体技术方案
flutter
Rudy10211 天前
分享我在flutter中使用的MVVM框架 - 2
前端·flutter
恋猫de小郭1 天前
什么?Flutter 又要凉了? Flock 是什么东西?
flutter
lqj_本人1 天前
<大厂实战场景> ~ Flutter&鸿蒙next 解析后端返回的 HTML 数据详解
flutter·华为·架构·harmonyos·1024程序员节
MavenTalk1 天前
前端跨平台开发常见的解决方案
前端·flutter·react native·reactjs·weex·大前端