Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2014 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 package com.android.tv.settings.util.bluetooth;
     18 
     19 import android.bluetooth.BluetoothClass;
     20 import android.bluetooth.BluetoothDevice;
     21 
     22 import java.util.regex.Pattern;
     23 
     24 /**
     25  * Class that decides whether a BluetoothDevice matches the parameters of the
     26  * type of device that is being looked for.
     27  *
     28  * For example, does the device MAC address match the expected pattern and
     29  * does the device provide the types of services (audio, video, input, etc) that
     30  * are needed.
     31  */
     32 public class BluetoothDeviceCriteria {
     33 
     34     // TODO add ability to determine matching device based on name
     35 
     36     public static final String GOOGLE_MAC_PATTERN = "^(00:1A:11|F8:8F:CA).*";
     37 
     38     private final Pattern mAddressPattern;
     39 
     40     public BluetoothDeviceCriteria() {
     41         this(".*");
     42     }
     43 
     44     public BluetoothDeviceCriteria(String macAddressPattern) {
     45         mAddressPattern = Pattern.compile(macAddressPattern, Pattern.CASE_INSENSITIVE);
     46     }
     47 
     48     public final boolean isMatchingDevice(BluetoothDevice device) {
     49         if (device == null) {
     50             return false;
     51         }
     52 
     53         if (device.getAddress() == null || !isMatchingMacAddress(device.getAddress())) {
     54             return false;
     55         }
     56 
     57         if (!isMatchingMajorDeviceClass(device.getBluetoothClass().getMajorDeviceClass())) {
     58             return false;
     59         }
     60 
     61         if (!isMatchingDeviceClass(device.getBluetoothClass().getDeviceClass())) {
     62             return false;
     63         }
     64 
     65         return true;
     66     }
     67 
     68     public boolean isMatchingMacAddress(String mac) {
     69         return mAddressPattern.matcher(mac).matches();
     70     }
     71 
     72     /**
     73      * Override this method to restrict the major device classes that match.
     74      * @param majorDeviceClass constant from {@link BluetoothClass.Device.Major}.
     75      */
     76     public boolean isMatchingMajorDeviceClass(int majorDeviceClass) {
     77         return true;
     78     }
     79 
     80     /**
     81      * Override this method to restrict specific device classes that match.
     82      * @param majorMinorClass constant from {@link BluetoothClass.Device}
     83      */
     84     public boolean isMatchingDeviceClass(int majorMinorClass) {
     85         return true;
     86     }
     87 }
     88