Bluetooth adapter device name and MAC address in Android

Today I am going to show you how to get the bluetooth adapter deviceĀ  name (if exists) and MAC address programmatically in Android. Note that this code will not work in emulator as it does not support bluetooth. To avoid a possible exception we check if mBluetoothAdapter==null in lines 09 and 25.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * get bluetooth local device name
 * @return device name String
 */
public static String getLocalBluetoothName() {
	BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

	// if device does not support Bluetooth
	if(mBluetoothAdapter==null){
		Log.d(TAG,"device does not support bluetooth");
		return null;
	}
	
	return mBluetoothAdapter.getName();
}

/**
 * get bluetooth adapter MAC address
 * @return MAC address String
 */
public static String getBluetoothMacAddress() {
	BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

	// if device does not support Bluetooth
	if(mBluetoothAdapter==null){
		Log.d(TAG,"device does not support bluetooth");
		return null;
	}
	
	return mBluetoothAdapter.getAddress();
}

The above methods require javaandroid.permission.BLUETOOTH uses permissions in AndroidManifest.xml file.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy