wifi scanning issues fixed

parent 0a871921
package com.sliit.blindvision;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
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.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import java.util.ArrayList;
import java.util.List;
public class FriendlyWifis extends Activity {
private static final int PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION = 1;
private Button addWifi;
WifiManager wifi;
List<ScanResult> results;
ListView wifisList;
protected CharSequence[] options;
protected boolean[] selections;
ArrayAdapter<Router> arrayAdapter;
ArrayList<Router> wifis;
String building;
Button save;
DatabaseHelper db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
db = new DatabaseHelper(this);
addWifi = findViewById(R.id.button_add);
wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
addWifi.setOnClickListener(new ButtonClickHandler());
save = findViewById(R.id.save);
wifisList = findViewById(R.id.friendly_wifis);
Intent intent = getIntent();
building = intent.getStringExtra("BUILDING_NAME");
wifis = db.getFriendlyWifis(building);
arrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, wifis);
// Set The Adapter
wifisList.setAdapter(arrayAdapter);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (db.addFriendlyWifis(building, wifis)) {
Toast toast = Toast.makeText(getApplicationContext(), "Saved Wi-Fi Network", Toast.LENGTH_SHORT);
toast.show();
}
}
});
wifisList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int arg2, long arg3) {
wifis.remove(arg2);
arrayAdapter.notifyDataSetChanged();
return false;
}
});
}
public void updateOptions() {
options = new CharSequence[results.size()];
for (int i = 0; i < results.size(); i++)
options[i] = results.get(i).SSID;
selections = new boolean[options.length];
}
public class ButtonClickHandler implements OnClickListener {
public void onClick(View view) {
if (ActivityCompat.checkSelfPermission(FriendlyWifis.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(FriendlyWifis.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
// After this point, you wait for the callback in onRequestPermissionsResult(int, String[], int[]) overridden method
} else {
getAndShowScanResults();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getAndShowScanResults();
}
}
private void getAndShowScanResults() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
return;
}
results = wifi.getScanResults();
updateOptions();
onCreateDialog(0).show();
}
@Override
protected Dialog onCreateDialog(int id) {
return new AlertDialog.Builder(this)
.setTitle("Choose Friendly Wifis")
.setMultiChoiceItems(options, selections, new DialogSelectionClickHandler())
.setPositiveButton("OK", new DialogButtonClickHandler())
.create();
}
public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener {
public void onClick(DialogInterface dialog, int clicked, boolean selected) {
Log.i("ME", options[clicked] + " selected: " + selected);
}
}
public class DialogButtonClickHandler implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int clicked) {
switch (clicked) {
case DialogInterface.BUTTON_POSITIVE:
updateFriendlyWifis();
break;
}
}
}
protected void updateFriendlyWifis() {
for (int i = 0; i < options.length; i++) {
if (selections[i]) {
Router router = new Router(results.get(i).SSID, results.get(i).BSSID);
if (!wifis.contains(router)) {
wifis.add(router);
Toast.makeText(getApplicationContext(), "Wi-Fi network added: " + router.getSSID(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Wi-Fi network already added: " + router.getSSID(), Toast.LENGTH_SHORT).show();
}
}
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, wifis);
// Set The Adapter
wifisList.setAdapter(arrayAdapter);
Log.i("ME", options[i] + " selected: " + selections[i]);
}
}
}
package com.sliit.blindvision;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class Scan extends Activity {
private TextView warning;
private TextView timeRemaining;
private Button calibrate;
private int readingCount = 10;
private int currentCount;
String currentPositionName;
WifiManager wifi;
Timer timer;
TimerTask myTimerTask;
public class ResultData {
private Router router;
public Router getRouter() {
return this.router;
}
public List<Integer> values;
public ResultData(Router router) {
// TODO Auto-generated constructor stub
this.router = router;
values = new ArrayList<Integer>();
}
}
private List<ResultData> resultsData;
private List<PositionData> positionsData;
private PositionData positionData;
@SuppressWarnings("null")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan);
warning = findViewById(R.id.TextView1);
timeRemaining = findViewById(R.id.TextView2);
calibrate = findViewById(R.id.start);
wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
// Check if Wi-Fi is enabled
if (!wifi.isWifiEnabled()) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Confirm...");
alertDialog.setMessage("Scanning requires WiFi.");
alertDialog.setPositiveButton("Turn on WiFi", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Activity transfer to Wi-Fi settings
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
alertDialog.setCancelable(false);
alertDialog.show();
}
Intent intent = getIntent();
currentPositionName = null;
if (intent.getBooleanExtra("isLearning", true))
currentPositionName = intent.getStringExtra("POSITION_NAME");
calibrate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
calibrate.setEnabled(false);
warning.setText("DO NOT MOVE FOR");
resultsData = new ArrayList<ResultData>();
currentCount = 0;
timer = new Timer();
myTimerTask = new TimerTask() {
@Override
public void run() {
refresh();
}
};
timer.schedule(myTimerTask, 0, 1000);
}
});
}
private void refresh() {
// TODO Auto-generated method stub
if (currentCount >= readingCount) {
if (myTimerTask != null)
myTimerTask.cancel();
}
currentCount++;
wifi.startScan();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
List<ScanResult> results = wifi.getScanResults();
for (int i = 0; i < results.size(); i++) {
// System.out.println("test2");
String ssid0 = results.get(i).SSID;
String bssid = results.get(i).BSSID;
int rssi0 = results.get(i).level;
boolean found = false;
for (int pos = 0; pos < resultsData.size(); pos++) {
if (resultsData.get(pos).getRouter().getBSSID().equals(bssid)) {
found = true;
resultsData.get(pos).values.add(rssi0);
break;
}
}
if (!found) {
ResultData data = new ResultData(new Router(ssid0, bssid));
data.values.add(rssi0);
resultsData.add(data);
}
// String rssiString0 = String.valueOf(rssi0);
// textStatus = textStatus.concat("\n" + ssid0 + " " +
// rssiString0);
// System.out.println("ajsdhks"+textStatus);
}
// Log.v("textStatus", textStatus);
// System.out.println(""+textStatus);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// result.setText("here"+currentCount);
timeRemaining
.setText(" " + (readingCount - currentCount) + "s");
if (currentCount >= readingCount) {
returnResults();
}
}
});
}
private void returnResults() {
// TODO Auto-generated method stub
positionData = new PositionData(currentPositionName);
for (int length = 0; length < resultsData.size(); length++) {
int sum = 0;
for (int l = 0; l < resultsData.get(length).values.size(); l++) {
sum += resultsData.get(length).values.get(l);
}
int average = sum / resultsData.get(length).values.size();
positionData.addValue(resultsData.get(length).getRouter(), average);
}
Intent intent = new Intent(getApplicationContext(), Positions.class);
intent.putExtra("PositionData", (Serializable) positionData);
setResult(RESULT_OK,intent);
finish();
}
}
package com.sliit.blindvision;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
public class SplashActivity extends AppCompatActivity {
private static final int SPLASH_DISPLAY_LENGTH = 1500; // 3 seconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ImageView splashGif = findViewById(R.id.splashGif);
Glide.with(this).asGif().load(R.drawable.bling_spash).into(splashGif);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, Landing.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
\ No newline at end of file
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="85.84757"
android:endY="92.4963"
android:startX="42.9492"
android:startY="49.59793"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter a new building name"
android:ems="10" >
</EditText>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:enabled="false"
android:text="Add" />
<requestFocus />
</LinearLayout>
<ListView
android:id="@+id/buildingslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#0000CC"
android:dividerHeight="0.1dp" >
</ListView>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/locate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20px"
android:text="Locate"
android:layout_marginBottom="100dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/result"
android:textSize="30dp"
android:text=""
android:textColor="#ff0000"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your position"
android:textSize="30dp"
android:paddingBottom="30dp"
android:layout_above="@+id/result"
android:layout_centerHorizontal="true" />
<!--<LinearLayout-->
<!--android:id="@+id/everythingelse"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:orientation="vertical" >-->
<!---->
<!--<ScrollView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" >-->
<!--<TextView-->
<!--android:id="@+id/result"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" />-->
<!--</ScrollView>-->
<!--</LinearLayout>-->
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add more wifis" />
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<ListView
android:id="@+id/friendly_wifis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#0000CC"
android:dividerHeight="0.1dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@id/learn_button"
android:text="Main Menu"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/learn_button"
android:layout_width="@dimen/button_width"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_alignStart="@id/locate_button"
android:layout_alignLeft="@id/locate_button"
android:layout_above="@id/locate_button"
android:text="Learn"
android:onClick="onClick" />
<Button
android:id="@+id/locate_button"
android:layout_width="@dimen/button_width"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Locate"
android:onClick="onClick" />
</RelativeLayout>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment