I need to create WiFi scanner app on android device. I managed to do it, yet there is something I don't know how to deal with.
class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
sb = new StringBuilder();
Comparator<ScanResult> comparator = new Comparator<ScanResult>() {
#Override
public int compare(ScanResult o1, ScanResult o2) {
return (o1.level>o2.level ? -1 : (o1.level==o2.level ? 0 : 1));
}
};
lista = wifiManager.getScanResults();
Collections.sort(list, comparator);
for (int i = 0; i < list.size(); i++) {
scanResult = wifiManager.getScanResults().get(i);
sb.append(new Integer(i + 1).toString() + ". " + (list.get(i)).SSID + " " + (list.get(i)).BSSID + " " + (list.get(i)).level + "\n");
}
txt.setText(sb);
wifiManager.startScan();
}
}
There are several WiFi networks in the building with the same SSID, lets say ABCD. I want to save in list, which is List<ScanResult> type, only networks with this specific SSID ABCD without the need to create another field such as list if possible. I woukd be grateful for any help
I assume you mean you want a way to filter the scan and return only the specified ssid, there is no way that I know of on the back-end to do that. You can go through your existing list and pull out the ones you want and save to a new List.
You can also use your existing code and check to see if the ssid matches inside your loop and only build the string if it does.
Most of the time you will be showing a list of available networks in a list or recycler view so usually the secondary list is the way to go.
Related
I am not able to sort the wifi list, I am getting duplicate wifi SSIDs so I want get list of unique SSIDs which has higher strength. You can see my following code but not getting correct wifi list.
List<ScanResult> results = mWifiManager.getScanResults();
int size = results.size();
try {
for (int i = 0; i < results.size() - 1; i++) {
for (int k = i + 1; k < results.size(); k++) {
if (results.get(i).SSID.equals(results.get(k).SSID)) {
if (results.get(k).level > results.get(i).level) {
results.remove(i);
} else {
results.remove(k);
}
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
merge ScanResults using HashMap in which key will be String SSID. just add check for existence of previously added ScanResult (in iteration), then compare both level values
edit:
List<ScanResult> results = mWifiManager.getScanResults();
HashMap<String, ScanResult> filtered = new HashMap<String, ScanResult>();
for(ScanResult sc : results){
ScanResult prevScanResultOrNull = filtered.get(sc.SSID);
// compare lever with previous ScanResult,
// insert bigger value under SSID key, they are same
if(prevScanResultOrNull!=null)
filtered.put(prevScanResultOrNull.SSID, // or sc.SSID, whatever
prevScanResultOrNull.level > sc.level ?
prevScanResultOrNull : sc);
// no SSID in HashMap, so just put as highest value
else
filtered.put(sc.SSID, sc);
}
edit2:
if you still want to use List interface then:
results = new ArrayList<ScanResult>(filtered.values());
I wish to know how an app on a device knows the neighbouring network which have a device offering a particular service without even being connected to the network.
I will take for example the case of the mobile app called Xender. When you launch this app and Click on"Create a group", it create a hotspot with the default device hotspot name as the SSID of the network, when another device launch the app and click on "Join group", this device is able to see the available device around by sensing the networks around. The second device is able to receive the a name of the first device, without even being connected to the network created by the fist device.
How is it possible? Especially in android.
Xender just encodes the SSID like if you set name "xx" then the SSID will be ADYXeHg or ADYYeHg or something else but ADY_eHg letters remain same in different device at same name "xx" so we have to just decode that method of Xender.
I guess they use cipher encryption to convert profile name into SSID.
This is how xender code would be look like in encryption (Hotspot side).
code : not this is not xender's code it is coded by me.
public String Encry(String sc) {
int i;
String data = sc;
StringBuffer pas = new StringBuffer(data);
for (i = 0; i < pas.length(); i++) {
int temp = 0;
temp = (int) pas.charAt(i);
temp = temp - 10;
pas.setCharAt(i, (char) temp);
}
String data1 = "robo" + pas;
String[] one = data1.split("robo");
String s = Arrays.toString(one);
s = s.substring(1, s.length()-1).replaceAll(",", "");
StringBuffer tu = new StringBuffer(s);
for (i = 0; i < tu.length(); i++) {
int da = 0;
da = (int) tu.charAt(i);
da = da + 10;
tu.setCharAt(i, (char) da);
}
System.out.println("\n");
String star = tu.toString();
star = star.substring(1, star.length() - 1).replaceAll(",", ""); // remove " * " from String.
return data1;
}
and this is how dicryption works ( Join network side ) :
note : this is not xender's code it is coded by me.
public String Dicry(String SSID) {
int i;
String data1 = SSID;
String[] one = data1.split("robo");
String s = Arrays.toString(one);
s = s.substring(1, s.length()).replaceAll(",", "");
StringBuffer tu = new StringBuffer(s);
for (i = 0; i < tu.length(); i++) {
int da = 0;
da = (int) tu.charAt(i);
da = da + 10;
tu.setCharAt(i, (char) da);
}
String star = tu.toString();
star = star.substring(1, star.length()).replaceAll(",", ""); // remove " * " from String.
return star;
} // scanner
Note : this code is coded by me . its not a xender's original code. please keep in mind this.
this is how cipher encryption works.
What Xender do when you create a group, is creating an hotspot, like your home router.
What routers do when they are on, is broadcast their name (SSID) so other devices (like your phone or laptop) will be able to "se" them and connect to them.
So basically, when one Xender app joins to another Xender app group, it first connects to the hotspot that the phone creates, and then start sharing data on that connection.
The thing that bugs me the most about this is; I have another activity that uses a different approach (using startActivityForResult(intent....) vs a listener class implementing RecognitionListener, and one works the other doesnt'. I'm certain I'm doing something stupidly wrong, but I sure can't figure out what.
I'm getting the speech recog results back using onResults, using a listener class that implements RecognitionListener.
public void onResults(Bundle results) {
Log.d("Speech", "onResults");
ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < strlist.size(); i++) {
String matchstring = matchpair[1].toString().toLowerCase();
String speechstring = strlist.get(i).toString().toLowerCase();
Log.d("Speech", "result=" + speechstring);
if (checkResult(matchstring, speechstring)) {
System.out.println("Phonetic match: " + speechstring + " = " + matchstring);
} else System.out.println("NO match: " + speechstring + " = " + matchstring);
}
}
The checkresult is simple, just a boolean compare:
public boolean checkResult(String result, String checkmatch) {
if (result.equals(checkmatch))
{
return true;
}
return false;
}
Looking at the debug info, the results appear to compare - but the boolean is returning false.
It's driving me nuts. I'm going this route so I can use the recognition functionality without having the Google dialog pop on every recognition attempt. In the activity I began with, the compare is working perfectly.
I welcome someone pointing out whatever very stupid thing I am overlooking.
First sorry for my English it is something bad.
Now i am currently programming an application and there is a problem with the list.
I always get duplicates in my list after searching for Devices, when i search again,I get the same devices again. I have to tried to use sets but i get still duplicates.
I don't know how to solve this problem.
Please help someone with Code.
Thank you advance.
Here is my Code:
`
public void Init(){
foundDevicesSet = new HashSet<String>();
tBtn = (ToggleButton)findViewById(R.id.toggleButton1);
availableDivce = (TextView)findViewById(R.id.textView2);
bluetoohOff =(TextView)findViewById(R.id.textView1);
DeviceList = (ListView)findViewById(R.id.listView1);
searchButton =(Button)findViewById(R.id.button1);
deviceNameAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
DeviceList.setAdapter(deviceNameAdapter);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice newDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (newDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
foundDevicesSet.add(newDevice.getName() + "|" + newDevice.getAddress());
for(String one : foundDevicesSet){
deviceNameAdapter.add(one);
}
}
}
}
};
} `
I'm on a Bluetooth project too and I was facing this same problem 5 minutes ago, not with paired devices but with available devices around. Here is the code that made it work without any duplicate device. I must say I'm using a ListView to show them and an ArrayAdapter aswell:
if (mNewDevicesArrayAdapter.getPosition((device.getName()+ "\n" + device.getAddress())) == -1)
{
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
mNewDevicesArrayAdapter.notifyDataSetChanged();
}
This instruction looks for this device.getName() + "\n" + device.getAddress() in the ArrayAdapter because that's is the format I use to save it in. Hope it helps you.
Each time a new device is found, you're adding the entire devices set to the adapter again... So, either:
Call deviceNameAdapter.add(newDevice.getName() + "|" + newDevice.getAddress()); instead of adding it to the set.
Call deviceNameAdapter.clear() before adding the entire set to the adapter.
I m developing a app that downloads list of files from ftp server. the file names are downloaded using array. how can i pass this array to main activity and display it in a list view.i have a very little knowledge in this domain so kind of help is appreciated.thanking you in advance
FTPFile[] ftpFiles = mFTPClient.listFiles("/public_html/");
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
Log.i(TAG, "File : " + name);
}
else {
Log.i(TAG, "Directory : " + name);
}
You need to use ListView widget. The simplest way of displaying a list of items in a list view would be to use predefined ArrayAdapter class and an existing Android layout for the rows. Please check this tutorial for detailed explanation how to use ListView.
http://www.vogella.com/articles/AndroidListView/article.html