Java - Wifi API - java

I am trying to find out if there is a Wifi API for Java. Something that can connect to Wifi networks and scan them (to find devices). I can't seem to find something like that. Any suggestions? Thanks!
P.S.
I know about the WifiManager for Android, but I am not developing for Android, I am developing with JDK 6.

Wireless networking cards differ greatly depending on manufacturer and even version, and most operating systems do not have a standardized way of interacting with them. Some computers do not even come with wireless cards. The reason it works so well with Android is because Google can guarantee that every phone that has Android installed has a proper wireless networking interface.
tl;dr no, sorry

You can take help of command line tools to get list of available networks using command "netsh wlan show networks mode=Bssid".
Try below java method.
public static ArrayList scanWiFi() {
ArrayList<String> networkList = new ArrayList<>();
try {
// Execute command
String command = "netsh wlan show networks mode=Bssid";
Process p = Runtime.getRuntime().exec(command);
try {
p.waitFor();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(p.getInputStream())
);
String line;
StringBuilder sb = new StringBuilder();
String ssidArr[];
while ((line = reader.readLine()) != null) {
//System.out.println(line);
if (line.contains("SSID ") && !line.contains("BSSID ")) {
sb.append(line);
networkList.add(line.split(":")[1]);
//System.out.println("data : " + ssidArr[1]);
}
}
//System.out.println(networkList);
} catch (IOException e) {
}
return networkList;
}

Related

Get data usage for Wi-Fi and cellular network in Android java for Android versions above pie

I would like to know how to determine the data usage for the specific application above Android version pie programmatically. I found from Android 10 onwards subscriber Id is not accessible to third party applications. Hence I'm not able to fetch data usage using networkStatsManager. But I found there are applications in Play store which provide the solution . I would like to know the implementation for the same.
I'm not sure on this but they actually use the /proc/net/dev file generated by the phone to get the data .
An example to do that without permissions would be to use a function like this
public static long[] getNetworkUsageKb() {
BufferedReader reader;
String line;
String[] values;
long[] totalBytes = new long[2];//rx,tx
try {
reader = new BufferedReader(new FileReader("/proc/net/dev"));
while ((line = reader.readLine()) != null) {
if (line.contains("eth") || line.contains("wlan")){
values = line.trim().split("\\s+");
totalBytes[0] +=Long.parseLong(values[1]);//rx
totalBytes[1] +=Long.parseLong(values[9]);//tx
}
}
reader.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
//transfer to kb
totalBytes[0] = totalBytes[0] / 1024;
totalBytes[1] = totalBytes[1] / 1024;
return totalBytes;
}
SO Question on data in proc-net-dev

How to know exact model of Android device? (Like Samsung S7 'herolte') [duplicate]

This question already has answers here:
Get Android Phone Model programmatically , How to get Device name and model programmatically in android?
(16 answers)
Closed 3 years ago.
I wonder how to find the exact model of a device as mentioned in the different analytics reports. Example: where to find on my device the 'herolte' of "Samsung S7 (herolte)"?
There are a lot of device properties in android.os.Build : https://developer.android.com/reference/android/os/Build
check some of them that look suitable, like DISPLAY, BOARD, BRAND
"herolte" can be retrieved from the ro.build.product system property (at least for Samsung devices):
public String getProduct() {
Process p;
String product = "";
try {
p = new ProcessBuilder("/system/bin/getprop", "ro.build.product").redirectErrorStream(true).start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
product = line;
}
p.destroy();
} catch (IOException e) {
e.printStackTrace();
}
return product;
}
If ro.build.product is not correct, you can run adb shell getprop and check the name of property you want.
I'm just not sure if ro.build.product is available for all brands. You mentioned Samsung S7.. ro.build.product works with Samsung devices.
NOTE
I used the code from this answer to build this one

Java runtime exec getInputStream when process pauses

I am creating a wrapper for a executable that runs on the windows command line. The executable takes a few commands then attempts to connect to another device. then it outputs and ERROR! or Ready For "Device Name" i do not get this message until the app exits. The problem is this app is a tunnel allowing me to run telnet on the external box but i need to make sure the Device is ready this is my code.
public void startUDPTunnel() {
//TODO Pull Amino serial number from webportal
Properties prop = new Properties();
InputStream inConfig = getClass().getClassLoader().getResourceAsStream("config.properties");
try {
prop.load(inConfig);
} catch (IOException e) {
System.out.println(e.getMessage());
}
String server = prop.getProperty("server");//config.GetProp("server");
System.out.println(server);
String port = prop.getProperty("port");//config.GetProp("port");
System.out.println(port);
String location = prop.getProperty("location");//config.GetProp("location");
System.out.println(location);
String url = prop.getProperty("URL");
System.out.println(url);
String input = "";
try {
input = getSerial(url);
System.out.println(input);
Process p = Runtime.getRuntime().exec(location+"udptunnel.exe -c 127.0.0.1 23 "+input+" "+server+" "+port+" 127.0.0.1 23");
threadSleep();
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
if(line.equals("ERROR!")){
System.out.println("There was an ERROR");
}
if(line.equals("Ready for \""+input+"\"")){
System.out.println("Load Telnet");
}
}
p.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
Sorry there is a lot of debug code left in this function.
EDIT
OK I am pretty sure know what the issue is bufferReader.readLine() requires a \n or \r or just hangs is there anyway to watch the stream with out the buffer?
You should use a ProcessBuilder, and then use redirectErrorStream(). I think this will cause stdout of the process to be unbuffered. And even if it doesn't, you'll only have to read from one InputStream to get both stdout and stderr.
I have figured out my problem the applications that i am executing with java do not have a EOL at the end of the line in fact they just hang on the line For example telnet waits for the username then the password. i am not sure this is proper but it works and is what i am going to use for now
while((i=br.read())!=-1){
ch += (char)i;
}
This outputs every char as they come in when then i just make sure the string contains what i am looking for!

how to get the details of USB devices connected to computer using java

I am trying to get the details of USB hardware devices connected to computer but I don't know the native code of windows so is it possible to get the details of hardware connected to computer using JAVA Thanks in advance
vbscript code:
Set HDs = GetObject("winmgmts:(impersonationLevel=impersonate)")
Set colItem=HDs.ExecQuery("Select * from Win32_DiskDrive")
For Each hd In colItem
Wscript.Echo hd.PnPDeviceID & "vigi"
Next
java code:
try {
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch(Exception e){
e.printStackTrace();
}
now i'm trying to get the details using this vbscipt code but when i'm executing this code the error comes no script found
Try using JUsb. The link here provides simple example.

Java Killswitch

I've created a java application I'm selling for money, and the verification system involves using an unique HWID to ID the computer to see if they've paid. I was wondering if there was a way for a java application to "kill" itself, maybe deleting some of it's own class files, corrupting itself, or overriding itself.
Is there any way?
Make it web based, keep records in the database, make the user log in to use the system. Any dedicated cracker will defeat your system in a matter of time.
If this is a commercial grade app, then I would recommend using a security solution designed by professionals. Security and Cryptography is best left to experts
Layman solution :
Could you execute a getmac (assuming this app runs out of windows) from within your system and do the check.? MAC ids are assumed to be unique for a PC. There are ways to override it but should address 90% of the cases.
Corrupting your app doesn't seem to be a good solution.
public static String getURLSource(String link) {
try {
URL url = new URL(link);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder str = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null) {
str.append(inputLine);
}
reader.close();
return str.toString();
} catch (Exception e) {
throw new RuntimeException("Couldn't properly connect to internet.");
}
}
public void main(String[] args) {
if(!getUrlSource("yourlink").contains("a string you want when it's not killswitched")) { //The link must be readable text by java
//Do stuff here
}
}

Categories