udp icon indicating copy to clipboard operation
udp copied to clipboard

Android receive udp broadcast from other device but fails sending its own broadcast

Open lgirolimetto opened this issue 2 years ago • 0 comments

Hi, I'm trying to send broadcast from Android phone to the network but it fails. Here is the code (the same code works on iOS devices and on Mac). Android device receive the broadcast from that devices, but other devices do not receive its broadcast. Maybe I'm missing something obvious but I cannot find what. Thanks in advance, Luca.

In the AndroidManifest I've set the following permissions:

SyncServer({ required int port, required this.broadcastPort, required this.onDataReceived, this.onClientConnected }) : endpoint = Endpoint.any(port: Port(port));

void start () { broadcaster = UDP.bind(endpoint); ServerSocket .bind(endpoint.address, endpoint.port!.value) .then((ServerSocket socket) { _socket = socket; _socket?.listen((client) { _handleConnection(client); }); }); }

void broadcast({required bool periodic, Duration? period, required String hiringMessage}) { broadcaster?.then( (value) { final packet = BasePacket(senderName: DeviceInfoService().getName(), command: 'hello', data: hiringMessage); final dataToSend = packet .toJson() .codeUnits; if (periodic) { _broadcastTimer = Timer.periodic( period ?? const Duration(seconds: 1), (timer) { value.send(dataToSend, Endpoint.broadcast(port: Port(broadcastPort))); } ); } else { value.send(dataToSend, Endpoint.broadcast(port: Port(broadcastPort))); } } ); }

lgirolimetto avatar Apr 02 '22 17:04 lgirolimetto