How to make own device client or sender

眉间皱痕 提交于 2019-12-11 15:02:48

问题


I am developing a sharing app where i am using wifip2p framework to connect two devices and one is sender and other is receiver. And the problem that i am facing is that who would be sender and who would be receiver get selected automatically.

I am using below code to connect to devices.

/**
 * @param device to connect
 */
@Override
public void connect(WifiP2pDevice device) {
    WifiP2pConfig config = new WifiP2pConfig();
    config.groupOwnerIntent = 0;
    config.deviceAddress = device.deviceAddress;
    config.wps.setup = WpsInfo.PBC;
    manager.connect(channel, config, new WifiP2pManager.ActionListener() {

        @Override
        public void onSuccess() {
            Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(int reason) {
            Toast.makeText(getApplicationContext(), "Connect failed. Retry.",
                    Toast.LENGTH_SHORT).show();
        }
    });
}

and to delete previous created groups i used below code.

/**
 *  @deletePersistentGroups delete all created groups
 */

private void deletePersistentGroups(){
    try {
        Method[] methods = WifiP2pManager.class.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("deletePersistentGroup")) {
                // Delete any persistent group
                for (int netid = 0; netid < 32; netid++) {
                    methods[i].invoke(manager, channel, netid, null);
                }
            }
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
}

this onConnectionInfoAvailable function is trigger when both device connect successfully and i am facing the problem i.e. if i click on connect button from device 1 , sometimes it acts as sender or sometimes receiver what i want to achieve is that device 1 must always be sender and other device receiver


  /**
     * @param wifiP2pInfo connection information after device connected to desire device
     */
    @Override
    public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) {
        if (wifiP2pInfo.groupFormed && wifiP2pInfo.isGroupOwner ) {
            Toast.makeText(getApplicationContext(), "Owner", Toast.LENGTH_LONG).show();
            new FileServerAsyncTask(this).execute();
        }
        else if (wifiP2pInfo.groupFormed){
            Toast.makeText(getApplicationContext(), "Client", Toast.LENGTH_LONG).show();
            Intent serviceIntent = new Intent(getApplicationContext(), FileTransferService.class);
            serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
            serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, path);
            serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS, wifiP2pInfo.groupOwnerAddress.getHostAddress());
            serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
            startService(serviceIntent);
        }
    }

来源:https://stackoverflow.com/questions/57834846/how-to-make-own-device-client-or-sender

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!