1 /*
2  * Copyright (C) 2011 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.settingslib.bluetooth;
18 
19 import static android.bluetooth.BluetoothAdapter.STATE_CONNECTED;
20 import static android.bluetooth.BluetoothAdapter.STATE_CONNECTING;
21 import static android.bluetooth.BluetoothAdapter.STATE_DISCONNECTED;
22 import static android.bluetooth.BluetoothAdapter.STATE_DISCONNECTING;
23 import static android.bluetooth.BluetoothAdapter.STATE_OFF;
24 import static android.bluetooth.BluetoothAdapter.STATE_ON;
25 import static android.bluetooth.BluetoothAdapter.STATE_TURNING_OFF;
26 import static android.bluetooth.BluetoothAdapter.STATE_TURNING_ON;
27 
28 import android.annotation.IntDef;
29 
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 
36 /**
37  * BluetoothCallback provides a callback interface for the settings
38  * UI to receive events from {@link BluetoothEventManager}.
39  */
40 public interface BluetoothCallback {
41     /**
42      * It will be called when the state of the local Bluetooth adapter has been changed.
43      * It is listening {@link android.bluetooth.BluetoothAdapter#ACTION_STATE_CHANGED}.
44      * For example, Bluetooth has been turned on or off.
45      *
46      * @param bluetoothState the current Bluetooth state, the possible values are:
47      * {@link android.bluetooth.BluetoothAdapter#STATE_OFF},
48      * {@link android.bluetooth.BluetoothAdapter#STATE_TURNING_ON},
49      * {@link android.bluetooth.BluetoothAdapter#STATE_ON},
50      * {@link android.bluetooth.BluetoothAdapter#STATE_TURNING_OFF}.
51      */
onBluetoothStateChanged(@dapterState int bluetoothState)52     default void onBluetoothStateChanged(@AdapterState int bluetoothState) {}
53 
54     /**
55      * It will be called when the local Bluetooth adapter has started
56      * or finished the remote device discovery process.
57      * It is listening {@link android.bluetooth.BluetoothAdapter#ACTION_DISCOVERY_STARTED} and
58      * {@link android.bluetooth.BluetoothAdapter#ACTION_DISCOVERY_FINISHED}.
59      *
60      * @param started indicate the current process is started or finished.
61      */
onScanningStateChanged(boolean started)62     default void onScanningStateChanged(boolean started) {}
63 
64     /**
65      * It will be called in following situations:
66      * 1. In scanning mode, when a new device has been found.
67      * 2. When a profile service is connected and existing connected devices has been found.
68      * This API only invoked once for each device and all devices will be cached in
69      * {@link CachedBluetoothDeviceManager}.
70      *
71      * @param cachedDevice the Bluetooth device.
72      */
onDeviceAdded(@onNull CachedBluetoothDevice cachedDevice)73     default void onDeviceAdded(@NonNull CachedBluetoothDevice cachedDevice) {}
74 
75     /**
76      * It will be called when requiring to remove a remote device from CachedBluetoothDevice list
77      *
78      * @param cachedDevice the Bluetooth device.
79      */
onDeviceDeleted(@onNull CachedBluetoothDevice cachedDevice)80     default void onDeviceDeleted(@NonNull CachedBluetoothDevice cachedDevice) {}
81 
82     /**
83      * It will be called when bond state of a remote device is changed.
84      * It is listening {@link android.bluetooth.BluetoothDevice#ACTION_BOND_STATE_CHANGED}
85      *
86      * @param cachedDevice the Bluetooth device.
87      * @param bondState the Bluetooth device bond state, the possible values are:
88      * {@link android.bluetooth.BluetoothDevice#BOND_NONE},
89      * {@link android.bluetooth.BluetoothDevice#BOND_BONDING},
90      * {@link android.bluetooth.BluetoothDevice#BOND_BONDED}.
91      */
onDeviceBondStateChanged( @onNull CachedBluetoothDevice cachedDevice, int bondState)92     default void onDeviceBondStateChanged(
93             @NonNull CachedBluetoothDevice cachedDevice, int bondState) {}
94 
95     /**
96      * It will be called in following situations:
97      * 1. When the adapter is not connected to any profiles of any remote devices
98      * and it attempts a connection to a profile.
99      * 2. When the adapter disconnects from the last profile of the last device.
100      * It is listening {@link android.bluetooth.BluetoothAdapter#ACTION_CONNECTION_STATE_CHANGED}
101      *
102      * @param cachedDevice the Bluetooth device.
103      * @param state the Bluetooth device connection state, the possible values are:
104      * {@link android.bluetooth.BluetoothAdapter#STATE_DISCONNECTED},
105      * {@link android.bluetooth.BluetoothAdapter#STATE_CONNECTING},
106      * {@link android.bluetooth.BluetoothAdapter#STATE_CONNECTED},
107      * {@link android.bluetooth.BluetoothAdapter#STATE_DISCONNECTING}.
108      */
onConnectionStateChanged( @ullable CachedBluetoothDevice cachedDevice, @ConnectionState int state)109     default void onConnectionStateChanged(
110             @Nullable CachedBluetoothDevice cachedDevice,
111             @ConnectionState int state) {}
112 
113     /**
114      * It will be called when device been set as active for {@code bluetoothProfile}
115      * It is listening in following intent:
116      * {@link android.bluetooth.BluetoothA2dp#ACTION_ACTIVE_DEVICE_CHANGED}
117      * {@link android.bluetooth.BluetoothHeadset#ACTION_ACTIVE_DEVICE_CHANGED}
118      * {@link android.bluetooth.BluetoothHearingAid#ACTION_ACTIVE_DEVICE_CHANGED}
119      *
120      * @param activeDevice the active Bluetooth device.
121      * @param bluetoothProfile the profile of active Bluetooth device.
122      */
onActiveDeviceChanged( @ullable CachedBluetoothDevice activeDevice, int bluetoothProfile)123     default void onActiveDeviceChanged(
124             @Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {}
125 
126     /**
127      * It will be called in following situations:
128      * 1. When the call state on the device is changed.
129      * 2. When the audio connection state of the A2DP profile is changed.
130      * It is listening in following intent:
131      * {@link android.bluetooth.BluetoothHeadset#ACTION_AUDIO_STATE_CHANGED}
132      * {@link android.telephony.TelephonyManager#ACTION_PHONE_STATE_CHANGED}
133      */
onAudioModeChanged()134     default void onAudioModeChanged() {}
135 
136     /**
137      * It will be called when one of the bluetooth device profile connection state is changed.
138      *
139      * @param cachedDevice the active Bluetooth device.
140      * @param state the BluetoothProfile connection state, the possible values are:
141      * {@link android.bluetooth.BluetoothProfile#STATE_CONNECTED},
142      * {@link android.bluetooth.BluetoothProfile#STATE_CONNECTING},
143      * {@link android.bluetooth.BluetoothProfile#STATE_DISCONNECTED},
144      * {@link android.bluetooth.BluetoothProfile#STATE_DISCONNECTING}.
145      * @param bluetoothProfile the BluetoothProfile id.
146      */
onProfileConnectionStateChanged( @onNull CachedBluetoothDevice cachedDevice, @ConnectionState int state, int bluetoothProfile)147     default void onProfileConnectionStateChanged(
148             @NonNull CachedBluetoothDevice cachedDevice,
149             @ConnectionState int state,
150             int bluetoothProfile) {
151     }
152 
153     /**
154      * Called when ACL connection state is changed. It listens to
155      * {@link android.bluetooth.BluetoothDevice#ACTION_ACL_CONNECTED} and {@link
156      * android.bluetooth.BluetoothDevice#ACTION_ACL_DISCONNECTED}
157      *
158      * @param cachedDevice Bluetooth device that changed
159      * @param state        the Bluetooth device connection state, the possible values are:
160      *                     {@link android.bluetooth.BluetoothAdapter#STATE_DISCONNECTED},
161      *                     {@link android.bluetooth.BluetoothAdapter#STATE_CONNECTED}
162      */
onAclConnectionStateChanged( @onNull CachedBluetoothDevice cachedDevice, int state)163     default void onAclConnectionStateChanged(
164             @NonNull CachedBluetoothDevice cachedDevice, int state) {}
165 
166     /**
167      * Called when the Auto-on state is changed for any user. Listens to intent
168      * {@link android.bluetooth.BluetoothAdapter#ACTION_AUTO_ON_STATE_CHANGED }
169      *
170      * @param state        the Auto-on state, the possible values are:
171      *                     {@link android.bluetooth.BluetoothAdapter#AUTO_ON_STATE_ENABLED},
172      *                     {@link android.bluetooth.BluetoothAdapter#AUTO_ON_STATE_DISABLED}
173      */
onAutoOnStateChanged(int state)174     default void onAutoOnStateChanged(int state) {}
175 
176     @Retention(RetentionPolicy.SOURCE)
177     @IntDef(prefix = { "STATE_" }, value = {
178             STATE_DISCONNECTED,
179             STATE_CONNECTING,
180             STATE_CONNECTED,
181             STATE_DISCONNECTING,
182     })
183     @interface ConnectionState {}
184 
185     @IntDef(prefix = { "STATE_" }, value = {
186             STATE_OFF,
187             STATE_TURNING_ON,
188             STATE_ON,
189             STATE_TURNING_OFF,
190     })
191     @Retention(RetentionPolicy.SOURCE)
192     @interface AdapterState {}
193 }
194