Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does shareit discovers nearby devices android programmatically [duplicate]

I'm want to create a file sharing app like shareit but I'm really confused about how shareit discovers the nearby devices.

When you click receive button shareit creates a hotspot at the receiver side and the sender without connecting to the hotspot shows the receiver name. How is that possible?

If shareit uses Wi-Fi direct then what's the point of creating hotspot?

And to use Network Service Discovery (NSD) both server and client should be on same network so I think shareit is using something else

If anyone can explain this concept of shareit it will be very helpful.

like image 464
Mubarak Basha Avatar asked Mar 23 '16 10:03

Mubarak Basha


2 Answers

I finally found the answer! SHAREit uses WiFi SSID to identify the nearby app users. The SSID Consist of two partslike this. BAHD-bXViYQ WHERE 'B' Stands for ANDROID DEVICE and the AHD for the user icon. the second part is the user name encoded in Base64. In this example my name muba. I hope this answer helps save some time.

like image 56
Mubarak Basha Avatar answered Sep 28 '22 11:09

Mubarak Basha


Well I have found how to enable the hotspot from one sharing app and find the list of available wifi enabled by that sharing app into another one. just like shareIt receiver enables the wifi hotspot and sender discovers the list of available receivers. First of all You have to scan all available wifi network using WifiManager

public void startScan(){
    mWifiManager.startScan();
    mScanResultList = mWifiManager.getScanResults();
    mWifiConfigurations = mWifiManager.getConfiguredNetworks();
}

now pass this mScanResultList to a method which finds the network according to your requirement.

public static List<ScanResult> filterWithNoPassword(List<ScanResult> scanResultList){
    if(scanResultList == null || scanResultList.size() == 0){
        return scanResultList;
    }

    List<ScanResult> resultList = new ArrayList<>();
    for(ScanResult scanResult : scanResultList){
        if(scanResult.capabilities != null && scanResult.capabilities.equals(NO_PASSWORD) || scanResult.capabilities != null && scanResult.capabilities.equals(NO_PASSWORD_WPS)){
            resultList.add(scanResult);
        }
    }

    return resultList;
}

Now pass the resultList to arraylist adapter to show the network in list. Inside adapter's convertView method just pass that dataList to scanner to get ssid and mac address of the network

@Override
public View convertView(int position, View convertView) {
    ScanResultHolder viewHolder = null;

    if(convertView == null){
        convertView = View.inflate(getContext(), R.layout.item_wifi_scan_result, null);
        viewHolder = new ScanResultHolder();
        viewHolder.iv_device = (ImageView) convertView.findViewById(R.id.iv_device);
        viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
        viewHolder.tv_mac = (TextView) convertView.findViewById(R.id.tv_mac);
        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ScanResultHolder) convertView.getTag();
    }

    ScanResult scanResult = getDataList().get(position);
    if(scanResult != null){
        viewHolder.tv_name.setText(scanResult.SSID);
        viewHolder.tv_mac.setText(scanResult.BSSID);
    }

    return convertView;
}

This is the code to enable the hotspot

public static boolean configApState(Context context, String apName) {
    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiConfiguration wificonfiguration = null;
    try {
        wificonfiguration = new WifiConfiguration();
        wificonfiguration.SSID = apName;
        // if WiFi is on, turn it off
        if(isApOn(context)) {
            wifimanager.setWifiEnabled(false);
            // if ap is on and then disable ap
            disableAp(context);
        }
        Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        method.invoke(wifimanager, wificonfiguration, !isApOn(context));
        return true;
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
like image 41
Shahkar Raza Avatar answered Sep 28 '22 10:09

Shahkar Raza