Android get UUID(Services) of own bluetooth device - android

I am creating a application in which one module is there where i want to retrieve the services supported by my own Bluetooth device...
Currently i am able to fetch the UUID of remote devices, by i havent found out any way to retrieve the UUID of my own device.
Thanks in advance

Finally after a lot of struggling i found a way to find the UUID of own bluetooth device. Sdptool provides the interface for performing SDP queries on Bluetooth devices, and administering a local sdpd. Code snippet for it is follows:This code will only work in devices with root access.
try {
System.setOut(new PrintStream(new FileOutputStream("/mnt/sdcard/abc.txt")));
System.out.println("HelloWorld1");
Process p;
p = Runtime.getRuntime().exec(new String[] { "su", "-c","sdptool", "browse", "local" });
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String s;
String res = "";
while ((s = stdInput.readLine()) != null) {
if(s.contains(""))
System.out.println(s);
Log.e("above -----", s);
}
p.destroy();
return res;
} catch (Exception e) {
e.printStackTrace();
}
and in case you want to discover the services of another Bluetooth device then you can replace "local" with the MAC address of the remote device.
Or you can also try running the sdp tool usinf adb shell as follows:
adb shell sdptool browse local

Related

Detecting all devices connected to my wifi router using Android Programming

I am trying to build an app in Android that would need the IP addresses of all devices (PCs and other mobile devices) connected to a wifi router (my local router). The IP addresses are the ones assigned to the devices by the router using DHCP. Moreover, the app that I am trying to build would be local to a device connected to the same router. I have looked all over the web for Android code that could accomplish this, but all I found was how to scan for wifi access-points. Is what I am trying to do possible using Android programming?
There's no direct API for this. Its not like the wifi router gives everyone a list of all IPs it assigns. You could try pinging every IP on your wifi network (you can tell what IPs those are by netmask), but that will only work if the device is configured to return ICMP packets and your router doesn't block them.
What might work for your app is Wi-fi direct (http://developer.android.com/guide/topics/connectivity/wifip2p.html).
It totally depends on your router: if it has this sort of functionality exposed via API or other. Most routers don't permit this sort of deep-querying. You might look at tomato or dd-wrt if you want to have more control over it.
You can do this by using the arp cache table by:
BufferedReader br = null;
ArrayList<String[]> ipCache = new ArrayList<>(3);
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(" +");
if (split.length >= 4 ) {
if(!split[0].equals("IP") &&!split[0].equals(ROUTER_IP) ){
ipCache.add(split);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int ipsPonged = ipCache.size();
if(ipsPonged>0){
for (String[] client :
ipCache) {
// if one ping test succeeds we are fine
if(!ping(client[0])){
ipsPonged--;
}
}
if(ipsPonged == 0){
return true;
}
}else{
return false;
}

How to send AT commands based on BT Hands-Free profile in android?

I am trying to establish Bluetooth connection between an Android device with other mobile phone over Handsfree profile. I am using following code -
private static final UUID MY_UUID = UUID.fromString("0000111F-0000-1000-8000-00805F9B34FB"); // UUID for Hands free profile
// Some code...
// Get Bluetooth Adapter.
m_oBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Some code...
// For paired BT device, getting a connection established.
if(null != m_oBluetoothDevice)
{
if(BluetoothDevice.BOND_BONDED == m_oBluetoothDevice.getBondState())
{
try
{
m_oBluetoothSocket = m_oBluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
m_oBluetoothSocket.connect();
Log.i(TAG, "Socket Connected");
}
catch(Exception e)
{
if(null != m_oBluetoothSocket)
{
Log.i(TAG, "Closing socket");
try
{
m_oBluetoothSocket.close();
}
catch (Exception e1)
{
Log.i(TAG, "Error while closing socket : " + e1.getMessage());
}
}
}
}
}
I can create RFCOMMSocket using this code.
Now I want to send AT commands based on Bluetooth Hands-Free profile. e.g. If other mobile phone receives a phone call, my Android device can reject this call by sending AT command- "+CHUP". I am not sure whether this is possible or not.
At this point, I am stuck. I have read Bluetooth APIs where I found -
BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
Can we use this Intent for sending AT commands? Is this a proper way to send AT command based on Bluetooth Hands-Free profile? Please someone help me out and give me proper direction.
Any input from you all will be great help for me.
Thanks in advance.
You need to create InputStream and OutputStream so you can talk to the phone:
mmInStream = m_oBluetoothSocket.getInputStream();
mmOutStream = m_oBluetoothSocket.getOutputStream();
To setup the HFP connection you start to send:
mmOutStream.write("AT+BRSF=20\r".getBytes());
Where 20 is code for what you support of HFP.
And to read from the phone:
buffer = new byte[200];
mmInStream.read(buffer);
command = new String(buffer).trim();
So now you can talk beetwen the devices and you can read more about the Handsfree profile on https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=238193
Adding reference to AT commnads
http://forum.xda-developers.com/showthread.php?t=1471241
http://www.zeeman.de/wp-content/uploads/2007/09/ubinetics-at-command-set.pdf

How to get the system ip address after usb tethering of android phone?

I'm developing a mobile application in android.
Here I want to detect the IP address of the computer,system,etc after the usb tethering of the any android phone
I cannot find the solution.
If I put the following code then it takes the only the IP address of phone ,I need IP address of system
The following are code
ArrayList<InetAddress> arrayList=new ArrayList<InetAddress>();
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
arrayList.add(inetAddress);
inetAddress=null;
}
}
} catch (SocketException ex) {
Log.e("SALMAN", ex.toString());
}
return arrayList;
Please help me to get the system's IP address,If we cannot able to get means so please mention me. Because I'm new to android.
I'm using android 1.6 .
There is server side application in the windows xp system. That application is a windows service which is developed by C# .net.
That windows service listen to some port such like 234,etc.If some data comes to port then it will process the data and send response through that port.
In android the android application is send the data to the windows service via socket.
The android phone is USB tethered to the system in which windows service is running.Then system assume android phone is modem and additional IP address is generated for the system.This ip address is dynamically generated when the android phone is tethered.
For data transfer form mobile to system via socket .I will need to give the ip address of the system (after tethered) in my android coding.
If there is any method in android coding to get this IP address.
All are please give your ideas on regarding this.
Its not possible to find IP address created in PC from android after tethering. There is no API or other way to find it.
If you use InetAddress , it will return 192.168.42.129 - which is a DHCP address created by USB Tethering. It wont help you to communicate.
The other way is to scan the list of IP. USB Tethering will create ip ranging for 192.168.42.1 to 192.168.42.255 . You can write a simple scanner to find which one is active. But it will take some time.
Thanks to 'Swim N Swim' above. I found a code at
Retrieve IP and MAC addresses from /proc/net/arp (Android)
and modified a bit to get first IP having valid mac address. Works great when developing as a single user on your PC with tethered. You may follow above link for further selective IPs based on company name etc.
public static String getUSBThetheredIP() {
BufferedReader bufferedReader = null;
String ips="";
try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
if (mac.matches("00:00:00:00:00:00")) {
//Log.d("DEBUG", "Wrong:" + mac + ":" + ip);
} else {
//Log.d("DEBUG", "Correct:" + mac + ":" + ip);
ips = ip;
break;
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ips;
}
Note that each time you tether after untether, you must start your apache or other processes on PC to take new IP effective. THis is what I experienced.

Use TCP in Internet

I'am use TCP for connect my android phone with Windows 7 PC. When I'am send message phone-PC in LAN this system is work, as i`am use this system in Internet she is down because android app send me "time out". Why?
// The host name can either be a machine name, such as "java.sun.com", or a
// textual representation of its IP address
String host = "10.26.144.118";
int port = 20;
try {
Socket socket = new Socket(InetAddress.getByName(host), port);
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
// true for auto flush
writer.println("Hello World");
myView.setText("Send hello world");
} catch (Exception e) {
System.out.println("Error" + e);
myView.setText("Error" + e);
}
You are probably looking for port-forwarding
Your problem is that you mixed up the LAN (local area network) with the WAN (wide area network) aka the internet. Your personal LAN is protected from outside.
You need a static public IP or a DDNS (Dynamic DNS) solution e.g. dyndns. Than you have to forward the traffic from your public IP to you internal Server IP. See also thax's answer.
Than can your smartphone connect to your static public ip or to your DDNS address. Than should your app also work with the mobile network.

Detecting android devices connected to Wifi

I want to make an android application that connects to a Wifi network, say network SSID = "ABC".Assume that it is connected to the Wifi ABC. After connecting to ABC, i would want my application to display the ips of all the android devices that are connected to the same wifi ABC network. How can i achieve that? Thanks
Check out the file: /proc/net/arp on your phone.
It has the ip and MAC addreses of all the other devices connected to the same network. However I am affraid you wont be able to differentiate if they are android phones or not.
You will want to use tcpdump to put the network card into promiscous mode and then capture packets to identify what other clients are on your network.
How to use tcpdump on android:
http://source.android.com/porting/tcpdump.html
You can run commands in your code like so:
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

Categories

Resources