Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | package android.companion; |
| 19 | |
| 20 | import static android.text.TextUtils.firstNotEmpty; |
| 21 | |
| 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
Mathew Inwood | 70e89d5 | 2018-08-09 15:27:52 +0100 | [diff] [blame] | 24 | import android.annotation.UnsupportedAppUsage; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 25 | import android.bluetooth.BluetoothDevice; |
| 26 | import android.bluetooth.le.ScanFilter; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 27 | import android.net.wifi.ScanResult; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 28 | import android.os.ParcelUuid; |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 29 | import android.os.Parcelable; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | |
| 32 | import java.util.Arrays; |
| 33 | import java.util.List; |
| 34 | import java.util.regex.Pattern; |
| 35 | |
| 36 | /** @hide */ |
| 37 | public class BluetoothDeviceFilterUtils { |
| 38 | private BluetoothDeviceFilterUtils() {} |
| 39 | |
| 40 | private static final boolean DEBUG = false; |
Eugene Susla | 3c9aa17 | 2017-03-28 15:04:12 -0700 | [diff] [blame] | 41 | private static final String LOG_TAG = "BluetoothDeviceFilterUtils"; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 42 | |
| 43 | @Nullable |
| 44 | static String patternToString(@Nullable Pattern p) { |
| 45 | return p == null ? null : p.pattern(); |
| 46 | } |
| 47 | |
| 48 | @Nullable |
| 49 | static Pattern patternFromString(@Nullable String s) { |
| 50 | return s == null ? null : Pattern.compile(s); |
| 51 | } |
| 52 | |
| 53 | static boolean matches(ScanFilter filter, BluetoothDevice device) { |
Eugene Susla | 3c9aa17 | 2017-03-28 15:04:12 -0700 | [diff] [blame] | 54 | boolean result = matchesAddress(filter.getDeviceAddress(), device) |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 55 | && matchesServiceUuid(filter.getServiceUuid(), filter.getServiceUuidMask(), device); |
Eugene Susla | 3c9aa17 | 2017-03-28 15:04:12 -0700 | [diff] [blame] | 56 | if (DEBUG) debugLogMatchResult(result, device, filter); |
| 57 | return result; |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static boolean matchesAddress(String deviceAddress, BluetoothDevice device) { |
| 61 | final boolean result = deviceAddress == null |
Eugene Susla | 0c4a926 | 2017-06-09 18:03:14 -0700 | [diff] [blame] | 62 | || (device != null && deviceAddress.equals(device.getAddress())); |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 63 | if (DEBUG) debugLogMatchResult(result, device, deviceAddress); |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | static boolean matchesServiceUuids(List<ParcelUuid> serviceUuids, |
| 68 | List<ParcelUuid> serviceUuidMasks, BluetoothDevice device) { |
| 69 | for (int i = 0; i < serviceUuids.size(); i++) { |
| 70 | ParcelUuid uuid = serviceUuids.get(i); |
| 71 | ParcelUuid uuidMask = serviceUuidMasks.get(i); |
| 72 | if (!matchesServiceUuid(uuid, uuidMask, device)) { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | static boolean matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask, |
| 80 | BluetoothDevice device) { |
| 81 | final boolean result = serviceUuid == null || |
| 82 | ScanFilter.matchesServiceUuids( |
| 83 | serviceUuid, |
| 84 | serviceUuidMask, |
| 85 | Arrays.asList(device.getUuids())); |
| 86 | if (DEBUG) debugLogMatchResult(result, device, serviceUuid); |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | static boolean matchesName(@Nullable Pattern namePattern, BluetoothDevice device) { |
| 91 | boolean result; |
| 92 | if (namePattern == null) { |
| 93 | result = true; |
| 94 | } else if (device == null) { |
| 95 | result = false; |
| 96 | } else { |
| 97 | final String name = device.getName(); |
| 98 | result = name != null && namePattern.matcher(name).find(); |
| 99 | } |
| 100 | if (DEBUG) debugLogMatchResult(result, device, namePattern); |
| 101 | return result; |
| 102 | } |
| 103 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 104 | static boolean matchesName(@Nullable Pattern namePattern, ScanResult device) { |
| 105 | boolean result; |
| 106 | if (namePattern == null) { |
| 107 | result = true; |
| 108 | } else if (device == null) { |
| 109 | result = false; |
| 110 | } else { |
| 111 | final String name = device.SSID; |
| 112 | result = name != null && namePattern.matcher(name).find(); |
| 113 | } |
| 114 | if (DEBUG) debugLogMatchResult(result, device, namePattern); |
| 115 | return result; |
Eugene Susla | e70e6aa | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 118 | private static void debugLogMatchResult( |
| 119 | boolean result, BluetoothDevice device, Object criteria) { |
| 120 | Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria); |
| 121 | } |
| 122 | |
| 123 | private static void debugLogMatchResult( |
| 124 | boolean result, ScanResult device, Object criteria) { |
| 125 | Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria); |
| 126 | } |
| 127 | |
Mathew Inwood | 70e89d5 | 2018-08-09 15:27:52 +0100 | [diff] [blame] | 128 | @UnsupportedAppUsage |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 129 | public static String getDeviceDisplayNameInternal(@NonNull BluetoothDevice device) { |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 130 | return firstNotEmpty(device.getAliasName(), device.getAddress()); |
| 131 | } |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 132 | |
Mathew Inwood | 70e89d5 | 2018-08-09 15:27:52 +0100 | [diff] [blame] | 133 | @UnsupportedAppUsage |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 134 | public static String getDeviceDisplayNameInternal(@NonNull ScanResult device) { |
| 135 | return firstNotEmpty(device.SSID, device.BSSID); |
| 136 | } |
| 137 | |
Mathew Inwood | 70e89d5 | 2018-08-09 15:27:52 +0100 | [diff] [blame] | 138 | @UnsupportedAppUsage |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 139 | public static String getDeviceMacAddress(@NonNull Parcelable device) { |
| 140 | if (device instanceof BluetoothDevice) { |
| 141 | return ((BluetoothDevice) device).getAddress(); |
| 142 | } else if (device instanceof ScanResult) { |
| 143 | return ((ScanResult) device).BSSID; |
| 144 | } else if (device instanceof android.bluetooth.le.ScanResult) { |
| 145 | return getDeviceMacAddress(((android.bluetooth.le.ScanResult) device).getDevice()); |
| 146 | } else { |
| 147 | throw new IllegalArgumentException("Unknown device type: " + device); |
| 148 | } |
| 149 | } |
Eugene Susla | 6ed45d8 | 2017-01-22 13:52:51 -0800 | [diff] [blame] | 150 | } |