I'm just trying to get automatically the active device on my System.
For Example:
My PC has two devices.
1. TAP-Windows Adapter V9
2. Intel(R) Ethernet Connection
Actually the active device is the Intel Connection.
So I want that my application can automatically use the active device to dump a pcap.
My Idea is to search for the Subnetmask in both devices.
The active device has an INET4 IP like this mask=[INET4: 255.255.255.0].
The deactive one gives me this: mask=[0]
Is it the right thinking, that an active device never gets a mask of 0?
This extends also for the broadcast.
Here is my implementation:
static public PcapIf selectActiveDev(List alldevs){
PcapIf device = new PcapIf();
for(int a = 0; a<=alldevs.size()-1; a++){
if(alldevs.get(a).getAddresses().get(0).getNetmask().toString() != "0"){
device = alldevs.get(a);
}
}
return device;
}
I'm using JNetPcap 1.3.0 with eclipse. :)
Related
I have an android application related to bluetooth and I have a question. How can I find out which device is connected to a smartphone? I need to know the device model, I can get the device name, but the user can change it, for example “MEIZU EP51” == >> “My favorite headphones”. I need to get the device model or ID, for example, I connected the Meizu EP51 headphones and I need the phone to recognize exactly the model of these headphones. In the Android documentation, I did not find it, maybe I did not read carefully, I would be grateful for the answer.
You can get the connected ble devices by Bluetooth Manager like this,
BluetoothManager bluetoothManager = (BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
As i have Mi Band 3 i can see this connected device over it. But as it is BLE device so it is visible to me but not sure about other devices.
By BluetoothDevice object you can get device name and address. Try it.
I have made an android app which on startup gets the current wifi network and connects to a different one. At least that is what it is supposed to do:
wifiInfo = wifiManager.getConnectionInfo();
OldNetworkID = wifiInfo.getNetworkId(); //save current network
WDTNetworkID = wifiManager.addNetwork(wificonfiguration); //add new network
wifiManager.disconnect();
wifiManager.enableNetwork(WDTNetworkID, true); //enable new network and disable all others
wifiManager.reconnect();
When I debug I can see wificonfiguration contains the right SSID (the SSID of the new network).
After addNetwork() I see that wifiManager.getConfiguredNetworks() contains this new network with the right SSID and the same networkID as WDTNetworkID. At this point the network is enabled.
But after enableNetwork() instead of WDTNetworkID enabled and the rest disabled I see that OldNetworkID is enabled and the rest is disabled.
Am I doing something wrong?
I have added a picture of a couple of watches while debugging.
You can see here that the old network is enabled and the rest is disabled.
The problem here is that the device the app is running on is android 5.1.1.
As it states in the documentation of enableNetwork():
Note: If an application's target SDK version is LOLLIPOP or newer, network communication may not use Wi-Fi even if Wi-Fi is connected; traffic may instead be sent through another network, such as cellular data, Bluetooth tethering, or Ethernet. For example, traffic will never use a Wi-Fi network that does not provide Internet access (e.g. a wireless printer), if another network that does offer Internet access (e.g. cellular data) is available. Applications that need to ensure that their network traffic uses Wi-Fi should use APIs such as bindSocket(java.net.Socket), openConnection(java.net.URL), or bindProcessToNetwork(Network) to do so.
You just need add this line to your code. i had same problem, adding this line help me.conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//After sleep a little milliseconds then check exist connection with this code:
try {
Thread.sleep(3000);
Toast.makeText(MyActivity.this, "You are connected to " +
mainWifimanagerObject.getConnectionInfo().getSSID(), Toast.LENGTH_LONG).show();
}
catch (InterruptedException e) {e.printStackTrace();}
I have an app that I use to connect to WiFi network that doesn't have connection to the Internet. It has been working fine with versions before Nougat. Since I upgraded my phone to Nougat few weeks ago, I'm not able to connect to the same WiFi network anymore. It connected briefly and disconnected and then rolled back to the previous network that has connection to the Internet. Below is the code that was working fine before Nougat.
WifiManager manager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration selectedConfig = new WifiConfiguration();
selectedConfig.SSID = ssid;
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
manager.addNetwork(selectedConfig);
List<WifiConfiguration> cofigs = manager.getConfiguredNetworks();
for( WifiConfiguration config : cofigs)
{
if(config.SSID != null && config.SSID.equals(selectedConfig.SSID))
{
manager.disconnect();
manager.enableNetwork(config.networkId, true);
break;
}
}
In the past, setting true in manager.enableNetwork(config.networkId, true) seemed to let the connection stay on with the network that doesn't have connection to the internet. However, since Nougat, this doesn't seem to work anymore.
Does anyone know what's going on and how to make it work?
Thanks.
the boolean parameter from the enableNetwork method is to block the other networks.
Actually, there is no way for you to tell the device to connect to certain network, the only thing that you can do is to block the other networks and that way, the only option that the device has to connect is the one that you didn't disable (the one with the id on the 1st parameter).
I would recommend you to try and check what is returned from the addNetwork and check this google issue Google issue .
If that doesn't solve your problem, check if you are scanning somewhere else. If you scan for the available networks, your device will enable the other networks, so the others won't be disabled anymore and your device will connect to the one that it was in the beginning or the one that has higher priority.
Hope it helped
I am writing an app to add a secure wifi enterprise configuration on android. The problem is this app is working as intended on my Samsung galaxy S4 mini but on the second phone a LG L40 it returns allways -1 when i add the connection. The permissions are set.
Here are some code parts:
wifiManager = (WifiManager)appContext.getSystemService("wifi");
returnValue = wifiManager.addNetwork(wifiConfig);
The SSID is quotet in the wifiConfig. I think it is a device specific problem. Is there a way to get the RemoteException in the WifiManager?
I'm writing a program that speaks with an external accessory over rfcomm.
My problem is that I don't know what the correct way of identifying my device is.
the way I do it now is like this:
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals(MY_DEVICE_NAME)) {
this.myDevice = device;
break;
}
}
This method however relies on the name of the device which to me seems dirty and bad :)
is there a better way to do this?
I tried looking at all the methods of BluetoothDevice but none seemed to help - is the name really the best way to do it?
I saw that in some places people say that I should use UUIDs but that is used to open the socket to the device once I have it:
_socket = myDevice.createRfcommSocketToServiceRecord(MY_UUID);
is there a better way to do it?
Devices of the same kind/functionality and/or brand will usually have a similar name. For example, all RN-41 devices from Roving Networks have the following name:
FireFly-XXXX
where XXXX is the last 4 digits of the device's address. That means you can use the following to connect to any of them:
if (device.getName().startsWith("FireFly-")) {
this.myDevice = device;
break;
}
This is exactly what I do in my app and haven't found any more reliable/consistent way to do it. As a generalization, you should be able to use a regular pattern if the name in the devices you are interested in is any more complex than the example above.
You can use myDevice.getAddress() to get the bluetooth device address and compare, it will always be unique (unlike name)
You can also use BluetoothDevice.getBluetoothClass() for at narrowing down which devices might be relevant.
BluetoothClass.getMajorDeviceClass() will tell you roughly what kind of device it is - a phone, a computer, an audio or video device, or whatever.
BluetoothClass.hasService() further specifies some capabilities of the device.
Within each of the major classes, some minor classes are defined - what kind of computer / audio-video device / phone / health equipment etc. it is.
Also, on recent versions of the Android platform (API level 15+), you can query for the service records of a device, without having to connect to it. See BluetoothDevice.fetchUuidsWithSdp() and BluetoothDevice.getUuids().