adhoc7 / localization

0 stars 1 forks source link

Progress report on Android RSSI Logger #13

Open adhoc7 opened 7 years ago

adhoc7 commented 7 years ago

Due Date: 11-5-2016

alblowi commented 7 years ago

I am still trying to learn about Android e Stdio . what I did until now are: 1- how to deal with the android stdio Like (how to start ,Built an app, deal with the files and some code )
2- make an an activity on screen . 3- make a button and link it to a new window activity . 4- debugging from device.
5- write a code that can get the SSID Name (but there is problem not on the code the code is correct no error but it dose not brings any result ).

alblowi commented 7 years ago

this is what I did until now: 1- get the name of SSID. 2- GET RSS ( but im not sure if it correct because it should be positive # and 2 digit ). this is the code : package com.example.alblowi.myapplication_rss;

import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView;

import java.util.HashMap; import java.util.Iterator; import java.util.Map;

public class Page_1 extends AppCompatActivity {

    private TextView wifiDetails;
    private String data;
    private String ipAddressValue;
private String rssv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_1);

        wifiDetails = (TextView)findViewById(R.id.wifiDetails);

        data ="";

        WifiManager mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        WifiInfo connInfo = mWifiManager.getConnectionInfo();
        int numberOfLevels = 5;
        int rss = WifiManager.calculateSignalLevel(connInfo.getRssi(),5);

        final int NumOfRSSILevels = 6;

        HashMap<String,String> wifiInfo = new HashMap<String,String>();

        wifiInfo.put("SSID",connInfo.getSSID());

        wifiInfo.put("rss",rss+"");

        Iterator it = wifiInfo.entrySet().iterator();

        while (it.hasNext()) {

            Map.Entry pair = (Map.Entry)it.next();

            data = data+pair.getKey() + " : " + pair.getValue()+"\n";

            it.remove();
        }

        wifiDetails.setText(data);

    }

}  
alblowi commented 7 years ago

hello guys what I did until now : 1- get the MAC ,RSS and SSID name of the current network that I'm connected with . This is the code : package com.example.alblowi.myapplication_rss;

import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView;

import java.util.HashMap; import java.util.Iterator; import java.util.Map;

public class Page_1 extends AppCompatActivity {

    public TextView wifiDetails;
    public String data;
    public String ipAddressValue;
    public Button But1;

public void mm(View view){
wifiDetails = (TextView)findViewById(R.id.wifiDetails);
data ="";
WifiManager mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo connInfo = mWifiManager.getConnectionInfo();
int numberOfLevels = 5;
int rss = (connInfo.getRssi());
final int NumOfRSSILevels = 6;
HashMap<String,String> wifiInfo = new HashMap<String,String>();
wifiInfo.put("SSID",connInfo.getSSID());
wifiInfo.put("RSS",rss+"");
wifiInfo.put("MAC",connInfo.getBSSID());
Iterator it = wifiInfo.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    data = data+pair.getKey() + " : " + pair.getValue()+"\n";
    it.remove();
}
wifiDetails.setText(data);

}

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page_1);
}}
alblowi commented 7 years ago

today I tried many open source code but it doesn't work . My plan is go back to the previous code and try to do something .

alblowi commented 7 years ago

https://github.com/ajnas/WiFiPS the first code I tried https://www.tutorialspoint.com/android/android_wi_fi.htm the second

aymanausa commented 7 years ago

@alblowi it's better for you to test your work on push on git so whenever you wanna to go back to this code if someone wants to help you on it. it will be available for everyone and you will keep track of all your work. in addition, both links on your last comments is not working. finally it's good practice to mentioned why it's not working what kind of error you are encountering so it will be easier to help you on it

alblowi commented 7 years ago

Hello this is good is fine but it dose not show result any one can help my

Activity.JAVA package com.example.alblowi.myapplication_31;

import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;

import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.appindexing.Thing; import com.google.android.gms.common.api.GoogleApiClient;

import java.util.List;

public class WIFIScanner extends Activity implements OnClickListener {

private static final String TAG = "WIFIScanner";

// WifiManager variable
WifiManager wifimanager;

// UI variable
TextView textStatus;
Button btnScanStart;
Button btnScanStop;

private int scanCount = 0;
String text ="";
String result ="";

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            getWIFIScanResult(); // get WIFISCanResult
            wifimanager.startScan(); // for refresh
        } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            sendBroadcast(new Intent("wifi.ON_NETWORK_STATE_CHANGED"));
        }
    }
};
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

public void getWIFIScanResult() {

    List<ScanResult> mScanResult = wifimanager.getScanResults();
    // Scan count
    textStatus.setText("Scan count is \t" + ++scanCount + " times \n");

    textStatus.append("=======================================\n");
    for (int i = 0; i < mScanResult.size(); i++) {
        ScanResult r = mScanResult.get(i);
        textStatus.append((i + 1) + ". SSID : " + r.SSID + "\t\t RSSI : " + r.level + " dBm\n");

    textStatus.append("=======================================\n");}
}

public void initWIFIScan() {
    // init WIFISCAN
    scanCount = 0;
    text = "";
    final IntentFilter filter = new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    registerReceiver(mReceiver, filter);
    wifimanager.startScan();
    Log.d(TAG, "initWIFIScan()");
}

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Setup UI
    textStatus = (TextView) findViewById(R.id.textStatus);
    btnScanStart = (Button) findViewById(R.id.btnScanStart);
    btnScanStop = (Button) findViewById(R.id.btnScanStop);

    // Setup OnClickListener
    btnScanStart.setOnClickListener(this);
    btnScanStop.setOnClickListener(this);

    // Setup WIFI
    wifimanager = (WifiManager) getSystemService(WIFI_SERVICE);
    Log.d(TAG, "Setup WIfiManager getSystemService");

    // if WIFIEnabled
    if (!wifimanager.isWifiEnabled())
        wifimanager.setWifiEnabled(true);

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

public void printToast(String messageToast) {
    Toast.makeText(this, messageToast, Toast.LENGTH_LONG).show();
}

@Override
public void onClick(View v) {

    if (v.getId() == R.id.btnScanStart) {
        Log.d(TAG, "OnClick() btnScanStart()");
        printToast("WIFI SCAN !!!");
        initWIFIScan(); // start WIFIScan
    }
    if (v.getId() == R.id.btnScanStop) {
        Log.d(TAG, "OnClick() btnScanStop()");
        printToast("WIFI STOP !!!");
        unregisterReceiver(mReceiver); // stop WIFISCan
    }
}

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
public Action getIndexApiAction() {
    Thing object = new Thing.Builder()
            .setName("WIFIScanner Page") // TODO: Define a title for the content shown.
            // TODO: Make sure this auto-generated URL is correct.
            .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
            .build();
    return new Action.Builder(Action.TYPE_VIEW)
            .setObject(object)
            .setActionStatus(Action.STATUS_TYPE_COMPLETED)
            .build();
}

@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    AppIndex.AppIndexApi.start(client, getIndexApiAction());
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    AppIndex.AppIndexApi.end(client, getIndexApiAction());
    client.disconnect();
}

}

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="WIFIScanner"
    android:layout_weight="0.08"
    android:textAppearance="@style/TextAppearance.AppCompat.Button" />

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tableLayout1"
    android:layout_weight="0.22">

</TableLayout>
<TableRow android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_height="wrap_content"
        android:text="Scan"
        android:id="@+id/btnScanStart"
        android:layout_width="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnScanStop"
        android:text="Stop"
        android:layout_weight="1"/>
</TableRow>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:id="@+id/textStatus" />

manifests.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.alblowi.myapplication_31">

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".WIFIScanner">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
 App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>