Reputation: 3426
I am sending an image via bluetooth in android and want to fetch the MAC address of Device to which the image is being sent.
Please find below my code.
private void bluetoothadd(){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Log.e("Bluetooth ","not found");
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
Log.e("Mac Addressess","are: "+mBluetoothAdapter.getRemoteDevice(device.getAddress()));
}
}
}
}
I am getting all paired device's MAC Address. I want the MAC Address of a device only to which data is being transmitted.
Upvotes: 7
Views: 43193
Reputation: 547
private String connectedDeviceAdd = "";
private BluetoothDevice connectedDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(this.mReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
connectedDeviceAdd = device.getAddress();
connectedDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(connectedDeviceAdd);
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Log.e(TAG, "Device Disconnected");
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(this.mReceiver);
}
Upvotes: 1
Reputation: 1338
This work for me:
String macAddress = android.provider.Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_address");
Upvotes: -1
Reputation: 59
Use this:
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
Upvotes: 5
Reputation: 6887
When the intent is fired to connect to the remote device and the device is successfully established the Device Address is returned as extra data with the Flag EXTRA_DEVICE_ADDRESS
.
You can check for the connection and establish it
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
You can check the activity in the on onActivityResult
function to find the address like this
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String add = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
address= add.toString();
// Get the BluetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
}
break;
}
}
This trick is used in the Bluetooth Chat Sample application you can find in the Examples folder of the SDK
Upvotes: 1
Reputation: 17844
So, it sounds like you want to get the bd_addr/mac of a device that you have a connection with? Then note that the BluetoothSocket class has a member 'getRemoteDevice', which returns a BluetoothDevice instance representing the device you are connected to, on which you can call getAddress() to get the MAC.
Or you can register for ACTION_ACL_CONNECTED which contains 'EXTRA_DEVICE' that will lead you to a BluetoothDevice.
Upvotes: 0
Reputation: 758
Try it.
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();
Upvotes: -3