faithoflifedev / easy_onvif_workspace

This package works with a variety of ONVIF compatible devices allowing for IP Cameras and NVRs (network video recorders) to be integrated into Dart and Flutter applications.
33 stars 21 forks source link

iOS || Unable to Discover Device #70

Closed kanojiyasmit closed 2 days ago

kanojiyasmit commented 2 days ago

Hi @faithoflifedev , The application fails to send UDP packets due to a “No route to host” error during device discovery.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: SocketException: Send failed (OS Error: No route to host, errno = 65), address = 0.0.0.0, port = 0
#0      _NativeSocket.send (dart:io-patch/socket_patch.dart:1275:34)
#1      _RawDatagramSocket.send (dart:io-patch/socket_patch.dart:2590:15)
#2      MulticastProbeIo.start (package:easy_onvif/src/platform/multicast_probe_io.dart:160:23)
#3      MulticastProbeIo.probe (package:easy_onvif/src/platform/multicast_probe_io.dart:144:5)
<asynchronous suspension>
#4      _MyHomePageState.fetchDevice (package:onvif_discovery_hub/main.dart:37:5)
<asynchronous suspension>
#5      _FutureBuilderState._subscribe.<anonymous closure> (package:flutter/src/widgets/async.dart:638:31)
<asynchronous suspension>
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter OnVif Camera Demo',
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    fetchDevice();
  }

  Future<List<ProbeMatch>> fetchDevice() async {
    final multicastProbe = MulticastProbe();
    await multicastProbe.probe();
    return multicastProbe.onvifDevices;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.grey[200],
      ),
      backgroundColor: Colors.grey[200],
      body: Center(
        child: FutureBuilder(
          future: fetchDevice(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    CircularProgressIndicator(),
                    SizedBox(height: 16),
                    Text(
                      'Scanning network to find out ONVIF devices',
                      style: TextStyle(fontSize: 16),
                    ),
                  ],
                ),
              );
            } else if (snapshot.hasError) {
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Icon(
                    Icons.error,
                    color: Colors.red,
                    size: 48,
                  ),
                  const SizedBox(height: 16),
                  Text(
                    'An error occurred: ${snapshot.error}',
                    style: const TextStyle(fontSize: 16, color: Colors.red),
                    textAlign: TextAlign.center,
                  ),
                ],
              );
            } else if (snapshot.hasData && snapshot.data!.isNotEmpty) {
              final devices = snapshot.data!;
              return ListView.builder(
                itemCount: devices.length,
                itemBuilder: (context, index) {
                  final device = devices[index];
                  return Card(
                    margin:
                        const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                    elevation: 4,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: ListTile(
                      contentPadding: const EdgeInsets.all(16),
                      leading: const Icon(
                        Icons.devices,
                        size: 40,
                        color: Colors.blue,
                      ),
                      title: Text(
                        device.hardware,
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      subtitle: Text(
                        device.xAddr,
                        style: const TextStyle(
                          fontSize: 14,
                          color: Colors.grey,
                        ),
                      ),
                      trailing: const Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.grey,
                      ),
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => DeviceDetails(device: device),
                          ),
                        );
                      },
                    ),
                  );
                },
              );
            } else {
              return const Text("No devices found.");
            }
          },
        ),
      ),
    );
  }
}
faithoflifedev commented 2 days ago

Hi @kanojiyasmit,

There are several issues with your code that are preventing it from operating as you expect. This is not a problem with the package itself. I recommend posting to a forum that provides help with Flutter and Dart coding.

kanojiyasmit commented 1 day ago

Hi @faithoflifedev Thanks for quick reply, i am checking several times of my code but not getting any clue can you help me on this ? What i am doing wrong? Also i am using your package and example everything works perfectly on Android but from iOS side i am not able to discover. Your help will be appreciated.

faithoflifedev commented 2 hours ago
import 'package:easy_onvif/probe.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Onvif Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blueGrey,
      ),
      home: MyHomePage(title: 'Onvif Flutter Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final multicastProbe = MulticastProbe(releaseMode: kReleaseMode);

  final String title;

  MyHomePage({
    super.key,
    required this.title,
  });

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final multicastProbe = MulticastProbe(releaseMode: kReleaseMode);

  Future<List<ProbeMatch>> fetchDevice() async {
    await multicastProbe.probe();

    return multicastProbe.onvifDevices;
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.grey[200],
      ),
      backgroundColor: Colors.grey[200],
      body: Center(
        child: FutureBuilder(
          future: fetchDevice(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    CircularProgressIndicator(),
                    SizedBox(height: 16),
                    Text(
                      'Scanning network to find out ONVIF devices',
                      style: TextStyle(fontSize: 16),
                    ),
                  ],
                ),
              );
            } else if (snapshot.hasError) {
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Icon(
                    Icons.error,
                    color: Colors.red,
                    size: 48,
                  ),
                  const SizedBox(height: 16),
                  Text(
                    'An error occurred: ${snapshot.error}',
                    style: const TextStyle(fontSize: 16, color: Colors.red),
                    textAlign: TextAlign.center,
                  ),
                ],
              );
            } else if (snapshot.hasData && snapshot.data!.isNotEmpty) {
              final devices = snapshot.data!;
              return ListView.builder(
                itemCount: devices.length,
                itemBuilder: (context, index) {
                  final device = devices[index];
                  return Card(
                    margin:
                        const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                    elevation: 4,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: ListTile(
                      contentPadding: const EdgeInsets.all(16),
                      leading: const Icon(
                        Icons.devices,
                        size: 40,
                        color: Colors.blue,
                      ),
                      title: Text(
                        device.hardware,
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      subtitle: Text(
                        device.xAddr,
                        style: const TextStyle(
                          fontSize: 14,
                          color: Colors.grey,
                        ),
                      ),
                      trailing: const Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.grey,
                      ),
                      onTap: () {
                        // Navigator.push(
                        //   context,
                        //   MaterialPageRoute(
                        //     builder: (context) => DeviceDetails(device: device),
                        //   ),
                        // );
                      },
                    ),
                  );
                },
              );
            } else {
              return const Text("No devices found.");
            }
          },
        ),
      ),
    );
  }
}
kanojiyasmit commented 1 hour ago

Hi @faithoflifedev ,

Thank you for the correction. I tried this approach, but I’m still encountering the same error on the iOS side.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: SocketException: Send failed (OS Error: No route to host, errno = 65), address = 0.0.0.0, port = 0
#0      _NativeSocket.send (dart:io-patch/socket_patch.dart:1275:34)
#1      _RawDatagramSocket.send (dart:io-patch/socket_patch.dart:2590:15)
#2      MulticastProbeIo.start (package:easy_onvif/src/platform/multicast_probe_io.dart:160:23)
#3      MulticastProbeIo.probe (package:easy_onvif/src/platform/multicast_probe_io.dart:144:5)
<asynchronous suspension>
#4      _MyHomePageState.fetchDevice (package:onvif_discovery_hub/main.dart:41:5)
<asynchronous suspension>
#5      _FutureBuilderState._subscribe.<anonymous closure> (package:flutter/src/widgets/async.dart:638:31)
<asynchronous suspension>

I’ve already added Local Network Permission and App Transport Security (ATS) configurations. Is there any other capability or configuration required for iOS to make this work?