Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0174efa2 authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

LeAudioTestApp: Fix passing BASS scan context

This cleans up broadcast sources scanning code in case when
there is no scan delegator device. It removes the confusing
code with fake device.

Scanning from the main activity gives no device context and it
does not support any Broadcast Scan Assistant specific actions
besides the scanning, which is the only action that requires no
remote device. Scanning on behalf of a scan delegator device is
suppose to be triggered from the LeAudioRecycleViewAdapter.
This is suppose to be the point where one interacts with remote
BASS device, adds, modify or removes broadcast sources.

Bug: 150670922
Tag: #feature
Test: build
Sponsor: jpawlowski@
Change-Id: I32244bc541f085ddd31ef1c85bd1f0b58b609f85
parent fe1986fc
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.os.ParcelUuid;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
@@ -55,7 +56,7 @@ public class BluetoothProxy {
    private BluetoothLeAudio bluetoothLeAudio = null;
    private BluetoothLeBroadcast mBluetoothLeBroadcast = null;
    private BluetoothLeBroadcastAssistant mBluetoothLeBroadcastAssistant = null;
    private Set<BluetoothDevice> mBroadcastScanOnBehalfDevices = new HashSet<>();
    private Set<BluetoothDevice> mBroadcastScanDelegatorDevices = new HashSet<>();
    private BluetoothCsipSetCoordinator bluetoothCsis = null;
    private BluetoothVolumeControl bluetoothVolumeControl = null;
    private BluetoothHapClient bluetoothHapClient = null;
@@ -1026,20 +1027,24 @@ public class BluetoothProxy {
        }
    }

    public boolean scanForBroadcasts(@NonNull BluetoothDevice onBehalfDevice, boolean scan) {
    public boolean scanForBroadcasts(@Nullable BluetoothDevice scanDelegator, boolean scan) {
        if (mBluetoothLeBroadcastAssistant != null) {
            // Note: startSearchingForSources() does not support scanning on behalf of
            // a specific device - it only searches for all BASS connected devices.
            // Therefore, we manage the list of the devices and start/stop the scanning.
            if (scan) {
                mBroadcastScanOnBehalfDevices.add(onBehalfDevice);
                if (scanDelegator != null) {
                    mBroadcastScanDelegatorDevices.add(scanDelegator);
                }
                mBluetoothLeBroadcastAssistant.startSearchingForSources(new ArrayList<>());
                if (mBassEventListener != null) {
                    mBassEventListener.onScanningStateChanged(true);
                }
            } else {
                mBroadcastScanOnBehalfDevices.remove(onBehalfDevice);
                if (mBroadcastScanOnBehalfDevices.isEmpty()) {
                if (scanDelegator != null) {
                    mBroadcastScanDelegatorDevices.remove(scanDelegator);
                }
                if (mBroadcastScanDelegatorDevices.isEmpty()) {
                    mBluetoothLeBroadcastAssistant.stopSearchingForSources();
                    if (mBassEventListener != null) {
                        mBassEventListener.onScanningStateChanged(false);
@@ -1053,7 +1058,7 @@ public class BluetoothProxy {

    public boolean stopBroadcastObserving() {
        if (mBluetoothLeBroadcastAssistant != null) {
            mBroadcastScanOnBehalfDevices.clear();
            mBroadcastScanDelegatorDevices.clear();
            mBluetoothLeBroadcastAssistant.stopSearchingForSources();
            if (mBassEventListener != null) {
                mBassEventListener.onScanningStateChanged(false);
+5 −11
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ public class BroadcastScanActivity extends AppCompatActivity {
    private BluetoothDevice device;
    private BroadcastScanViewModel mViewModel;
    private BroadcastItemsAdapter adapter;
    private String mLocalBluetoothAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
@@ -53,8 +52,6 @@ public class BroadcastScanActivity extends AppCompatActivity {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        mLocalBluetoothAddress = BluetoothAdapter.getDefaultAdapter().getAddress();

        adapter = new BroadcastItemsAdapter();
        adapter.setOnItemClickListener(broadcastId -> {
            mViewModel.scanForBroadcasts(device, false);
@@ -73,14 +70,11 @@ public class BroadcastScanActivity extends AppCompatActivity {
                return;
            }

            // TODO: Support selecting the subgroups instead of using all
            // Set broadcast source on peer only if scan delegator device context is available
            if (device != null) {
                Toast.makeText(recyclerView.getContext(), "Adding broadcast source"
                                + " broadcastId=" + broadcastId, Toast.LENGTH_SHORT).show();
                mViewModel.addBroadcastSource(device, broadcast);
            if (TextUtils.equals(mLocalBluetoothAddress, broadcast.getSourceDevice().getAddress())) {
                Toast.makeText(recyclerView.getContext(), "Add local broadcast",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(recyclerView.getContext(), "Add remote broadcast",
                        Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.setAdapter(adapter);
+6 −11
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ import java.util.List;
public class BroadcastScanViewModel extends AndroidViewModel {
    private final String TAG = "BroadcastScanViewModel";
    boolean mIsActivityScanning = false;
    BluetoothDevice mOnBehalfDevice;
    BluetoothDevice mScanDelegatorDevice;

    // TODO: Remove these variables if they are unnecessary
//    // AddBroadcast context
@@ -71,8 +71,8 @@ public class BroadcastScanViewModel extends AndroidViewModel {

                // Continue as long as the main activity wants
                if (mIsActivityScanning) {
                    if (mOnBehalfDevice != null) {
                        mBluetooth.scanForBroadcasts(mOnBehalfDevice, true);
                    if (mScanDelegatorDevice != null) {
                        mBluetooth.scanForBroadcasts(mScanDelegatorDevice, true);
                    }
                }
            } else {
@@ -128,14 +128,9 @@ public class BroadcastScanViewModel extends AndroidViewModel {
        return mAllBroadcasts;
    }

    public void scanForBroadcasts(BluetoothDevice device, boolean scan) {
        if (device == null) {
            Log.e(TAG, "scanForBroadcasts: device is null. Ignoring.");
            return;
        }

    public void scanForBroadcasts(BluetoothDevice delegatorDevice, boolean scan) {
        mIsActivityScanning = scan;
        mOnBehalfDevice = scan ? device : null;
        mScanDelegatorDevice = delegatorDevice;

        // First update the live broadcast list
        List<BluetoothLeBroadcastMetadata> localSessionBroadcasts =
@@ -149,7 +144,7 @@ public class BroadcastScanViewModel extends AndroidViewModel {
        new_arr.addAll(mScanSessionBroadcasts.values());
        mAllBroadcasts.postValue(new_arr);

        mBluetooth.scanForBroadcasts(device, scan);
        mBluetooth.scanForBroadcasts(mScanDelegatorDevice, scan);
    }

    public void addBroadcastSource(BluetoothDevice sink, BluetoothLeBroadcastMetadata sourceMetadata) {
+1 −10
Original line number Diff line number Diff line
@@ -137,17 +137,8 @@ public class MainActivity extends AppCompatActivity {

        switch (item.getItemId()) {
            case R.id.action_scan:
                // Clicking this gives no device or receiver context - no extras for this intent.
                intent = new Intent(MainActivity.this, BroadcastScanActivity.class);
                // TODO: Why does this pass no information?
                //intent.putExtra(BluetoothBroadcastAudioScan.EXTRA_BASS_RECEIVER_ID, 0);

                // TODO: Change BluetoothAdapter.getDefaultAdapter() usages into BluetoothManager#getAdapter().
                BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();

                // What does this fake address mean?
                byte[] address = {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff};
                BluetoothDevice dev = mAdapter.getRemoteDevice(address);
                intent.putExtra(BluetoothDevice.EXTRA_DEVICE, dev);
                startActivity(intent);
                return true;