• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 /* Required to handle timeout notification when phone is suspended */
20 import android.app.AlarmManager;
21 import android.app.PendingIntent;
22 
23 import android.bluetooth.BluetoothAdapter;
24 import android.content.BroadcastReceiver;
25 import android.content.ContentResolver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.util.Log;
29 
30 
31 public class BluetoothDiscoverableTimeoutReceiver extends BroadcastReceiver {
32     private static final String TAG = "BluetoothDiscoverableTimeoutReceiver";
33 
34     private static final String INTENT_DISCOVERABLE_TIMEOUT =
35         "android.bluetooth.intent.DISCOVERABLE_TIMEOUT";
36 
setDiscoverableAlarm(Context context, long alarmTime)37     static void setDiscoverableAlarm(Context context, long alarmTime) {
38         Log.d(TAG, "setDiscoverableAlarm(): alarmTime = " + alarmTime);
39 
40         Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
41         intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
42         PendingIntent pending = PendingIntent.getBroadcast(
43             context, 0, intent, 0);
44         AlarmManager alarmManager =
45               (AlarmManager) context.getSystemService (Context.ALARM_SERVICE);
46 
47         if (pending != null) {
48             // Cancel any previous alarms that do the same thing.
49             alarmManager.cancel(pending);
50             Log.d(TAG, "setDiscoverableAlarm(): cancel prev alarm");
51         }
52         pending = PendingIntent.getBroadcast(
53             context, 0, intent, 0);
54 
55         alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pending);
56     }
57 
cancelDiscoverableAlarm(Context context)58     static void cancelDiscoverableAlarm(Context context) {
59         Log.d(TAG, "cancelDiscoverableAlarm(): Enter");
60 
61         Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
62         intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
63         PendingIntent pending = PendingIntent.getBroadcast(
64             context, 0, intent, PendingIntent.FLAG_NO_CREATE);
65         if (pending != null) {
66             // Cancel any previous alarms that do the same thing.
67             AlarmManager alarmManager =
68               (AlarmManager) context.getSystemService (Context.ALARM_SERVICE);
69 
70             alarmManager.cancel(pending);
71         }
72     }
73 
74     @Override
onReceive(Context context, Intent intent)75     public void onReceive(Context context, Intent intent) {
76         LocalBluetoothAdapter localBluetoothAdapter = LocalBluetoothAdapter.getInstance();
77 
78          if(localBluetoothAdapter != null  &&
79             localBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
80             Log.d(TAG, "Disable discoverable...");
81 
82             localBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
83          } else {
84             Log.e(TAG, "localBluetoothAdapter is NULL!!");
85         }
86     }
87 };
88