我们如何在搜索蓝牙设备时只获取android设备的mac地址?

时间:2014-09-27 12:19:20

标签: android bluetooth android-bluetooth

我只想在聊天应用中搜索蓝牙设备时只包含手机和平板电脑的Android设备mac地址。

1 个答案:

答案 0 :(得分:2)

我猜你已经知道如何获取所列设备的Bluetooth MAC地址,但我会在此列出完整性:

private static BluetoothAdapter getDeviceAdapter() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return bluetoothAdapter;
}

private static String getMacAddress() {
    String macAddress = getDeviceAdapter().getAddress();
    return macAddress;
}

要确定Bluetooth设备是智能手机还是平板电脑,请执行以下操作:

private static boolean isPhoneOrTablet(int deviceClass) {
    // Tablets are defined as "COMPUTER_HANDHELD_PC_PDA"
    // while smart phones are defined as "PHONE_SMART"
    if ((deviceClass == BluetoothClass.Device.COMPUTER_HANDHELD_PC_PDA)
        || (deviceClass == BluetoothClass.Device.PHONE_SMART)) {
        return true;
    }
    return false;
}

使用isPhoneOrTablet方法的结果执行任何操作。 deviceClass参数派生自BluetoothDevice.getBluetoothClass().getDeviceClass()方法。

要同时检查多个Bluetooth已发现的设备,请使用以下循环:

for (BluetoothDevice device : devices) {
    if (isPhoneOrTablet(device.getBluetoothClass().getDeviceClass())) {
        Log.i("TESTING", getDeviceAdapter().getName());
    }
}