简体   繁体   中英

How we get only android device mac address during searching of bluetooth device?

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

I guess you already know about getting the Bluetooth MAC address of a listed device, but I'll list it here for completeness:

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

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

To determine whether a Bluetooth device is a smart phone or a tablet, do this:

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;
}

Do whatever you want with the result of the isPhoneOrTablet method. The deviceClass parameter is derived from BluetoothDevice.getBluetoothClass().getDeviceClass() method.

To check multiple Bluetooth discovered devices at the same time, use a loop like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM