Getting device's Bluetooth MAC address - android

I need to self identify a device using its bluetooth MAC address or UUID. It seems there is no way to get a device's MAC or UUID in Xamarin, Android or iOS. Is there any way to do this? I've tried using the following statement:
macAddress = Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_address");
but this is not working and reruns null, I checked the code behind Secure and there is actually no bluetooth_address. I used the same code and I easily got back bluetooth_on which is in Secure code so I'm sure the code is working but there is not any bluetooth_address.
Any idea how I can do this? If I can't get this my entire design has to change.
Best

Related

how to get mac address in android with python

I am trying to code an addon in XBMC linux environment within Android.
I can see Mac address inside XBMC. But I'd like to grab the mac address for the addon and I can't figure out how.
mac=uuid.getnode()
I have already tried with code like above but gives me numbers only and different everytime when run in android.
could someone give any suggestion please
You can use XBMC InfoLabels
if xbmc.getInfoLabel('Network.MacAddress') != None:
mac_address = xbmc.getInfoLabel('Network.MacAddress')
else:
mac_address = None
If you look at the docs:
Get the hardware address as a 48-bit positive integer. The first time this runs, it may launch a separate program, which could be quite slow. If all attempts to obtain the hardware address fail, we choose a random 48-bit number with its eighth bit set to 1 as recommended in RFC 4122.
The first part explains why it's "numbers only". It's supposed to be a number. If you want that in some particular hex-string format, just format it—e.g., by calling hex().
The last sentence explains why it's "different everytime". If you look at the source, on any non-Windows platform, getnode will try _unixdll_getnode, then _ifconfig_getnode, then fall back to a random number. The former requires a function called uuid_generate_time in either libuuid or libc, which doesn't exist on Android. The latter runs the command ifconfig with a series of different flags and searches for specific strings, and falls back to arp and lanscan. Again, none of this works on Android.
There is no recommended way to get the MAC address on Android, mainly because they don't want you to get one. This blog post explains why, and this SO question (especially Seka Alekseyev's answer) adds more detail. Some apps try persisting the MAC address once they've gotten it, and never checking again, which gets around some of the problems, but not most of them.
There is a Java API to get the MAC for each service where it makes sense—WiFi, 3G, Bluetooth, etc. It's up to you to decide which is "the" MAC, and you need the right permissions (e.g., android.permission.ACCESS_WIFI_STATE), and there may be no value or a garbage value, but you can get it with code like this:
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String mac = wm.getConnectionInfo().getMacAddress();
As far as I know, there's nothing in SL4A or any other Android Python distribution that exposes these functions directly, so you'll have to write your own wrapper.
You can get mac address on this easy way, assume "eth0" is your network device name:
o = open('/sys/class/net/eth0/address', 'r')
mac_address = o.read().strip() #on "ff:ff:ff:ff:ff:ff" form

Android WiFi Direct Demo Issues

im testing the new technology wifi direct and im having some issues using
the wifi direct demo from the samples that come with the android-sdk.
So, I have two devices A and B, both with android 4.0.3.
First, from device A, I send a file to B. Nothing wrong here, B
receives the file.
Then A disconnects from B.
Now, from device B I try to send a file to A.
But the device that receives the file is B, instead of A.
To fix, i need to turn off and on both devices...
Also, sometimes when i click disconnect and try to
connect again, connection fails and i have to disable and
enable wifi direct...
Anyone else experiencing this?
Is it because the new technology is not mature yet or maybe
something wrong with my build/driver/etc or maybe this demoapp
doesnt support two-way sharing.
Any ideas and/or explanations would be apreciated.
When providing a WifiP2pConfig instance to the connect() function, you can set the groupOwnerIntent property of this configuration object as follows:
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = "..."; // insert ip here
config.groupOwnerIntent = 0;
config.wps.setup = WpsInfo.PBC;
manager.connect(..., config, ...);
From the android reference:
This (the groupOwnerIntent) is an integer value between 0 and 15 where
0 indicates the least inclination to be a group owner and 15 indicates
the highest inclination to be a group owner.
Furthermore, the demo probably repeatedly sends the file to the same device because there is always made a socket connection to the ip-address obtained from:
WifiP2pInfo.groupOwnerAddress
If you would like to support bidirectional communication, the first step in setting this up would be sending the ip-address of the non group owner to the group owner.
As far as the disconnect/reconnect problem goes, I seem to have the same inconsistencies with Android 4.0.2 devices.
I have been trying for a while to transfer files between two devices using wifi direct. I have use the Android SDK WifiDirectDemo as base. My experience:
GO address is always the same (at least in Samsung Nexus), but this is not really a problem, because you can use this to know who is the server (or client).
Another strange thing was that MAC address of devices were different when you got it from Android WifiManager and when you read it from "/proc/net/arp" file.
At the end I did it, and you can see the code here.
I hope it helps you!
I have been struggling with the same problem lately. I suppose this is an OS issue. To give you a brief background, I have installed Wi-Fi Direct application to both devices with different OS versions, one with OS 4.0.1 and one with OS 4.0.2. The connection fails from time to time when I disconnect and reconnect the devices. It goes same while searching for devices too. But the thing is, this only happens on the device with OS 4.0.2. Other device does not crash or disconnect.
While searching for that problem, I have found the links below. People discussed about that and they share the same idea. Apparently this is an OS 4.0.2 issue. I am not sure if it is the same for OS 4.0.3 but there is no problem with the previous version OS 4.0.1 for sure.
Here are the links:
http://code.google.com/p/android/issues/detail?id=24402
http://osdir.com/ml/android-platform/2012-01/msg00226.html

Get MAC Address of android device without Wifi

How do I get the MAC-Address of the network interface of an android device which doesn't have a Wifi-Interface (e.g. the android emulator)? WifiInfo obtained via the WifiManager returns null.
EDIT
To be more clear: I have to communicate with an existing network protocol (not designed by me) on the local network where I have to send the mac address of the communicating interface within the payload during a registration phase.
I'm going to take a leap and assume that you want this MAC address in order to establish a unique identifier for the device. Mac Addresses are not the way to do this.
There's an Android Developer Blog post titled "Identifying App Installations" which covers the topic of generating unique ID's fairly well, including the popular methods, and the pros/cons. It's definitely worth a read. Quite relevant to this post is the following quote:
It may be possible to retrieve a Mac address from a device’s WiFi or Bluetooth hardware. We do not recommend using this as a unique identifier. To start with, not all devices have WiFi. Also, if the WiFi is not turned on, the hardware may not report the Mac address.
The options available to you instead include TelephonyManager.getDeviceId(), android.os.Build.SERIAL, and Settings.Secure.ANDROID_ID, all of which are covered in more detail in the linked post.
Read /sys/class/net/[something]/address as a text file
But it's unlikely to be useful in the way you think.
See this post where I have submitted Utils.java example to provide pure-java implementations.
Utils.getMACAddress("wlan0");
Utils.getMACAddress("eth0");
Utils.getIPAddress(true); // IPv4
Utils.getIPAddress(false); // IPv6
What is the network interface you want the MAC address of? If there's no wifi, you certainly can't get the wifi device's MAC address. It represents the physical hardware and if that's not present, it simply doesn't exist.
To get wifi MAC of android device using adb:
adb shell getprop ril.wifi_macaddr
Use the following code in Java to get it programmatically:
Process p = Runtime.getRuntime.exec("adb", "shell", "getprop", "ril.wifi_macaddr")
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream());
String macAddress = br.readLine();

Get IMEI code or/and Mac Address of a Device

I'm implementing an way to unique identify the device.
The architect send me the following specs:
devices with Wifi + 3G: IMEI Code
devices Wifi only - MacAddress
Both needs also the Manufacturer + Model for complete the id.
My questions are:
How do I get the IMEI from the device?
How can I get the Manufacturer and Model from the device? (I see theres some constants on Build class for it, but don't know where to use them)
Theres devices with 3G only?
How can I know if the device is wifi only, wifi+3g or 3g only if exists?
Also, suggestions for unique identifiers are available.
PS: I'm already able to get the MacAddres through WifiManager.
You can use the TelephonyManager getDeviceID method. This should return IMEI or MEID. For build information you can see android.os.Build and android.os.Build.VERSION. Also, if its about uniquely identifying your application installs, you may want to go through (and/or show your architect) the developer blog post on Identifying App Installations
How to get IMEI can be found in this question: How to get the device's IMEI/ESN programmatically in android?;
MANUFACTURER and MODEL can be retrieved from android.os.BUILD class.
Theoretically, yes, there might be devices without Wi-Fi but with 3G. But I haven't seen any.
If getSystemService(WIFI_SERVICE) returns null, then there is no Wi-Fi.
android.telephony.TelephonyManager.getDeviceId() gets you 'the IMEI for GSM and the MEID or ESN for CDMA phones'.
Build.MODEL is a static field, so you can just use it like that, so long as you've imported android.os.Build first.
Quite possibly.
You could try calling Context.getSystemService(Context.WIFI_SERVICE) and seeing if it's null, although I suppose it's possible that the service might still exist even if there's no wifi (in such a case, I would expect checking the wifi to return DISABLED.)
These questions have plagued Android developers for a while, see Is there a unique Android device ID?
The Android development team has tried to address these concerns directly in their blog here: http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
In answer to your last question: just test for null when querying for SIM/MAC/Whatever and then act accordingly. It's been reported that there are cases where MAC will return null; see my first link.

get the MAC address of the closest WiFi using Android

Q/ Write an Android program to get the MAC address of the closest WiFi Access Point
is there any ready written program i can use ?
dose any body have information that might help me ?
thank you
See this:
http://developer.android.com/reference/android/net/wifi/WifiManager.html#getScanResults()
The BSSID is the MAC address of the AP.

Categories

Resources