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

相关推荐
tangweiguo030519872 小时前
Flutter 中打开网络图片的完整指南
flutter
西西学代码2 小时前
Flutter---BLE设备通信
flutter
程序员Ctrl喵3 小时前
状态管理与响应式编程 —— 驾驭复杂应用的“灵魂工程”
开发语言·flutter·ui·架构
始持4 小时前
第十五讲 本地存储
前端·flutter
始持4 小时前
第十四讲 网络请求与数据解析
前端·flutter
啥都想学点4 小时前
第14天:Flutter 打造质感拉满的轮播图
flutter
tangweiguo030519875 小时前
Flutter 深潜:当动态 List 遇上 JSON 序列化,如何优雅解决?
flutter
恋猫de小郭5 小时前
Flutter 的 build_runner 已经今非昔比,看看 build_runner 2.13 有什么特别?
android·前端·flutter
小白学鸿蒙1 天前
使用Flutter从0到1构建OpenHarmony/HarmonyOS应用
flutter·华为·harmonyos
不爱吃糖的程序媛1 天前
Flutter OH 框架介绍
flutter