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; 24 import android.bluetooth.BluetoothDevice; 25 import android.bluetooth.le.ScanFilter; 26 import android.compat.annotation.UnsupportedAppUsage; 27 import android.net.wifi.ScanResult; 28 import android.os.ParcelUuid; 29 import android.os.Parcelable; 30 import android.util.Log; 31 32 import java.util.Arrays; 33 import java.util.Collections; 34 import java.util.List; 35 import java.util.regex.Pattern; 36 37 /** @hide */ 38 public class BluetoothDeviceFilterUtils { BluetoothDeviceFilterUtils()39 private BluetoothDeviceFilterUtils() {} 40 41 private static final boolean DEBUG = false; 42 private static final String LOG_TAG = "BluetoothDeviceFilterUtils"; 43 44 @Nullable patternToString(@ullable Pattern p)45 static String patternToString(@Nullable Pattern p) { 46 return p == null ? null : p.pattern(); 47 } 48 49 @Nullable patternFromString(@ullable String s)50 static Pattern patternFromString(@Nullable String s) { 51 return s == null ? null : Pattern.compile(s); 52 } 53 matches(ScanFilter filter, BluetoothDevice device)54 static boolean matches(ScanFilter filter, BluetoothDevice device) { 55 boolean result = matchesAddress(filter.getDeviceAddress(), device) 56 && matchesServiceUuid(filter.getServiceUuid(), filter.getServiceUuidMask(), device); 57 if (DEBUG) debugLogMatchResult(result, device, filter); 58 return result; 59 } 60 matchesAddress(String deviceAddress, BluetoothDevice device)61 static boolean matchesAddress(String deviceAddress, BluetoothDevice device) { 62 final boolean result = deviceAddress == null 63 || (device != null && deviceAddress.equals(device.getAddress())); 64 if (DEBUG) debugLogMatchResult(result, device, deviceAddress); 65 return result; 66 } 67 matchesServiceUuids(List<ParcelUuid> serviceUuids, List<ParcelUuid> serviceUuidMasks, BluetoothDevice device)68 static boolean matchesServiceUuids(List<ParcelUuid> serviceUuids, 69 List<ParcelUuid> serviceUuidMasks, BluetoothDevice device) { 70 for (int i = 0; i < serviceUuids.size(); i++) { 71 ParcelUuid uuid = serviceUuids.get(i); 72 ParcelUuid uuidMask = serviceUuidMasks.get(i); 73 if (!matchesServiceUuid(uuid, uuidMask, device)) { 74 return false; 75 } 76 } 77 return true; 78 } 79 matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask, BluetoothDevice device)80 static boolean matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask, 81 BluetoothDevice device) { 82 ParcelUuid[] uuids = device.getUuids(); 83 final boolean result = serviceUuid == null || 84 ScanFilter.matchesServiceUuids( 85 serviceUuid, 86 serviceUuidMask, 87 uuids == null ? Collections.emptyList() : Arrays.asList(uuids)); 88 if (DEBUG) debugLogMatchResult(result, device, serviceUuid); 89 return result; 90 } 91 matchesName(@ullable Pattern namePattern, BluetoothDevice device)92 static boolean matchesName(@Nullable Pattern namePattern, BluetoothDevice device) { 93 boolean result; 94 if (namePattern == null) { 95 result = true; 96 } else if (device == null) { 97 result = false; 98 } else { 99 final String name = device.getName(); 100 result = name != null && namePattern.matcher(name).find(); 101 } 102 if (DEBUG) debugLogMatchResult(result, device, namePattern); 103 return result; 104 } 105 matchesName(@ullable Pattern namePattern, ScanResult device)106 static boolean matchesName(@Nullable Pattern namePattern, ScanResult device) { 107 boolean result; 108 if (namePattern == null) { 109 result = true; 110 } else if (device == null) { 111 result = false; 112 } else { 113 final String name = device.SSID; 114 result = name != null && namePattern.matcher(name).find(); 115 } 116 if (DEBUG) debugLogMatchResult(result, device, namePattern); 117 return result; 118 } 119 debugLogMatchResult( boolean result, BluetoothDevice device, Object criteria)120 private static void debugLogMatchResult( 121 boolean result, BluetoothDevice device, Object criteria) { 122 Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria); 123 } 124 debugLogMatchResult( boolean result, ScanResult device, Object criteria)125 private static void debugLogMatchResult( 126 boolean result, ScanResult device, Object criteria) { 127 Log.i(LOG_TAG, getDeviceDisplayNameInternal(device) + (result ? " ~ " : " !~ ") + criteria); 128 } 129 130 @UnsupportedAppUsage getDeviceDisplayNameInternal(@onNull BluetoothDevice device)131 public static String getDeviceDisplayNameInternal(@NonNull BluetoothDevice device) { 132 return firstNotEmpty(device.getAlias(), device.getAddress()); 133 } 134 135 @UnsupportedAppUsage getDeviceDisplayNameInternal(@onNull ScanResult device)136 public static String getDeviceDisplayNameInternal(@NonNull ScanResult device) { 137 return firstNotEmpty(device.SSID, device.BSSID); 138 } 139 140 @UnsupportedAppUsage getDeviceMacAddress(@onNull Parcelable device)141 public static String getDeviceMacAddress(@NonNull Parcelable device) { 142 if (device instanceof BluetoothDevice) { 143 return ((BluetoothDevice) device).getAddress(); 144 } else if (device instanceof ScanResult) { 145 return ((ScanResult) device).BSSID; 146 } else if (device instanceof android.bluetooth.le.ScanResult) { 147 return getDeviceMacAddress(((android.bluetooth.le.ScanResult) device).getDevice()); 148 } else { 149 throw new IllegalArgumentException("Unknown device type: " + device); 150 } 151 } 152 } 153