Reputation: 1205
I am trying to compare the MAC address from a paired device to make sure its one of two known addresses in the app. I am using the following to get the device
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = btAdapter.getRemoteDevice(address1);
mmDevice = device;
}
So what I want to do is something like
if(foundMacAddress == address1){
BluetoothDevice device = btAdapter.getRemoteDevice(address1);
}else{
BluetoothDevice device = btAdapter.getRemoteDevice(address2);
}
However I am uncertain as to how I can retrieve and compare the MAC address.
Upvotes: 2
Views: 943
Reputation: 1205
I found that I could loop through the paired devices, and check the address that way:
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
String conn = null;
for(BluetoothDevice device : pairedDevices)
{
String theAddress = device.getAddress();
if(theAddress == address1) {
conn = address1;
}else{
conn = address2;
}
Log.d("CONNECTION: ",conn);
}
BluetoothDevice device = btAdapter.getRemoteDevice(conn);
mmDevice = device;
Upvotes: 0
Reputation: 12953
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
You can check the activity in the on onActivityResultfunction to find the MAC 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;
}
}
Upvotes: 0
Reputation: 12986
You can use the methods : .getBluetoothClass() , getMajorDeviceClass() on the BluetoothDevice object, which can be gained from the pairedDevices, in your code. The rest is just string comparison. Also, this may help you : How to get the bluetooth devices as a list?
Upvotes: 1