Wifidirect Discovery没有找到同行

时间:2017-03-13 18:08:50

标签: java android-fragments bluetooth wifi-direct

我有一个wifidirect应用程序成功发现并连接到同行,我已经附加了#34;该应用程序到我正在开发的另一个应用程序。该应用程序应该通过Wifidirect同时控制一个带有蓝牙和通信的机器人,它有一个"播放按钮"当它被点击时,它转到另一个活动,该活动有两个片段,一个是第一个出现在"玩"单击按钮并强制用户连接到同伴(这是我附加了wifidirect应用程序),连接后转到第二个片段,这个片段有游戏界面,它有所有的蓝牙部分(连接和控制机器人) )。

总结:第一个活动有一个播放按钮,单击按钮,它会转到带有两个片段的第二个活动。第二个活动,必须连接到Wifidirect的对等体,之后它必须用蓝牙连接和控制机器人。

在第二项活动中同时发生了WifiDirect和蓝牙通信(令人困惑......我知道)

我的问题:如果我只使用Wifidirect应用程序,它会成功搜索和连接,但需要使用" fusion"这两个应用程序,它只是永远搜索而没有发现任何东西。任何想法发生什么?也许是为了在同一活动中发生的许多事情或许多事情?如果我感到困惑,请告诉我。

我的Wifidiscovery活动:

public class WiFiServiceDiscoveryActivity extends Activity implements
    DeviceClickListener, Handler.Callback, MessageTarget,
    ConnectionInfoListener {

public static final String TAG = "wifidirectdemo";

// TXT RECORD properties
public static final String TXTRECORD_PROP_AVAILABLE = "available";
public static final String SERVICE_INSTANCE = "_wifidemotest";
public static final String SERVICE_REG_TYPE = "_presence._tcp";

public static final int MESSAGE_READ = 0x400 + 1;
public static final int MY_HANDLE = 0x400 + 2;
private WifiP2pManager manager;

static final int SERVER_PORT = 4545;

private final IntentFilter intentFilter = new IntentFilter();
private Channel channel;
private BroadcastReceiver receiver = null;
private WifiP2pDnsSdServiceRequest serviceRequest;

private Handler handler = new Handler(this);
private jogar chatFragment;
private WiFiDirectServicesList servicesList;

private TextView statusTxtView;
Button botaorecomecar;

public Handler getHandler() {
    return handler;
}

public void setHandler(Handler handler) {
    this.handler = handler;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    statusTxtView = (TextView) findViewById(R.id.status_text);
    botaorecomecar = (Button) findViewById(R.id.botaorecomecar);

    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    intentFilter
            .addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    intentFilter
            .addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);
    startRegistrationAndDiscovery();

    servicesList = new WiFiDirectServicesList();
    getFragmentManager().beginTransaction()
            .add(R.id.container_root, servicesList, "services").commit();

    botaorecomecar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }


    });}





@Override
protected void onRestart() {
    Fragment frag = getFragmentManager().findFragmentByTag("services");
    if (frag != null) {
        getFragmentManager().beginTransaction().remove(frag).commit();
    }
    super.onRestart();
}

@Override
protected void onStop() {
    if (manager != null && channel != null) {
        manager.removeGroup(channel, new ActionListener() {

            @Override
            public void onFailure(int reasonCode) {
                Log.d(TAG, "Disconecção falhada. Razão: :" + reasonCode);
            }

            @Override
            public void onSuccess() {
            }

        });
    }
    super.onStop();
}

/**
 * Registers a local service and then initiates a service discovery
 */
private void startRegistrationAndDiscovery() {
    Map<String, String> record = new HashMap<String, String>();
    record.put(TXTRECORD_PROP_AVAILABLE, "visivel");

    WifiP2pDnsSdServiceInfo service = WifiP2pDnsSdServiceInfo.newInstance(
            SERVICE_INSTANCE, SERVICE_REG_TYPE, record);
    manager.addLocalService(channel, service, new ActionListener() {

        @Override
        public void onSuccess() {
            appendStatus("Serviço local adicionado");
        }

        @Override
        public void onFailure(int error) {
            appendStatus("Falha ao adicionar serviço");
        }
    });

    discoverService();

}

private void discoverService() {

    /*
     * Register listeners for DNS-SD services. These are callbacks invoked
     * by the system when a service is actually discovered.
     */

    manager.setDnsSdResponseListeners(channel,
            new DnsSdServiceResponseListener() {

                @Override
                public void onDnsSdServiceAvailable(String instanceName,
                                                    String registrationType, WifiP2pDevice srcDevice) {

                    // A service has been discovered. Is this our app?

                    if (instanceName.equalsIgnoreCase(SERVICE_INSTANCE)) {

                        // update the UI and add the item the discovered
                        // device.
                        WiFiDirectServicesList fragment = (WiFiDirectServicesList) getFragmentManager()
                                .findFragmentByTag("serviços");
                        if (fragment != null) {
                            WiFiDevicesAdapter adapter = ((WiFiDevicesAdapter) fragment
                                    .getListAdapter());
                            WiFiP2pService service = new WiFiP2pService();
                            service.device = srcDevice;
                            service.instanceName = instanceName;
                            service.serviceRegistrationType = registrationType;
                            adapter.add(service);
                            adapter.notifyDataSetChanged();
                            Log.d(TAG, "onBonjourServiceAvailable "
                                    + instanceName);
                        }
                    }

                }
            }, new DnsSdTxtRecordListener() {

                /**
                 * A new TXT record is available. Pick up the advertised
                 * buddy name.
                 */
                @Override
                public void onDnsSdTxtRecordAvailable(
                        String fullDomainName, Map<String, String> record,
                        WifiP2pDevice device) {
                    Log.d(TAG,
                            device.deviceName + " é "
                                    + record.get(TXTRECORD_PROP_AVAILABLE));
                }
            });

    // After attaching listeners, create a service request and initiate
    // discovery.
    serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
    manager.addServiceRequest(channel, serviceRequest,
            new ActionListener() {

                @Override
                public void onSuccess() {
                    appendStatus("Adicionado pedido de descoberta de serviços");
                }

                @Override
                public void onFailure(int arg0) {
                    appendStatus("Falha ao adicionar pedido de descoberta de serviços");
                }
            });
    manager.discoverServices(channel, new ActionListener() {

        @Override
        public void onSuccess() {
            appendStatus("Descoberta de serviços iniciada");
        }

        @Override
        public void onFailure(int arg0) {
            appendStatus("Descoberta de serviços falhada");

        }
    });
}

@Override
public void connectP2p(WiFiP2pService service) {
    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = service.device.deviceAddress;
    config.wps.setup = WpsInfo.PBC;
    if (serviceRequest != null)
        manager.removeServiceRequest(channel, serviceRequest,
                new ActionListener() {

                    @Override
                    public void onSuccess() {
                    }

                    @Override
                    public void onFailure(int arg0) {
                    }
                });

    manager.connect(channel, config, new ActionListener() {

        @Override
        public void onSuccess() {
            appendStatus("A conectar a um serviço");
        }

        @Override
        public void onFailure(int errorCode) {
            appendStatus("Falha ao conectar a serviço");
        }
    });
}

@Override
public boolean handleMessage(Message msg) {
    switch (msg.what) {
        case MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;
            // construct a string from the valid bytes in the buffer
            String readMessage = new String(readBuf, 0, msg.arg1);
            Log.d(TAG, readMessage);
            (chatFragment).pushMessage(readMessage);
            break;

        case MY_HANDLE:
            Object obj = msg.obj;
            (chatFragment).setChatManager((ChatManager) obj);

    }
    return true;
}

@Override
public void onResume() {
    super.onResume();
    receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
    registerReceiver(receiver, intentFilter);
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
}

@Override
public void onConnectionInfoAvailable(WifiP2pInfo p2pInfo) {
    Thread handler = null;
    /*
     * The group owner accepts connections using a server socket and then spawns a
     * client socket for every client. This is handled by {@code
     * GroupOwnerSocketHandler}
     */

    if (p2pInfo.isGroupOwner) {
        Log.d(TAG, "Conectado como GroupOwner");
        try {
            handler = new GroupOwnerSocketHandler(
                    ((MessageTarget) this).getHandler());
            handler.start();
        } catch (IOException e) {
            Log.d(TAG,
                    "Falha ao criar uma thread de servidor - " + e.getMessage());
            return;
        }
    } else {
        Log.d(TAG, "Conectado como peer");
        handler = new ClientSocketHandler(
                ((MessageTarget) this).getHandler(),
                p2pInfo.groupOwnerAddress);
        handler.start();
    }
    chatFragment = new jogar();
    getFragmentManager().beginTransaction()
            .replace(R.id.container_root, chatFragment).commit();
    statusTxtView.setVisibility(View.GONE);
}

public void appendStatus(String status) {
    String current = statusTxtView.getText().toString();
    statusTxtView.setText(current + "\n" + status);
}

}

1 个答案:

答案 0 :(得分:0)

您的代码看起来正确,应该按预期工作。但是,我看到一个可能导致服务被忽略的案例(来自上面的代码):

 // A service has been discovered. Is this our app?

 if (instanceName.equalsIgnoreCase(SERVICE_INSTANCE)) {

当发现服务时,您的应用程序正按服务名称过滤它们,我认为两个应用程序之间的混淆是它们使用不同的服务名称。 你可以尝试删除这个&#34;如果&#34;声明并再次测试应用程序?

希望这有帮助,

古德勒克。