Home | History | Annotate | Download | only in bluetooth
      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 package com.android.settings.bluetooth;
     18 
     19 import android.bluetooth.BluetoothAdapter;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.os.Handler;
     25 import android.support.annotation.VisibleForTesting;
     26 import android.util.Log;
     27 
     28 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
     29 
     30 import java.util.Timer;
     31 import java.util.TimerTask;
     32 
     33 /** Helper class, intended to be used by an Activity, to keep the local Bluetooth adapter in
     34  *  discoverable mode indefinitely. By default setting the scan mode to
     35  *  BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE will time out after some time, but some
     36  *  Bluetooth settings pages would like to keep the device discoverable as long as the page is
     37  *  visible. */
     38 public class AlwaysDiscoverable extends BroadcastReceiver {
     39     private static final String TAG = "AlwaysDiscoverable";
     40 
     41     private Context mContext;
     42     private LocalBluetoothAdapter mLocalAdapter;
     43     private IntentFilter mIntentFilter;
     44 
     45     @VisibleForTesting
     46     boolean mStarted;
     47 
     48     public AlwaysDiscoverable(Context context, LocalBluetoothAdapter localAdapter) {
     49         mContext = context;
     50         mLocalAdapter = localAdapter;
     51         mIntentFilter = new IntentFilter();
     52         mIntentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
     53     }
     54 
     55     /** After calling start(), consumers should make a matching call to stop() when they no longer
     56      * wish to enforce discoverable mode. */
     57     public void start() {
     58         if (mStarted) {
     59             return;
     60         }
     61         mContext.registerReceiver(this, mIntentFilter);
     62         mStarted = true;
     63         if (mLocalAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
     64             mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
     65         }
     66     }
     67 
     68     public void stop() {
     69         if (!mStarted) {
     70             return;
     71         }
     72         mContext.unregisterReceiver(this);
     73         mStarted = false;
     74         mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
     75     }
     76 
     77     @Override
     78     public void onReceive(Context context, Intent intent) {
     79         String action = intent.getAction();
     80         if (action != BluetoothAdapter.ACTION_SCAN_MODE_CHANGED) {
     81             return;
     82         }
     83         if (mLocalAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
     84             mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
     85         }
     86     }
     87 }
     88