1

我正在构建一个应用程序,它应该使用附近的连接 api 连接至少 4 个设备。我可以间歇性地连接它们,否则只有两个设备正在连接。

我正在使用 P2P-CLUSTER 拓扑并将数据作为已成功发送的文件有效负载发送。我有两个问题:

  1. 关于如何在 2 个以上设备之间建立稳定连接的任何建议。
  2. 在将数据作为文件负载发送时,会在下载文件夹中创建一个文件夹。有没有办法放弃这一步,并且能够直接将数据作为文件有效负载发送,而无需在本地保存它们。

这是我仅关于连接部分的代码。

  private final EndpointDiscoveryCallback endpointDiscoveryCallback =
        new EndpointDiscoveryCallback() {
            @Override
            public void onEndpointFound(String endpointId, final DiscoveredEndpointInfo info) {

                arrlist.add(endpointId);
                for (int i = 0; i< arrlist.size(); i++) {
                    connectionsClient
                            .requestConnection(Build.MODEL, arrlist.get(i), connectionLifecycleCallback)
                            .addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                }
                            })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                }
                            });
                }
            }

            @Override
            public void onEndpointLost(@NonNull String endpointId) {
            }
        };


private final ConnectionLifecycleCallback connectionLifecycleCallback =
        new ConnectionLifecycleCallback() {
            @Override
            public void onConnectionInitiated(@NonNull String endpointId, @NonNull ConnectionInfo connectionInfo) {
                // Automatically accept the connection on both sides.
                connectionsClient.acceptConnection(endpointId, new PayloadCallback() {
                    @Override
                    public void onPayloadReceived(@NonNull String s, @NonNull Payload payload) {
                        }
                    }

                    @Override
                    public void onPayloadTransferUpdate(@NonNull String s, @NonNull PayloadTransferUpdate payloadTransferUpdate) {

            }

            @Override
            public void onConnectionResult(@NonNull String endpointId, ConnectionResolution result) {
                switch (result.getStatus().getStatusCode()) {
                    case ConnectionsStatusCodes.STATUS_OK:
                        if(arrlist != null && arrlist.contains(endpointId)){
                            System.out.println(TAG+ " End Point Found");
                        } else {
                            arrlist.add(endpointId);
                        }

                        connectionsClient.stopDiscovery();
                        connectionsClient.stopAdvertising();
                        break;
                    case ConnectionsStatusCodes.STATUS_CONNECTION_REJECTED:
                        // Some code 
                        break;
                    case ConnectionsStatusCodes.STATUS_ERROR:
                        // Some code
                        break;
                    default:
                }
            }

            @Override
            public void onDisconnected(@NonNull String endpointId) {
             // some code
            }
        };

  private void startAdvertising() {
    AdvertisingOptions advertisingOptions =
            new AdvertisingOptions.Builder().setStrategy(STRATEGY).build();
    Nearby.getConnectionsClient(context)
            .startAdvertising(
                    android.os.Build.MODEL, getPackageName(), connectionLifecycleCallback, advertisingOptions)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {

                }
            })
            .addOnFailureListener(new OnFailureListener() {
                                      @Override
                                      public void onFailure(@NonNull Exception e) {

                                      }
                                  });
}

private void startDiscovery() {
    DiscoveryOptions discoveryOptions =
            new DiscoveryOptions.Builder().setStrategy(STRATEGY).build();
    Nearby.getConnectionsClient(context)
            .startDiscovery(getPackageName(), endpointDiscoveryCallback, discoveryOptions)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                                      @Override
                                      public void onSuccess(Void aVoid) {
                                      }
                                  })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                }
            });
}
4

0 回答 0