Android 蓝牙连接检查 mac 个地址
Android bluetooth connect check mac addresses
我正在尝试比较来自配对设备的 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 地址。
您可以在 BluetoothDevice 对象上使用方法:.getBluetoothClass() , getMajorDeviceClass(),可以从pairedDevices,在你的代码中。剩下的只是字符串比较。
此外,这可能对您有所帮助:How to get the bluetooth devices as a list?
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
你可以在 onActivityResult 函数中查看 activity 来找到 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;
}
}
我发现我可以循环遍历配对的设备,并以这种方式检查地址:
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;
我正在尝试比较来自配对设备的 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 地址。
您可以在 BluetoothDevice 对象上使用方法:.getBluetoothClass() , getMajorDeviceClass(),可以从pairedDevices,在你的代码中。剩下的只是字符串比较。 此外,这可能对您有所帮助:How to get the bluetooth devices as a list?
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
你可以在 onActivityResult 函数中查看 activity 来找到 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;
}
}
我发现我可以循环遍历配对的设备,并以这种方式检查地址:
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;