@Test public void testIsValidBluetoothMacAddress() throws Exception { assertThat("The BT MAC is not valid if null is provided", BluetoothUtils.isValidBluetoothMacAddress(null), is(false)); assertThat("The BT MAC is not valid if empty string is provided", BluetoothUtils.isValidBluetoothMacAddress(""), is(false)); assertThat("The BT MAC is not valid if not hexadecimal digits provided", BluetoothUtils.isValidBluetoothMacAddress("ff:gg:hh:ff:gg:hh"), is(false)); assertThat("The BT MAC is not valid if five groups of two hexadecimal digits used", BluetoothUtils.isValidBluetoothMacAddress("00:11:22:33:44"), is(false)); assertThat("The BT MAC is not valid if seven groups of two hexadecimal digits used", BluetoothUtils.isValidBluetoothMacAddress("00:11:22:33:44:55:66"), is(false)); assertThat("The BT MAC is not valid if six groups of two hexadecimal digits not" + " separated by colons used", BluetoothUtils.isValidBluetoothMacAddress("00:11:22:33:44:55:66"), is(false)); assertThat("The BT MAC is not valid if lower case chars used", BluetoothUtils.isValidBluetoothMacAddress("0a:1b:2c:3d:4e:5f"), is(false)); assertThat("The BT MAC is valid if six colon separated groups of two hexadecimal digits used", BluetoothUtils.isValidBluetoothMacAddress("00:11:22:33:44:55"), is(true)); assertThat("The BT MAC is valid if six colon separated groups of two hexadecimal digits used", BluetoothUtils.isValidBluetoothMacAddress("0A:1B:2C:3D:4E:5F"), is(true)); }
public static boolean isValidBluetoothMacAddress(String bluetoothMacAddress) { boolean isValid = false; if (!isBluetoothMacAddressUnknown(bluetoothMacAddress) && bluetoothMacAddress.length() >= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MIN && bluetoothMacAddress.length() <= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MAX) { String[] bytesAsHexStringArray = bluetoothMacAddress.split(BLUETOOTH_ADDRESS_SEPARATOR); if (bytesAsHexStringArray.length == BLUETOOTH_ADDRESS_BYTE_COUNT) { boolean allBytesAreValid = true; for (String byteAsHexString : bytesAsHexStringArray) { if (byteAsHexString.length() == 0 || byteAsHexString.length() > 2 || !byteAsHexString.matches(UPPER_CASE_HEX_REGEXP_CONDITION)) { allBytesAreValid = false; break; } } isValid = allBytesAreValid; } } if (!isValid && bluetoothMacAddress != null) { Log.e(TAG, "isValidBluetoothMacAddress: Not a valid Bluetooth MAC address: " + bluetoothMacAddress); } return isValid; }
BluetoothUtils { public static boolean isValidBluetoothMacAddress(String bluetoothMacAddress) { boolean isValid = false; if (!isBluetoothMacAddressUnknown(bluetoothMacAddress) && bluetoothMacAddress.length() >= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MIN && bluetoothMacAddress.length() <= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MAX) { String[] bytesAsHexStringArray = bluetoothMacAddress.split(BLUETOOTH_ADDRESS_SEPARATOR); if (bytesAsHexStringArray.length == BLUETOOTH_ADDRESS_BYTE_COUNT) { boolean allBytesAreValid = true; for (String byteAsHexString : bytesAsHexStringArray) { if (byteAsHexString.length() == 0 || byteAsHexString.length() > 2 || !byteAsHexString.matches(UPPER_CASE_HEX_REGEXP_CONDITION)) { allBytesAreValid = false; break; } } isValid = allBytesAreValid; } } if (!isValid && bluetoothMacAddress != null) { Log.e(TAG, "isValidBluetoothMacAddress: Not a valid Bluetooth MAC address: " + bluetoothMacAddress); } return isValid; } }
BluetoothUtils { public static boolean isValidBluetoothMacAddress(String bluetoothMacAddress) { boolean isValid = false; if (!isBluetoothMacAddressUnknown(bluetoothMacAddress) && bluetoothMacAddress.length() >= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MIN && bluetoothMacAddress.length() <= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MAX) { String[] bytesAsHexStringArray = bluetoothMacAddress.split(BLUETOOTH_ADDRESS_SEPARATOR); if (bytesAsHexStringArray.length == BLUETOOTH_ADDRESS_BYTE_COUNT) { boolean allBytesAreValid = true; for (String byteAsHexString : bytesAsHexStringArray) { if (byteAsHexString.length() == 0 || byteAsHexString.length() > 2 || !byteAsHexString.matches(UPPER_CASE_HEX_REGEXP_CONDITION)) { allBytesAreValid = false; break; } } isValid = allBytesAreValid; } } if (!isValid && bluetoothMacAddress != null) { Log.e(TAG, "isValidBluetoothMacAddress: Not a valid Bluetooth MAC address: " + bluetoothMacAddress); } return isValid; }  }
BluetoothUtils { public static boolean isValidBluetoothMacAddress(String bluetoothMacAddress) { boolean isValid = false; if (!isBluetoothMacAddressUnknown(bluetoothMacAddress) && bluetoothMacAddress.length() >= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MIN && bluetoothMacAddress.length() <= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MAX) { String[] bytesAsHexStringArray = bluetoothMacAddress.split(BLUETOOTH_ADDRESS_SEPARATOR); if (bytesAsHexStringArray.length == BLUETOOTH_ADDRESS_BYTE_COUNT) { boolean allBytesAreValid = true; for (String byteAsHexString : bytesAsHexStringArray) { if (byteAsHexString.length() == 0 || byteAsHexString.length() > 2 || !byteAsHexString.matches(UPPER_CASE_HEX_REGEXP_CONDITION)) { allBytesAreValid = false; break; } } isValid = allBytesAreValid; } } if (!isValid && bluetoothMacAddress != null) { Log.e(TAG, "isValidBluetoothMacAddress: Not a valid Bluetooth MAC address: " + bluetoothMacAddress); } return isValid; }  static boolean isBluetoothMacAddressUnknown(String bluetoothMacAddress); static boolean isValidBluetoothMacAddress(String bluetoothMacAddress); static String getBluetoothMacAddressFromSocket(final BluetoothSocket bluetoothSocket); static PeerProperties validateReceivedHandshakeMessage(
            byte[] handshakeMessage, int handshakeMessageLength, BluetoothSocket bluetoothSocketOfSender); static int getPreviouslyUsedAlternativeChannelOrPort(); static void setNextAlternativeChannelOrPort(int channelOrPort); static BluetoothSocket createBluetoothSocketToServiceRecord(
            BluetoothDevice bluetoothDevice, UUID serviceRecordUuid, int channelOrPort, boolean secure); static BluetoothSocket createBluetoothSocketToServiceRecordWithNextPort(
            BluetoothDevice bluetoothDevice, UUID serviceRecordUuid, boolean secure); static BluetoothSocket createBluetoothSocket(
            BluetoothSocket originalBluetoothSocket, int channelOrPort, boolean secure); static BluetoothSocket createBluetoothSocketWithNextChannel(
            BluetoothSocket originalBluetoothSocket, boolean secure); }
BluetoothUtils { public static boolean isValidBluetoothMacAddress(String bluetoothMacAddress) { boolean isValid = false; if (!isBluetoothMacAddressUnknown(bluetoothMacAddress) && bluetoothMacAddress.length() >= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MIN && bluetoothMacAddress.length() <= BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MAX) { String[] bytesAsHexStringArray = bluetoothMacAddress.split(BLUETOOTH_ADDRESS_SEPARATOR); if (bytesAsHexStringArray.length == BLUETOOTH_ADDRESS_BYTE_COUNT) { boolean allBytesAreValid = true; for (String byteAsHexString : bytesAsHexStringArray) { if (byteAsHexString.length() == 0 || byteAsHexString.length() > 2 || !byteAsHexString.matches(UPPER_CASE_HEX_REGEXP_CONDITION)) { allBytesAreValid = false; break; } } isValid = allBytesAreValid; } } if (!isValid && bluetoothMacAddress != null) { Log.e(TAG, "isValidBluetoothMacAddress: Not a valid Bluetooth MAC address: " + bluetoothMacAddress); } return isValid; }  static boolean isBluetoothMacAddressUnknown(String bluetoothMacAddress); static boolean isValidBluetoothMacAddress(String bluetoothMacAddress); static String getBluetoothMacAddressFromSocket(final BluetoothSocket bluetoothSocket); static PeerProperties validateReceivedHandshakeMessage(
            byte[] handshakeMessage, int handshakeMessageLength, BluetoothSocket bluetoothSocketOfSender); static int getPreviouslyUsedAlternativeChannelOrPort(); static void setNextAlternativeChannelOrPort(int channelOrPort); static BluetoothSocket createBluetoothSocketToServiceRecord(
            BluetoothDevice bluetoothDevice, UUID serviceRecordUuid, int channelOrPort, boolean secure); static BluetoothSocket createBluetoothSocketToServiceRecordWithNextPort(
            BluetoothDevice bluetoothDevice, UUID serviceRecordUuid, boolean secure); static BluetoothSocket createBluetoothSocket(
            BluetoothSocket originalBluetoothSocket, int channelOrPort, boolean secure); static BluetoothSocket createBluetoothSocketWithNextChannel(
            BluetoothSocket originalBluetoothSocket, boolean secure); static final String BLUETOOTH_ADDRESS_SEPARATOR; static final int BLUETOOTH_ADDRESS_BYTE_COUNT; static final int BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MIN; static final int BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MAX; static final String SIMPLE_HANDSHAKE_MESSAGE_AS_STRING; static final byte[] SIMPLE_HANDSHAKE_MESSAGE_AS_BYTE_ARRAY; }