In my Android app I want to detect all Android device names found in the local wireless network. I am able to scan the network and find the devices IP and full qualified domain name (FQDN) like android-2120ee3b45******. I'm doing it like:
final InetAddress inetAddress = InetAddress.getByName(ip);
if (inetAddress.isReachable(400)) {
final String host = inetAddress.getHostName();
final String canHost = inetAddress.getCanonicalHostName();
final String ip = inetAddress.getAddress();
}
With java.net.InetAddress I only get the IP and the network name like android-2120ee3b45******. But I want the Android device name defined by the user on the device like "Peters Fire TV" or "Mikes Samsung SGS6". I saw apps like AllConnect or AllCast which can grab such name from Fire TV (which is a android device).
How can I get the Android device name defined by the user over the WIFI network?
add this line,
for(i=0;i<WifiP2pDeviceList.size();i++){
WifiP2pDevice device = WifiP2pDeviceList.get(i);
String deviceName=device.deviceName;
String devicestatus=device.status;
//so on
}
Related
I trying to create an app that can detect a usb devices. The android device act like a Host, and I now only need to read data of attached devices.
I import android.hardware.usb.UsbDevice
All work fine, but the problem is with any usbDevice methods. I can read the usbDevice.getDeviceName() method but I can't use for example getVersion(), whY?
He say me that cannos resolve this method. Also about other methods.
private String readDevice(UsbDevice device) {
StringBuilder sb = new StringBuilder();
String a = device.getDeviceName(); //work
String b = device.getVersion(); //doesn't work
}
You need to read This Link
It's included in API 23.
String versionStr = UsbDevice.getVersion();
Or you may help from This Link
I've implemented a list with all of my paired devices, and now I'd like to know if it's possible to connect to some of them only with clicking on the item.
For example if my list contains a bluetooth device called X and I want to connect to it (with my app) click on it and the connection is stablished between device and my phone.
This is how I list my paired devices :
myListView = (ListView) dialog.findViewById(R.id.BTList);
BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
myListView.setAdapter(BTArrayAdapter);
pairedDevices = myBluetoothAdapter.getBondedDevices();
for(BluetoothDevice device : pairedDevices)
BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());
If you know the name of the device you wish to pair to you can use an equals comparison.
private static final String DEVICE_WE_WANT_TO MATCH = "X";
String devName = device.getName();
if(devName.equals(DEVICE_WE_WANT_TO MATCH)){
// Connect.
}
You can also use an app UUID
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
This will mean that only devices using this UUID will connect using your protocol, it's an extra layer of security for the app.
In this, latter, case, we're relying on one android device to be acting as a BT server and the other as a BT client.
I am trying to detect the IP of a device (ESP8266), which is acting as a server and is connected to the same network. So that I can send some GET requests to it, but every time my router restarts the device get assigned a different address, so every time I have to lookup its IP from the router and put it in the app to let it start to work again.
I have found something that does my job rorist/android-network-discovery but it will be hard for me to implement it in my app (lack of documentation), secondly my antivirus detects it as a virus.
I am trying to do something like the Belking WeMo app.
What are my options now?
there is no straight way in android where you could do this unless if you have root access, however you could do it the classic way, for example we are sure that the range of the second part range will be always between 0-255 so first thing we need to do is, getting our IP by using this method:
public static String getMyIPAddress() {//P.S there might be better way to get your IP address (NetworkInfo) could do it.
String myIP = null;
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (InetAddress addr : addrs) {
if (!addr.isLoopbackAddress()) {
String sAddr = addr.getHostAddress().toUpperCase();
boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
if (isIPv4)
myIP = sAddr;
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return myIP;
}
method above will return the current IPthat assigned to our phone, from this we could start doing PING to indicates others who are connected to the same network, if the PING ever returned response then definitely the IP exists in the same network follow code below in a background thread:
String ipAddress = getMyIPAddress();
String subnet = ipAddress.substring(0, ipAddress.lastIndexOf("."));
String currentHost;
for (int i = 0; i < 255; i++) {
currentHost = subnet + "." + i;
Process p1 = Runtime.getRuntime().exec("ping -c 1 " + currentHost);
int returnVal = p1.waitFor();
boolean reachable = (returnVal == 0);
if (reachable) {
//currentHost (the IP Address) actually exists in the network
}
}
as you can see in above code, we are looping through between 0-255 and Pining each IP Address whoever falls into reachable is actually within the network.
Both IP Tools and Fing can do network discovery. They are free apps.
I believe they just ping every IP address in the device's subnet and wait for a response. You could make code to do this yourself.
You might want to check out this article for more info on it:
java code to ping an IP address
Tip: Use a ThreadPoolExecutor for good parallelization while waiting for the timeouts.
Why wouldn't you use static IP address assignment? If you have some access restrictions to your router configuration, then you may try some other options:
Remember a MAC-address of your device. Make your application on ESP8266 to request something from server and use arp -a command to find IP - MAC mapping.
Write a service, that answers broadcast/multicast queries and refresh your IP-address information periodically.
Develop an application that gets data from router about IP-address scope and look for you MAC there.
In my android application, BLE connection is working successfully, once BLE device has been connected to Android phone. How to change the connected BLE device name programatically? Sample code like below
private static final UUID Device_Name_UUID = UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb");
private static final UUID Write_UUID = UUID.fromString("00001800-0000-1000-8000-00805f9b34fb");
public void Device_Name(){
BluetoothGattService Name_Service = mBluetoothGatt.getService(Write_UUID );
if(Name_Service == null) {
Log.d(TAG, "Name_Service service not found!");
return;
}
BluetoothGattCharacteristic DeviceName = Name_Service.getCharacteristic(Device_Name_UUID);
if(DeviceName == null) {
Log.d(TAG, "DeviceName charateristic not found!");
return;
}
}
Log.v(TAG, "readCharacteristic(DeviceName) = " + mBluetoothGatt.readCharacteristic(DeviceName));
String i = "123";
DeviceName.setValue(i);
Log.v(TAG, "writeCharacteristic(DeviceName) = " + mBluetoothGatt.writeCharacteristic(DeviceName));
Log.v(TAG, "writeCharacteristic(DeviceName) = " + mBluetoothGatt.writeCharacteristic(DeviceName));
Here mBluetoothGatt.writeCharacteristic(DeviceName); method always returns false.
After some Research I found Below solution
You need to modify the firmware of CC2541 keyfob to add the write permission of device name by adding the code below into KeyFobApp_Init() inside keyfobdemo.c:
uint8 devNamePermission = GATT_PERMIT_READ|GATT_PERMIT_WRITE;
GGS_SetParameter( GGS_W_PERMIT_DEVICE_NAME_ATT, sizeof ( uint8 ), &devNamePermission );
Now my question is, where we need to add these permissions?
Please can anybody tell me that how to add these persmissions?
Thanks in advance
As the name indicates: keyfobdemo.c is a .c file, so TI is telling you that you have to add those permissions in the KeyFobDemo workspace, so you have to program the CC2541 kit in order to change the name. You cannot do it in Android.
All you have to do is:
Download the BLE stack from Texas Instruments website:
http://www.ti.com/tool/ble-stack
Then in the stack, open Projects\ble\KeyFob\CC2541DB\KeyFobDemo.eww file.
Programming of CC2540 kit requires an IDE named IAR Embedded Workbench. You can download it and get access with 30-days trial:
http://www.iar.com/Products/IAR-Embedded-Workbench/8051/
Then open the project and find the keyfobdemo.c file in the APP folder. From line 200-213 there is a char array named deviceName[], which actually defines the advertising name as "Keyfobdemo". You just have to change that to the desired name with correct hex values, and the length of the array as well.
Then in line 236, you have to change attDeviceName[] array as well, since this parameter defines the name of your device once it is in connected state.
No way to do that in Android! unless you change your BLE firmware!
Possible solution, once you connected to your BLE device, you could send some configuration command to your device to change the device name.
I want to find device specification of a mobile phone for examples, the device manufacturer, model no ( and may be types of sensors in the device, wifi chipset etc..). I want to get the device manufacture/model number (eg. Samsung GT-I9100 i.e Galaxy S2) programmatically. The manufacture/model number is also used in Google Play when installing an app. I want to use this information to make some configuration changes in my hardware dependent app (for eg. power measurement).
You can get as below:
String deviceName = android.os.Build.MODEL;
String deviceMan = android.os.Build.MANUFACTURER;
For more other device details, Please refer this document: android.os.Build
I just wanna elaborate on how to use the built in Attribute for the future visitors. You can use this method in identifying a Kindle Device(s)
public static boolean isKindle(){
final String AMAZON = "Amazon";
final String KINDLE_FIRE = "Kindle Fire";
return (Build.MANUFACTURER.equals(AMAZON) && Build.MODEL.equals(KINDLE_FIRE) ) || Build.MODEL.startsWith("KF");
}
String deviceName = android.os.Build.MODEL;
String deviceMan = android.os.Build.MANUFACTURER;
The original solution will work fine, however it will not detect a custom ROM (non OEM) in call cases. You may want to also evaluate the android.os.Build.PRODUCT string or other strings from the Build class.
String build = android.os.Build.PRODUCT;