• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.bluetooth.BluetoothDevice
20 import android.bluetooth.BluetoothLeBroadcastAssistant
21 import android.bluetooth.BluetoothLeBroadcastMetadata
22 import android.bluetooth.BluetoothLeBroadcastReceiveState
23 import com.android.internal.util.ConcurrentUtils
24 import kotlinx.coroutines.channels.awaitClose
25 import kotlinx.coroutines.flow.Flow
26 import kotlinx.coroutines.flow.callbackFlow
27 import kotlinx.coroutines.launch
28 
29 /** [Flow] for [BluetoothLeBroadcastAssistant.Callback] source connected/removed events */
30 val LocalBluetoothLeBroadcastAssistant.onSourceConnectedOrRemoved: Flow<Unit>
<lambda>null31     get() = callbackFlow {
32         val callback =
33             object : BluetoothLeBroadcastAssistant.Callback {
34                 override fun onReceiveStateChanged(
35                     sink: BluetoothDevice,
36                     sourceId: Int,
37                     state: BluetoothLeBroadcastReceiveState
38                 ) {}
39 
40                 override fun onSourceRemoved(sink: BluetoothDevice, sourceId: Int, reason: Int) {
41                     launch { send(Unit) }
42                 }
43 
44                 override fun onSearchStarted(reason: Int) {}
45 
46                 override fun onSearchStartFailed(reason: Int) {}
47 
48                 override fun onSearchStopped(reason: Int) {}
49 
50                 override fun onSearchStopFailed(reason: Int) {}
51 
52                 override fun onSourceFound(source: BluetoothLeBroadcastMetadata) {}
53 
54                 override fun onSourceAdded(sink: BluetoothDevice, sourceId: Int, reason: Int) {
55                     launch { send(Unit) }
56                 }
57 
58                 override fun onSourceAddFailed(
59                     sink: BluetoothDevice,
60                     source: BluetoothLeBroadcastMetadata,
61                     reason: Int
62                 ) {}
63 
64                 override fun onSourceModified(sink: BluetoothDevice, sourceId: Int, reason: Int) {}
65 
66                 override fun onSourceModifyFailed(
67                     sink: BluetoothDevice,
68                     sourceId: Int,
69                     reason: Int
70                 ) {}
71 
72                 override fun onSourceRemoveFailed(
73                     sink: BluetoothDevice,
74                     sourceId: Int,
75                     reason: Int
76                 ) {}
77             }
78         registerServiceCallBack(
79             ConcurrentUtils.DIRECT_EXECUTOR,
80             callback,
81         )
82         awaitClose { unregisterServiceCallBack(callback) }
83     }
84