设置无法连接的Android Wi-Fi P2P本地服务

时间:2019-03-28 14:28:32

标签: android android-wifi wifi-direct wifimanager wifip2p

我想使用一些dns记录设置Android Wi-Fi P2P本地服务,而我已经做到了。我的应用程序仅需要这些记录,而无需在同级之间建立连接。但是我面临一个问题,其他Android用户可以看到该服务,他们可以尝试连接到我的应用程序的服务。我的应用程序的用户将看到接受对话框:“接受共享请求?”这很烦人。有什么办法可以使我的服务无法访问或断开连接,然后显示接受对话框或进行密码验证(例如在wifi热点中)?谢谢。

private WifiP2pManager manager;
private WifiP2pManager.Channel channel;
private WifiP2pDnsSdServiceInfo serviceInfo;
private final int PERIOD = 5000;
private String Tag = "WifiP2PService";
String SERVICE_INSTANCE = "P2p_Service";
private WifiP2pDnsSdServiceRequest serviceRequest;

public WifiP2PService(Context ctx){
    manager = (WifiP2pManager) ctx.getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(ctx, ctx.getMainLooper(), null);
    serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
    Map record = new HashMap();
    record.put("user_id", ""+(int)(Math.random() * 1000));
    serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_INSTANCE, "_http._tcp", record);
    manager.setDnsSdResponseListeners(channel, null, new WifiP2pManager.DnsSdTxtRecordListener() {
        @Override
        public void onDnsSdTxtRecordAvailable(String fullDomainName, Map<String, String> txtRecordMap, WifiP2pDevice srcDevice) {
            Log.i(Tag, "Finded: "+txtRecordMap.get("user_id"));
        }
    });
}

public void startBroadcastingService(){
    manager.clearLocalServices(channel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            manager.addLocalService(channel, serviceInfo,
                    new WifiP2pManager.ActionListener() {
                        @Override
                        public void onSuccess() {
                            Log.i(Tag, "Started");
                            setWifiDirectName("WifiP2p Android");
                        }

                        @Override
                        public void onFailure(int error) {
                            Log.i(Tag, "Failed to start");
                        }
                    });
        }

        @Override
        public void onFailure(int error) {
            Log.i(Tag, "Failed to clean");
        }
    });
}

public void startSearching() {
    manager.removeServiceRequest(
            channel,
            serviceRequest,
            new WifiP2pManager.ActionListener() {
                @Override
                public void onSuccess() {
                    manager.addServiceRequest(channel, serviceRequest, new WifiP2pManager.ActionListener() {
                                @Override
                                public void onSuccess() {
                                    manager.discoverServices(channel, new WifiP2pManager.ActionListener() {
                                        @Override
                                        public void onSuccess() {

                                        }
                                        @Override
                                        public void onFailure(final int error) {
                                        }
                                    });
                                }
                                @Override
                                public void onFailure(final int reason) {
                                }
                            }
                    );
                }
                @Override
                public void onFailure(final int reason) {
                }
            }
    );
}

public void setWifiDirectName(final String deviceName) {
    try {
        final Class[] paramTypes = new Class[3];
        paramTypes[0] = WifiP2pManager.Channel.class;
        paramTypes[1] = String.class;
        paramTypes[2] = WifiP2pManager.ActionListener.class;
        final Method setDeviceName = manager.getClass().getMethod(
                "setDeviceName", paramTypes);
        setDeviceName.setAccessible(true);

        final Object[] arglist = new Object[3];
        arglist[0] = channel;
        arglist[1] = deviceName;
        setDeviceName.invoke(manager, arglist);
    } catch (final Exception e) {
        e.printStackTrace();
    }
}

public void stopLocalService() {
    Log.i(Tag, "Stopped");
    manager.clearLocalServices(channel, null);
    manager.removeServiceRequest(channel, serviceRequest, null);
}

0 个答案:

没有答案