Bluetooth adapter device name and MAC address in Android

less than 1 minute read

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.

/**
 * 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