我正在尝试比较配对设备中的MAC地址,以确保其在应用中的两个已知地址之一。 我使用以下内容获取设备
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = btAdapter.getRemoteDevice(address1);
mmDevice = device;
}
所以我想做的就像是
if(foundMacAddress == address1){
BluetoothDevice device = btAdapter.getRemoteDevice(address1);
}else{
BluetoothDevice device = btAdapter.getRemoteDevice(address2);
}
但是我不知道如何检索和比较MAC地址。
答案 0 :(得分:1)
您可以在 BluetoothDevice 对象中使用 .getBluetoothClass(),getMajorDeviceClass()方法,这些方法可以从代码中的pairedDevices获得。其余的只是字符串比较。 此外,这可以帮助您:How to get the bluetooth devices as a list?
答案 1 :(得分:0)
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
您可以查看on onActivityResultfunction上的活动,以便像这样找到 MAC地址
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;
}
}
答案 2 :(得分:0)
我发现我可以遍历配对的设备,并检查地址:
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;