我无法连接2个以上的设备

时间:2019-09-10 04:28:55

标签: android connection google-play-services p2p multipeer-connectivity

我正在创建一个使用诸如服务器之类的设备的应用程序,为此,有几台设备连接到该服务器。我正在使用Google的附近的连接A​​PI,我已经阅读了文档并做了她解释的步骤,但是当我放置设备时要使用startAdvertising()和其他的startDiscovery(),只有第一个设备已连接,而其他设备从未连接,我不知道是否被误解了,但文档表明它们可以继续将设备连接到服务器。 startAdvertising()是活动附加的我的代码,我的应用程序旨在创建一个星形网络,并且服务器向所有连接的设备发送广播,我如何将多个客户端连接到服务器?我感谢任何帮助或示例代码。

private void startAdvertising() {
    AdvertisingOptions advertisingOptions = new AdvertisingOptions.Builder().setStrategy(STRATEGY).build();
    Nearby.getConnectionsClient(context).startAdvertising(
                    nick.getText().toString(), SERVICE_ID, connectionLifecycleCallback, advertisingOptions)
            .addOnSuccessListener(
                    new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void unused) {
                            // We're advertising!
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            // We were unable to start advertising.
                        }
                    });
}

private void startDiscovery() {
    DiscoveryOptions discoveryOptions =
            new DiscoveryOptions.Builder().setStrategy(STRATEGY).build();
    Nearby.getConnectionsClient(context)
            .startDiscovery(SERVICE_ID, endpointDiscoveryCallback, discoveryOptions)
            .addOnSuccessListener(
                    new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void unused) {
                            // We're discovering!
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            // We're unable to start discovering.
                        }
                    });
}

private final EndpointDiscoveryCallback endpointDiscoveryCallback =
        new EndpointDiscoveryCallback() {
            @Override
            public void onEndpointFound(String endpointId, DiscoveredEndpointInfo info) {
                // An endpoint was found. We request a connection to it.
                Nearby.getConnectionsClient(context)
                        .requestConnection(nick.getText().toString(), endpointId, connectionLifecycleCallback)
                        .addOnSuccessListener(
                                new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void unused) {
                                        // We successfully requested a connection. Now both sides
                                        // must accept before the connection is established.
                                    }
                                })
                        .addOnFailureListener(
                                new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        // Nearby Connections failed to request the connection.
                                    }
                                });
            }

            @Override
            public void onEndpointLost(String endpointId) {
                // A previously discovered endpoint has gone away.
            }
        };

private final ConnectionLifecycleCallback connectionLifecycleCallback =
        new ConnectionLifecycleCallback() {
            @Override
            public void onConnectionInitiated(String endpointId, ConnectionInfo connectionInfo) {
                // Automatically accept the connection on both sides.
                Nearby.getConnectionsClient(context).acceptConnection(endpointId, payloadCallback);
                nom=connectionInfo.getEndpointName();
            }

            @Override
            public void onConnectionResult(String endpointId, ConnectionResolution result) {
                switch (result.getStatus().getStatusCode()) {
                    case ConnectionsStatusCodes.STATUS_OK:
                        // We're connected! Can now start sending and receiving data.
                        //toEndpointId1.add(endpointId);
                        toEndpointId=endpointId;
                        Toast.makeText(getApplicationContext(),"Conectado con "+nom,Toast.LENGTH_SHORT).show();
                        break;
                    case ConnectionsStatusCodes.STATUS_CONNECTION_REJECTED:
                        // The connection was rejected by one or both sides.
                        break;
                    case ConnectionsStatusCodes.STATUS_ERROR:
                        // The connection broke before it was able to be accepted.
                        break;
                    default:
                        // Unknown status code
                }
            }

            @Override
            public void onDisconnected(String endpointId) {
                // We've been disconnected from this endpoint. No more data can be
                // sent or received.
            }

0 个答案:

没有答案