ksenia312 / nearby_service

Nearby Service Flutter Plugin for creating connections in a P2P network.
https://pub.dev/packages/nearby_service
BSD 3-Clause "New" or "Revised" License
45 stars 4 forks source link

[QUESTION]: Callback to accept incoming connection invites #15

Closed Princewil closed 1 month ago

Princewil commented 1 month ago

Question:

I've noticed an issue on iOS where devices set as Advertisers (iOSNearbyService.ios?.setIsBrowser(value: false)) are unable to see Browser devices (iOSNearbyService.ios?.setIsBrowser(value: true)) via the iOSNearbyService.getPeersStream().

According to the iOSNearbyService.connectById(deviceId) documentation:

Note that the [NearbyIOSService] implementation invites or accepts invites depending on the [NearbyIOSService.isBrowserValue].

This suggests that when connectById is called, the Browser device sends an invite, and the Advertiser device needs to accept the invite for a connection to be established.

My question is: Where can I find the callback function on the Advertiser device that handles the connection request from the Browser device?

A prompt response would be greatly appreciated as I am in the middle of a project. Thank you!

Your effort is appreciated 💗

ksenia312 commented 1 month ago

Hello @Princewil! Method connectById sends request if this device is browser, and sends accept for request if this device is advertiser:

From source code:

  @override
  Future<bool> connectById(String deviceId) async {
    final result = _isBrowser.value
        ? await NearbyServiceIOSPlatform.instance.invite(deviceId)
        : await NearbyServiceIOSPlatform.instance.acceptInvite(deviceId);

Sorry for the confusion, I would be glad if you can suggest an improvement to better understand this aspect!

Princewil commented 1 month ago

Thanks for the response.

First, you are saying it's not a bug on why the advertising device don't get to see browsing devices??

Then secondly on your response for the connectById function. Assuming I have multiple devices as browsers how would the advertising device know the particular ID to use with the connectById callback to initiate a connections bearing in mind that these IDs are random values and can't be predetermined?

Princewil commented 1 month ago

Thanks for the response.

First, you are saying it's not a bug on why the advertising device don't get to see browsing devices??

Then secondly on your response for the connectById function. Assuming I have multiple devices as browsers how would the advertising device know the particular ID to use with the connectById callback to initiate a connections bearing in mind that these IDs are random values and can't be predetermined?

ksenia312 commented 1 month ago

@Princewil Ya, advertising device don't get to see browsing devices, it see only invitations from them It is Multipeer Connectivity logic https://developer.apple.com/documentation/multipeerconnectivity/mcnearbyserviceadvertiser

About connectById: For iOS, the execution logic can only be that the advertiser accepts the browsers invitation. That is, browsers first see the advertiser and call connectById. Only after that, the advertiser sees the list of invitations (peers). Accordingly, it calls connectById as a response to a specific invitation.

Princewil commented 1 month ago

Thank you so much, it's well understood now I think this should be stated clearly on the readme file

Thanks once again for this wonderful package

ksenia312 commented 1 month ago

@Princewil Thanks for writing here! If you have any ideas on how to improve the API, I'm open to suggestions!

Princewil commented 1 month ago

Got it!💪