Find all devices connected in a wifi network - android

I am trying to make an app here, which will detect all the devices connected in that WiFi network. I have done enough google and come up with an App which can detect IP Addresses of the devices connected in the WiFi network of the app.
Now I want few more things.
Can I find device name i.e. phone name or model or system name any
information by which we can detect the particular device?
Can we find the device distance like how far is that device from the
phone in which we are using our app?
This one is the main task- I want to share data over device
connected to same WiFi. So is that possible?
Any kind of help is appreciated

to identify device NMAP OS fingerprint can be run.
I want to share data between two device that is connected to same WiFi network. So is that possible?
What you mean by this ? if they are on same LAN they can communicate over socket connection provided client is listening on particular port.

Yes, you can get device name or model number using like this...
public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
return capitalize(model);
} else {
return capitalize(manufacturer) + " " + model;
}
}
private String capitalize(String s) {
if (s == null || s.length() == 0) {
return "";
}
char first = s.charAt(0);
if (Character.isUpperCase(first)) {
return s;
} else {
return Character.toUpperCase(first) + s.substring(1);
}
}

Related

read data from exising secret code

I want to read data from existing secret code. there are plenty of secret code in android like *#*#4636#*#* to get phone info and *#06#to get IMEI.
so there any way in android to write a code by which we can fire these secret code and read the result back in our activity ?
Afaik No, you cannot run those "secret" codes and get the result in your activity. Because, these codes need to be dialed in and then only the results come in. Even if you are able to dial these codes, you wont be able to get the output in your activity for sure.
But there are other many ways through which you can get the data that you need programmatically. For instance, for IMEI number you can use getDeviceId()
android.telephony.TelephonyManager.getDeviceId()
This requires the following permission.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
(Be careful with the above permission as users will wonder as to why your application needs to access the telephony stack)
To get other details like the manufacture, OS, etc take a look at Build and System. You can use the following codes
android.os.Build.MANUFACTURER // Manufacturer
android.os.Build.VERSION.SDK // API Level
android.os.Build.DEVICE // Device
android.os.Build.MODEL // Model
android.os.Build.PRODUCT
A simple and efficient code to get the device full name for all types of manufacturers and devices is as follows.
public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
return capitalize(model);
} else {
return capitalize(manufacturer) + " " + model;
}
}
private String capitalize(String s) {
if (s == null || s.length() == 0) {
return "";
}
char first = s.charAt(0);
if (Character.isUpperCase(first)) {
return s;
} else {
return Character.toUpperCase(first) + s.substring(1);
}
}

How to differentiate Open and Secure WiFi networks without connecting to it in Android programmatically?

I am trying to develop a small app where I can scan and connect to the WiFi hot-spots from scanned list of networks. But for both, Open and Secure networks I have written a password prompt and if the network is open (by knowing it beforehand) I do not enter password and keep the text blank and enter and then it connects. Can anyone tell how to identify programmatically open and secure wifi so that I won't ask for password for Open network and let it connect directly. (I won't be knowing which is Open and Which is secure network in future, so we need to identify open and secure network from SSID or something)
Here you have capabilities field which is used to identify the network type
WifiManager wifimanger = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> networkList = wifimanger.getScanResults();
if (networkList != null) {
for (ScanResult network : networkList) {
String capabilities = network.capabilities;
Log.w(TAG, network.SSID + " capabilities : " + capabilities);
if (capabilities.toUpperCase().contains("WEP")) {
// WEP Network
} else if (capabilities.toUpperCase().contains("WPA")
|| capabilities.toUpperCase().contains("WPA2")) {
// WPA or WPA2 Network
} else {
// Open Network
}
}
}
Use this filter function to differentiate.
private boolean isProtectedNetwork(String capability){
return (capability.contains("WPA") ||
capability.contains("WEP") ||
capability.contains("WPS")
);
}
So when you have hold of all the List. Just iterate through it and add them in two different lists. (One for open and one for secure networks).
Here is the code for that
private void filterScan(List<ScanResult> allScanResults){
List<ScanResult>openScans = new ArrayList<ScanResult>();
List<ScanResult>closeScans = new ArrayList<ScanResult>();
for(ScanResult result : allScanResults)
{
if(!isProtectedNetwork(result.capabilities))
{
openScans.add(result);
}
else {
closeScans.add(result);
}
}
}
Useful Resource:
You can find more related solutions on My Github Repository
Today I was looking for an answer to this same question, but the solution pointed out here and in other similar questions made me a little insecure. And if a new security mode is created in the future?
For now I prefer to do something like:
public WifiSecurityMode testAgainstSecurityModes(WifiSecurityMode... securities) {
for (WifiSecurityMode security : securities) {
if (this.capabilities.toUpperCase().contains(security.getName().toUpperCase())) {
return security;
}
}
return WifiSecurityMode.UNKNOWN;
}
public enum WifiSecurityMode {
WEP("WEP"), WPA("WPA"), WPA2("WPA2"),//....
UNKNOWN ("UNKNOWN");
private String name;
WifiSecurityMode (String name){
this.name = name;
}
public String getName() {
return name;
}
}
//to use it: (WifiInfo are just a class I create to encapsulate the values in a ScanResult object)
public List<WifiInfo> getOpenWifis() {
List<WifiInfo> open = new ArrayList<>();
for (WifiInfo w : wifiInfoList) {
if (w.testAgainstSecurityModes(WifiSecurityMode.WEP, WifiSecurityMode.WPA, WifiSecurityMode.WPA2).equals(WifiSecurityMode.UNKNOWN)) {
open.add(w);
}
}
return open;
}
After that, you do what you want with UNKNOWN return.
If a new security mode be created in the future, you will not need to change testAgainstSecurityModes method.

Android: Get the device name programmatically

This is what I tried.
mTextView.setText("MODEL: "+android.os.Build.MODEL
+"\nDEVICE: "+android.os.Build.DEVICE
+"\nBRAND: "+android.os.Build.BRAND
+"\nDISPLAY: "+android.os.Build.DISPLAY
+"\nBOARD: "+android.os.Build.BOARD
+"\nHOST: "+android.os.Build.HOST
+"\nMANUFACTURER: "+android.os.Build.MANUFACTURER
+"\nPRODUCT: "+android.os.Build.PRODUCT);
Can anyone tell me how to get these outputs:
Samsung Galaxy S
Samsung note 3
Sony Xperia z
Sony Xperai z1
Samsung Grand
for nexus it shows nexus 4 or nexus 7 not the same case with sony or samsung.
you can use:
public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
return capitalize(model);
} else {
return capitalize(manufacturer) + " " + model;
}
}
private String capitalize(String s) {
if (s == null || s.length() == 0) {
return "";
}
char first = s.charAt(0);
if (Character.isUpperCase(first)) {
return s;
} else {
return Character.toUpperCase(first) + s.substring(1);
}
}
see this for Build.MODEL.
more info in Get Android Phone Model Programmatically.
In order to get android device name you have to add only a single line of code:
android.os.Build.MODEL;
Found here:getting-android-device-name
Download csv from here:
https://support.google.com/googleplay/answer/1727131?hl=en
See if your device works with Google Play by checking the list below.
When you download the PDF file, devices are ordered alphabetically
(A-Z) by manufacturer name.
Then import this file into your database.
Query RetailBranding and Marketing Name by Model = Build.Model.
To have a better performance, put these data in your database server.
Get the RetailBranding and Marketing Name when the app is first launched and save it to the local database.
you can also get it via bluetoothAdapter see this
public String getPhoneName()
{
BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
String deviceName = myDevice.getName();
return deviceName;
}

How do you retrieve the WiFi Direct MAC address?

I am trying to retrieve the MAC address of an Android device. This is ordinarily possible through the WiFiManager API if WiFi is on.
Is there any way to get the MAC address if WiFi is off and WiFi Direct is on?
WiFi AND WiFi Direct can't be on at same time on my phone.
Thanks
I had been searching for this during my project. My requirements were to uniquely identify devices in an adhoc P2p network formed with WiFi Direct. Each device should identify its friend device the next time when it comes into proximity. I needed my own WiFi (Direct) MAC and my friends' to create a Key for this friend zone creation.
My Research: The design is in such a way that there is an Unique Universal ID and a Local ID. Reason: Universal ID can only be used to connect to Infrastructure mode Networks. Local ID could be used for "ad-hoc" mode networks(device to device). In this ad-hoc mode, there are possibilities that a single device might simultaneosly belong to several ad-hoc groups.
Hence to support this concurrent operations, P2p devices support
Multiple MAC entities, possibly on different channels.
For each session, a persistent group MAY use a different channel and device
MAC for each session.
P2P devices use their global MAC address as Device ID during discovery and negotiation, and a temporary local MAC address for all frames within a group. Understood from here
However, there is NO straight forward way to obtain one's own WiFi P2p MAC address. Issue 53437: Android.
In this issue discussion, the project member from google has suggested this is possible and just that it hasn't been documented
Solution: Using intent filter WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION and the extra
from the intent WifiP2pManager.EXTRA_WIFI_P2P_DEVICE
This is how I have used it in my project:
#Override
public void onReceive(Context context, Intent intent) {
....
....
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
.equals(action)) {
WifiP2pDevice device = (WifiP2pDevice) intent
.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
String myMac = device.deviceAddress;
Log.d(TAG, "Device WiFi P2p MAC Address: " + myMac);
/* Saving WiFi P2p MAC in SharedPref */
sharedPref = context.getSharedPreferences(context.getString(R.string.sp_file_name), Context.MODE_PRIVATE);
String MY_MAC_ADDRESS = sharedPref.getString(context.getString(R.string.sp_field_my_mac), null);
if (MY_MAC_ADDRESS == null || MY_MAC_ADDRESS != myMac) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(context.getString(R.string.sp_field_my_mac), myMac);
editor.commit();
}
Hope this helps someone!
The mac addresss of WiFi is different than that of WiFi Direct. Usually first 2 letters might be different. Be careful about that.
The mac address of WiFi is different than that of WiFi Direct.
You can get WiFi direct address using next code:
public String getWFDMacAddress(){
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ntwInterface : interfaces) {
if (ntwInterface.getName().equalsIgnoreCase("p2p0")) {
byte[] byteMac = ntwInterface.getHardwareAddress();
if (byteMac==null){
return null;
}
StringBuilder strBuilder = new StringBuilder();
for (int i=0; i<byteMac.length; i++) {
strBuilder.append(String.format("%02X:", byteMac[i]));
}
if (strBuilder.length()>0){
strBuilder.deleteCharAt(strBuilder.length()-1);
}
return strBuilder.toString();
}
}
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
return null;
}
The WiFi Direct mac address is going to be different. It's explained beautifully by #auselen here https://stackoverflow.com/a/14480530/3167704.
I just figured out a way to retrieve WiFi Direct mac address. It isn't pretty but gets the job done. Here's the code,
final WifiP2pManager p2pManager = (WifiP2pManager) getSystemService(WIFI_P2P_SERVICE);
final WifiP2pManager.Channel channel = p2pManager.initialize(this, getMainLooper(), null);
p2pManager.createGroup(channel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
p2pManager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
#Override
public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) {
Log.i("", wifiP2pGroup.getOwner().deviceAddress);
// Following removal necessary to not have the manager busy for other stuff, subsequently
p2pManager.removeGroup(channel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
Log.i("", "Removed");
}
#Override
public void onFailure(int i) {
Log.i("", "Failed " + i);
}
});
}
});
}
#Override
public void onFailure(int i) {
Log.i("", String.valueOf(i));
}
});

wifi search for the devices connected to the same network i.e other than acces point(for android)

i want to make a modification to my project and right now the project status is.....
it is searches the available WiFi networks and shows the list with info of the network this works properly.Now i want to search and see the details of the devices connected to the network.
Is there any way to find these devices ?
Your comment will be useful for me, Thanks.
You can loop over the IP ranges, and "ping" them.
It is not the best / fastest method (UDP is better) but, it works in many cases.
The sample code below returns the list of the IP addresses connected to the current network.
private int LoopCurrentIP = 0;
public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
ArrayList<InetAddress> ret = new ArrayList<InetAddress>();
LoopCurrentIP = 0;
String IPAddress = "";
String[] myIPArray = YourPhoneIPAddress.split("\\.");
InetAddress currentPingAddr;
for (int i = 0; i <= 255; i++) {
try {
// build the next IP address
currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
myIPArray[1] + "." +
myIPArray[2] + "." +
Integer.toString(LoopCurrentIP));
// 50ms Timeout for the "ping"
if (currentPingAddr.isReachable(50)) {
ret.add(currentPingAddr);
}
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
LoopCurrentIP++;
}
return ret;
}
Would you like to discover a specific device ? Or you need the list of all connected devices? The second I don't think is possible.
EDIT
Discovering specific devices:
Using UDP Broadcast. Some reference can be found here!
There are some protocols that are supported by some devices( routers, HDD, etc...), like UPNP!
If you develop a software on the device which you would like to discover you could create a UDP server listening on a specific port.
Your client will just send a broadcast message on that port and your Server will send a response with the information you need.
Here it is a simple example.

Categories

Resources