• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2025 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.bluetooth.le_scan
18 
19 import android.app.PendingIntent
20 import android.bluetooth.BluetoothDevice
21 import android.bluetooth.IBluetoothScan
22 import android.bluetooth.le.IPeriodicAdvertisingCallback
23 import android.bluetooth.le.IScannerCallback
24 import android.bluetooth.le.ScanFilter
25 import android.bluetooth.le.ScanResult
26 import android.bluetooth.le.ScanSettings
27 import android.content.AttributionSource
28 import android.os.WorkSource
29 import android.util.Log
30 
31 class ScanBinder(private val mScanController: ScanController) : IBluetoothScan.Stub() {
32 
33     companion object {
34         private val TAG = ScanBinder::class.java.simpleName
35     }
36 
37     @Volatile private var mIsAvailable = true
38 
39     fun cleanup() {
40         mIsAvailable = false
41     }
42 
43     private fun getScanController(): ScanController? {
44         if (mIsAvailable) {
45             return mScanController
46         } else {
47             Log.e(TAG, "getScanController() - ScanController requested, but not available!")
48             return null
49         }
50     }
51 
52     override fun registerScanner(
53         callback: IScannerCallback,
54         workSource: WorkSource?,
55         source: AttributionSource,
56     ) {
57         getScanController()?.let { scanController ->
58             scanController.registerScanner(callback, workSource, source)
59         }
60     }
61 
62     override fun unregisterScanner(scannerId: Int, source: AttributionSource) {
63         getScanController()?.let { scanController ->
64             scanController.unregisterScanner(scannerId, source)
65         }
66     }
67 
68     override fun startScan(
69         scannerId: Int,
70         settings: ScanSettings?,
71         filters: List<ScanFilter>?,
72         source: AttributionSource,
73     ) {
74         getScanController()?.let { scanController ->
75             scanController.startScan(scannerId, settings, filters, source)
76         }
77     }
78 
79     override fun startScanForIntent(
80         intent: PendingIntent,
81         settings: ScanSettings?,
82         filters: List<ScanFilter>?,
83         source: AttributionSource,
84     ) {
85         getScanController()?.let { scanController ->
86             scanController.registerPiAndStartScan(intent, settings, filters, source)
87         }
88     }
89 
90     override fun stopScan(scannerId: Int, source: AttributionSource) {
91         getScanController()?.let { scanController -> scanController.stopScan(scannerId, source) }
92     }
93 
94     override fun stopScanForIntent(intent: PendingIntent, source: AttributionSource) {
95         getScanController()?.let { scanController -> scanController.stopScan(intent, source) }
96     }
97 
98     override fun flushPendingBatchResults(scannerId: Int, source: AttributionSource) {
99         getScanController()?.let { scanController ->
100             scanController.flushPendingBatchResults(scannerId, source)
101         }
102     }
103 
104     override fun registerSync(
105         scanResult: ScanResult,
106         skip: Int,
107         timeout: Int,
108         callback: IPeriodicAdvertisingCallback,
109         source: AttributionSource,
110     ) {
111         getScanController()?.let { scanController ->
112             scanController.registerSync(scanResult, skip, timeout, callback, source)
113         }
114     }
115 
116     override fun unregisterSync(callback: IPeriodicAdvertisingCallback, source: AttributionSource) {
117         getScanController()?.let { scanController ->
118             scanController.unregisterSync(callback, source)
119         }
120     }
121 
122     override fun transferSync(
123         device: BluetoothDevice,
124         serviceData: Int,
125         syncHandle: Int,
126         source: AttributionSource,
127     ) {
128         getScanController()?.let { scanController ->
129             scanController.transferSync(device, serviceData, syncHandle, source)
130         }
131     }
132 
133     override fun transferSetInfo(
134         device: BluetoothDevice,
135         serviceData: Int,
136         advHandle: Int,
137         callback: IPeriodicAdvertisingCallback,
138         source: AttributionSource,
139     ) {
140         getScanController()?.let { scanController ->
141             scanController.transferSetInfo(device, serviceData, advHandle, callback, source)
142         }
143     }
144 
145     override fun numHwTrackFiltersAvailable(source: AttributionSource): Int {
146         return getScanController()?.let { scanController ->
147             scanController.numHwTrackFiltersAvailable(source)
148         } ?: 0
149     }
150 }
151