mannprerak2 / nearby_connections

Flutter plugin (android) for sharing bytes and files Offline, (Based on the android Nearby Connections API)
https://pub.dev/packages/nearby_connections
BSD 2-Clause "Simplified" License
93 stars 30 forks source link

onEndpointFound never gets called in dart code. Error : FlutterJNI(26633): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: nearby_connections. Response ID: 4 #75

Open rahulmaindargi opened 1 month ago

rahulmaindargi commented 1 month ago

Below is the error when discovery finds a device. but onEndpointFound on dart side never gets called.

NearbyConnections(26633): NFC discovery started. D/NearbyConnections(26633): Invalidating dispatch state. D/NearbyConnections(26633): Starting NFC dispatching. D/NearbyConnections(26633): Cannot dispatch NFC events. NFC is not supported. D/nearby_connections(26633): startDiscovery D/nearby_connections(26633): onEndpointFound W/FlutterJNI(26633): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: nearby_connections. Response ID: 4

mannprerak2 commented 1 month ago

Are you using this in a background service?

This package is not built in a way to be used inside a background service, and I'm not sure if it's even possible to do so.

rahulmaindargi commented 1 month ago

No I am not using as background service. but its using a separate isolate after connection is initialized.

I am working on a expense manager app , and want to sync/send data over nearby communication. I am using bloc cubit. I use separate isolate so that I can show progress of transfer over UI

To be exact, on bloc cubit call, I start nearby Discover, on endpoint found, I request connection, once connection is approved onConnectionInitiated I stop discovery, then then try to send data separate isolate.

but code never reaches upto isolate part. on on endpoint found callback never gets called and in console I get above error message.

rahulmaindargi commented 1 month ago

I have no experience of plugin/package development, but from stackoverflow

As per first answer, root cause appears to be using the wrong (or old) method channel. inside plugin/package code. so raised the issue here.

rahulmaindargi commented 1 month ago

if it helps here is my cubit.sendDatamethod has below code

await Nearby().startDiscovery(
        configId,
        Strategy.P2P_POINT_TO_POINT, // https://developers.google.com/nearby/connections/strategies
        onEndpointFound: (String id, String userName, String serviceId) {
          // called when an advertiser is
          logger.d("onEndpointFound $id $userName $serviceId");
}
...

but execution never reaches logger line mentioned, it fails and gives above error.

mannprerak2 commented 1 month ago

I'm guessing you cannot use a separate isolate, all the nearby connection interaction including any callbacks must be done in the main isolate itself.

Try removing the isolate usage.

rahulmaindargi commented 1 month ago

but code doesn't even reach the isolate part. any isolate related code will come into execution only after onConnectionInitiated but ENdpoint found fails itself.

I will try commenting the actual business logic (that involves isolate) tomorrow and update here. but I am 99% sure, its not the cause for the issue.

Kindly check below stackoverflow thread once you get time.

https://stackoverflow.com/questions/61934900/tried-to-send-a-platform-message-to-flutter-but-flutterjni-was-detached-from-n

mannprerak2 commented 1 month ago

Method channel is not being cached. See https://github.com/mannprerak2/nearby_connections/blob/63104a9afc7a07699e67f2b91ab372e8556dd59c/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java#L449

I'm guessing you are somehow using an isolate to call the nearby connection function. To be sure you can try running the example app on your device, if that works it's probably because of isolate usage.

If you are still not sure, you can post a link to a minimal reproducible sample I might be able to check it out over the weekend.

rahulmaindargi commented 2 days ago

image

Sorry for so late reply.

I found that

bool a = await Nearby().startAdvertising(...); logger.d("Advertising started $a"); never completes the future i.e. logger line never gets executed neither any error.

rahulmaindargi commented 2 days ago

bool a = await Nearby().startAdvertising(...); logger.d("Advertising started $a"); never completes the future i.e. logger line never gets executed neither any error.

Sorry looks like flutter version upgrade messed with gradle big time. will check and let you know