1 /* 2 * Copyright (C) 2022 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 android.nearby.multidevices.fastpair.provider.controller 18 19 import android.bluetooth.le.AdvertiseSettings 20 import android.content.Context 21 import android.nearby.fastpair.provider.FastPairSimulator 22 import android.nearby.fastpair.provider.bluetooth.BluetoothController 23 import com.google.android.mobly.snippet.util.Log 24 import com.google.common.io.BaseEncoding.base64 25 26 class FastPairProviderSimulatorController(private val context: Context) : 27 FastPairSimulator.AdvertisingChangedCallback, BluetoothController.EventListener { 28 private lateinit var bluetoothController: BluetoothController 29 private lateinit var eventListener: EventListener 30 private var simulator: FastPairSimulator? = null 31 setupProviderSimulatornull32 fun setupProviderSimulator(listener: EventListener) { 33 eventListener = listener 34 35 bluetoothController = BluetoothController(context, this) 36 bluetoothController.registerBluetoothStateReceiver() 37 bluetoothController.enableBluetooth() 38 bluetoothController.connectA2DPSinkProfile() 39 } 40 teardownProviderSimulatornull41 fun teardownProviderSimulator() { 42 simulator?.destroy() 43 bluetoothController.unregisterBluetoothStateReceiver() 44 } 45 startModelIdAdvertisingnull46 fun startModelIdAdvertising( 47 modelId: String, 48 antiSpoofingKeyString: String, 49 listener: EventListener 50 ) { 51 eventListener = listener 52 53 val antiSpoofingKey = base64().decode(antiSpoofingKeyString) 54 simulator = FastPairSimulator( 55 context, FastPairSimulator.Options.builder(modelId) 56 .setAdvertisingModelId(modelId) 57 .setBluetoothAddress(null) 58 .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) 59 .setAdvertisingChangedCallback(this) 60 .setAntiSpoofingPrivateKey(antiSpoofingKey) 61 .setUseRandomSaltForAccountKeyRotation(false) 62 .setDataOnlyConnection(false) 63 .setShowsPasskeyConfirmation(false) 64 .setRemoveAllDevicesDuringPairing(true) 65 .build() 66 ) 67 } 68 getProviderSimulatorBleAddressnull69 fun getProviderSimulatorBleAddress() = simulator!!.bleAddress!! 70 71 fun getLatestReceivedAccountKey() = 72 simulator!!.accountKey?.let { base64().encode(it.toByteArray()) } 73 74 /** 75 * Called when we change our BLE advertisement. 76 * 77 * @param isAdvertising the advertising status. 78 */ onAdvertisingChangednull79 override fun onAdvertisingChanged(isAdvertising: Boolean) { 80 Log.i("FastPairSimulator onAdvertisingChanged(isAdvertising: $isAdvertising)") 81 eventListener.onAdvertisingChange(isAdvertising) 82 } 83 84 /** The callback for the first onServiceConnected of A2DP sink profile. */ onA2DPSinkProfileConnectednull85 override fun onA2DPSinkProfileConnected() { 86 eventListener.onA2DPSinkProfileConnected() 87 } 88 89 /** 90 * Reports the current bond state of the remote device. 91 * 92 * @param bondState the bond state of the remote device. 93 */ onBondStateChangednull94 override fun onBondStateChanged(bondState: Int) { 95 } 96 97 /** 98 * Reports the current connection state of the remote device. 99 * 100 * @param connectionState the bond state of the remote device. 101 */ onConnectionStateChangednull102 override fun onConnectionStateChanged(connectionState: Int) { 103 } 104 105 /** 106 * Reports the current scan mode of the local Adapter. 107 * 108 * @param mode the current scan mode of the local Adapter. 109 */ onScanModeChangenull110 override fun onScanModeChange(mode: Int) { 111 eventListener.onScanModeChange(FastPairSimulator.scanModeToString(mode)) 112 } 113 114 /** Interface for listening the events from Fast Pair Provider Simulator. */ 115 interface EventListener { 116 /** Reports the first onServiceConnected of A2DP sink profile. */ onA2DPSinkProfileConnectednull117 fun onA2DPSinkProfileConnected() 118 119 /** 120 * Reports the current scan mode of the local Adapter. 121 * 122 * @param mode the current scan mode in string. 123 */ 124 fun onScanModeChange(mode: String) 125 126 /** 127 * Indicates the advertising state of the Fast Pair provider simulator has changed. 128 * 129 * @param isAdvertising the current advertising state, true if advertising otherwise false. 130 */ 131 fun onAdvertisingChange(isAdvertising: Boolean) 132 } 133 }