附近的连接:无法将有效负载发送到多个设备

时间:2019-02-10 22:42:16

标签: android android-bluetooth google-nearby

我无法创建多人聊天,附近的2个以上(最多15个)Android用户可以加入聊天室。

startMeshNetwork()开始宣告连接并发现。 在onCreate()中以及在成功连接的回调中都将调用它。这样一来,设备将继续加入附近的所有设备。

public void startMeshNetwork(){
    Nearby.getConnectionsClient(this)
            .startAdvertising(
                    /* endpointName= */ "Name here",
                    /* serviceId= */ "ID here",
                    mConnectionLifecycleCallback,
                    new AdvertisingOptions(com.google.android.gms.nearby.connection.Strategy.P2P_CLUSTER));

    Nearby.getConnectionsClient(this)
            .startDiscovery(
                    /* serviceId= */ "ID here",
                    new EndpointDiscoveryCallback() {
                        @Override
                        public void onEndpointFound(String endpointId, DiscoveredEndpointInfo info) {                       Nearby.getConnectionsClient(getApplicationContext())
                                    .requestConnection(
                                            /* endpointName= */ "Name here",
                                            endpointId,
                                            mConnectionLifecycleCallback);
                            endpoints.add(endpointId);
                        }

                        @Override
                        public void onEndpointLost(String endpointId) {
                            startMeshNetwork();
                        }
                    },
                    new DiscoveryOptions(com.google.android.gms.nearby.connection.Strategy.P2P_CLUSTER));

}

将有效载荷发送到所有连接的设备的代码段

Payload payload = Payload.fromBytes(pendingmessage.getBytes());
for(String endpointId:endpoints)
            Nearby.getConnectionsClient(getApplicationContext()).sendPayload(endpointId, payload);

正在使用这种方法建立连接,但是,有效载荷发送不一致,并且仅发送到一个设备。

0 个答案:

没有答案
相关问题