سؤال

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.

هل كانت مفيدة؟

المحلول

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]);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top