Signal strength in p2p connection with Wifi Direct?
Asked Answered
L

1

3

I am taking a look at the WifiDirect demo for Android and wanted to know whether it was possible to get something as the RSSI or signal strength between two connected devices.

Sorry if the question makes no sense, I know that one can get RSSI from access points but what I want to know is if this concept of RSSI exists in p2p connections among devices in Wifi Direct.

Thank you for your time.

Lorileelorilyn answered 15/5, 2012 at 14:49 Comment(0)
D
9

Note: This answer was true for the API level 14, I don't know if it still applies to the latest Android versions.

A non documented file named WifiP2pPeer exists in the Android code source. It contains some "interesting" lines.

We can see that a RSSI value is hard-coded (mRssi = 60; //TODO: fix), so the feature may not be implemented yet... (Like others in Android 14 regarding WifiP2p).

public class WifiP2pPeer extends Preference {

    private static final int[] STATE_SECURED = {R.attr.state_encrypted};
    public WifiP2pDevice device;

    private int mRssi;
    private ImageView mSignal;

    private static final int SIGNAL_LEVELS = 4;

    public WifiP2pPeer(Context context, WifiP2pDevice dev) {
        super(context);
        device = dev;
        setWidgetLayoutResource(R.layout.preference_widget_wifi_signal);
        mRssi = 60; //TODO: fix
    }

    @Override
    protected void onBindView(View view) {
        if (TextUtils.isEmpty(device.deviceName)) {
            setTitle(device.deviceAddress);
        } else {
            setTitle(device.deviceName);
        }
        mSignal = (ImageView) view.findViewById(R.id.signal);
        if (mRssi == Integer.MAX_VALUE) {
            mSignal.setImageDrawable(null);
        } else {
            mSignal.setImageResource(R.drawable.wifi_signal);
            mSignal.setImageState(STATE_SECURED,  true);
        }
        refresh();
        super.onBindView(view);
    }

    @Override
    public int compareTo(Preference preference) {
        if (!(preference instanceof WifiP2pPeer)) {
            return 1;
        }
        WifiP2pPeer other = (WifiP2pPeer) preference;

        // devices go in the order of the status
        if (device.status != other.device.status) {
            return device.status < other.device.status ? -1 : 1;
        }

        // Sort by name/address
        if (device.deviceName != null) {
            return device.deviceName.compareToIgnoreCase(other.device.deviceName);
        }

        return device.deviceAddress.compareToIgnoreCase(other.device.deviceAddress);
    }

    int getLevel() {
        if (mRssi == Integer.MAX_VALUE) {
            return -1;
        }
        return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS);
    }

    private void refresh() {
        if (mSignal == null) {
            return;
        }
        Context context = getContext();
        mSignal.setImageLevel(getLevel());
        String[] statusArray = context.getResources().getStringArray(R.array.wifi_p2p_status);
        setSummary(statusArray[device.status]);
    }
}
Dichlorodifluoromethane answered 15/5, 2012 at 15:31 Comment(6)
Thank you Fabien, this is useful for sure. Could I browse this file online somewhere? Or do I have to download the repo of 4.0 to view it?Lorileelorilyn
This should help : https://mcmap.net/q/63749/-where-can-i-find-android-source-code-online-closed. But I prefer to download it...Dichlorodifluoromethane
Great, I'll download it from the SDK manager and give it a look. Very useful discovery Fabien thanks again!Lorileelorilyn
FYI, the latest preview release (Android 5.L preview) has not fixed this. See this.Sketchy
Still hard coded in 5.1.1. LinkNevis
here is the link to the master: android.googlesource.com/platform/packages/apps/Settings/+/… (still hard coded)Calumniate

© 2022 - 2024 — McMap. All rights reserved.