How to get Wifip2pInfo object in wifi direct - android

It's giving a null pointer exception.I would like to get the IP address of a device connected via wifi direct.How to do this can anyone explain?Attached a Screen to see.
Thanks in advance.

You can use following code snippet, When WiFiP2pInfo has connection then it is not null but when there is no connection or connection lost then it will be null.
if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION
.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
manager.requestConnectionInfo(channel,
new ConnectionInfoListener() {
#Override
public void onConnectionInfoAvailable(
WifiP2pInfo info) {
if (info != null) {
activity.setConnectionInfo(info); // When connection is established with other device, We can find that info from wifiP2pInfo here.
}
}
}
);
} else {
activity.resetData(); // When connection lost then we can reset data w.r.t that connection.
}
}
Following code helps to find address of Client and Owner,
public static String getDestinationDeviceIpAddress(WifiP2pInfo wifiP2pInfo) {
String destinationAddress;
if (wifiP2pInfo.isGroupOwner) {
destinationAddress = WifiDirectUtil.getIPFromMac();
} else {
destinationAddress = wifiP2pInfo.groupOwnerAddress.getHostAddress();
}
return destinationAddress;
}
public static String getIPFromMac() {
BufferedReader br = null;
boolean isFirstLine = true;
String ipAddress = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue;
}
String[] splitted = line.split(" +");
Log.d(TAG, "** length **" + splitted.length);
if (splitted.length >= 4) {
String device = splitted[5];
Log.d(TAG, device);
if (device.contains("p2p")) {
ipAddress = splitted[0];
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return ipAddress;
}
And add permission Read External storage in manifest.xml.

Related

Android : How to know the ip connected to my hotspot in android

How to know the ip connected to my hotspot in android
I try to under code but /proc/net/arp is not reachable file in android 10
https://developer.android.com/about/versions/10/privacy/changes#proc-net-filesystem
BufferedReader br = null;
final ArrayList<ClientScanResult> result = new ArrayList<ClientScanResult>();
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if ((splitted != null) && (splitted.length >= 4)) {
// Basic sanity check
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.toString());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}

Get the list of all the devices connected within same Network Android

In Android,
How to get the list of devices connected in same WiFi Network?
How to get the nearest devices from your smartphone connected with same WiFi network?
Did frequency check is able to find the nearest device?
I find the solution for list of devices connected in same network,and i placed the code below
Call this method in onCreate -> getClientList(true, 200);
write this method in our class
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
BufferedReader br = null;
ArrayList<ClientScanResult> result = null;
try {
result = new ArrayList<ClientScanResult>();
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
int i = 0;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if ((splitted != null) && (splitted.length >= 4)) {
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
i++;
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.getMessage());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}
return result;
}
if any clarifications, please reply a message for this post,
THANKS

BufferedReader.readline() return nothing in result in android

i am working on socket programming in android and trying to read server responce from my android application .
For this i am using bufferreader.readline() but it is not returen any thing
I have tested it in android studio "Evalute Expression" , it will return just "result=" in result.
Here is my code which i have used
try
{
if (socket != null)
{
//Log.i("AsynkTask", "readFromStream : Reading message");
//message = dis.toString();
/*InputStreamReader isrdr=new InputStreamReader(socket.getInputStream());
if(isrdr.ready())
{
}
else
{
message="Reader Not Ready : ";
}*/
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true)
{
//String msg = in.readLine();
if (in.readLine() == null) {
message = "No data read";
} else {
message = "data available";
}
}
}
else
{
message="No Socket Connected";
Log.i("AsynkTask", "readFromStream : Cannot Read, Socket is closed");
}
}
catch (Exception e)
{
message="Exception Occured";
Log.i("AsynkTask", "readFromStream : Writing failed");
}
Id check that your socket.getInputStream() is not returning null
Also your while will never escape until an exception is thrown so id change that to be while (in.readLine() != null)
Edit
I would do this
if (socket.getInputStream() == null) {
//deal with this
}
else {
while(in.readLine() != null) {
processString();
}
}
}
I have no idea why your socket is null but i can t see any initialisation for it

Get a notification when client connects to access point?

I have a question:
Is there any why to get a notification (intent, broadcast etc..) in my MainActivity when a client connects to the Wifi Access Point I openend before?
I want to print information about connected clients to a console when they connect.
thanks for any help!
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
macCount++;
}
}
}
} catch (Exception e) { e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Android - How to know when Super User is granted to my Application?

My application requires root access. For example this code:
Process p;
try {
p = Runtime.getRuntime().exec("su");
BufferedReader es = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
if((line = es.readLine()) != null)
{
if(line.contentEquals("Permission denied"))
Log.e("TrisTag", "ROOT isn't granted");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So I know when my root access is denied (Read from the error stream). But what's about granted ? How to know ?
Answer from kevin.
public class Root {
private static String LOG_TAG = Root.class.getName();
public boolean isDeviceRooted() {
if (checkRootMethod1()){return true;}
if (checkRootMethod2()){return true;}
if (checkRootMethod3()){return true;}
return false;
}
public boolean checkRootMethod1(){
String buildTags = android.os.Build.TAGS;
if (buildTags != null && buildTags.contains("test-keys")) {
return true;
}
return false;
}
public boolean checkRootMethod2(){
try {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
} catch (Exception e) { }
return false;
}
public boolean checkRootMethod3() {
if (new ExecShell().executeCommand(SHELL_CMD.check_su_binary) != null){
return true;
}else{
return false;
}
}
}
 
/**
* #author Kevin Kowalewski
*
*/
public class ExecShell {
private static String LOG_TAG = ExecShell.class.getName();
public static enum SHELL_CMD {
check_su_binary(new String[] {"/system/xbin/which","su"}),
;
String[] command;
SHELL_CMD(String[] command){
this.command = command;
}
}
public ArrayList<String> executeCommand(SHELL_CMD shellCmd){
String line = null;
ArrayList<String> fullResponse = new ArrayList<String>();
Process localProcess = null;
try {
localProcess = Runtime.getRuntime().exec(shellCmd.command);
} catch (Exception e) {
return null;
}
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(localProcess.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(localProcess.getInputStream()));
try {
while ((line = in.readLine()) != null) {
Log.d(LOG_TAG, "--> Line received: " + line);
fullResponse.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d(LOG_TAG, "--> Full response was: " + fullResponse);
return fullResponse;
}

Categories

Resources