No device found whille searching Bluetooth Devices on OnePlus

123 views Asked by At

I have an oneplus 8 pro device and test our android application (Xamarin).

When I try with samsung devices there is no problem I can discover BLE devices but when I try with oneplus, I got nothing, OnReceive method has not been triggered for ActionBondStateChanged, ActionFound as well as ActionDiscoveryFinished.

But when open bluetooth settings and touch "pair new device", it searches and when I go back to my app, ActionDiscoveryFinished is triggered (but not ActionFound)

PS: I turn off battery save mode.

I really do not know what may cause this problem. Here is my code

public class Receiver : BroadcastReceiver
{
    actDetectBluetoothDevices _parentActivity;

    public Receiver(actDetectBluetoothDevices parentActivity)
    {
        _parentActivity = parentActivity;
    }

    public override void OnReceive(Context context, Intent intent)
    {
        string action = intent.Action;

        // When discovery finds a device
        switch (action)
        {
            // Get the BluetoothDevice object from the Intent
            case BluetoothDevice.ActionFound:
                
                var device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
                
                
                // If it's already paired, skip it, because it's been listed already
                if (device.BondState != Bond.Bonded)
                {
                    // check if the device has been already added, if added then skip it.. (prevent duplication)
                    if (!_addedBluetoothDeviceAddresses.Contains(device.Address))
                    {
                        _addedBluetoothDeviceAddresses.Add(device.Address);
                        _newDevicesArrayAdapter.Add(string.Format("{0}\n{1}", device.Name, device.Address));
                    }
                }

                break;

            // When discovery is finished, change the Activity title
            case BluetoothAdapter.ActionDiscoveryFinished:

                _parentActivity.ShowProgressBar(false);
                _parentActivity.ShowScanButton(true);
                _parentActivity.SetTitle(Resource.String.config_bt_select_device);
                if (_newDevicesArrayAdapter.Count == 0)
                {
                    var noDevices = _parentActivity.Resources.GetText(Resource.String.config_bt_none_found);
                    _newDevicesArrayAdapter.Add(noDevices);
                }
                break;
        }
    }

}

And here how I initialize

        _receiver = new Receiver(this);
        var filter = new IntentFilter(BluetoothDevice.ActionFound);
        RegisterReceiver(_receiver, filter);

        RegisterReceiver(_receiver, new IntentFilter(BluetoothDevice.ActionBondStateChanged));
        
        
        // Register for broadcasts when discovery has finished
        filter = new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished);
        RegisterReceiver(_receiver, filter);

        _btAdapter = DeviceTools.GetBluetoothAdapter(this);
       _btAdapter.StartDiscovery();

and this method gives me an instance of BluetoothAdapter

    public static BluetoothAdapter GetBluetoothAdapter(Context context)
    {
        BluetoothAdapter adapter = null;

        // Retrieve the adapter differently based on the current android version
        if (IsOsVersionEqualOrLessThan(BuildVersionCodes.JellyBeanMr1))
        {
            adapter = BluetoothAdapter.DefaultAdapter;
        }
        else if (IsOsVersionEqualOrGreaterThan(BuildVersionCodes.JellyBeanMr2))
        {
            var bluetoothManager = (BluetoothManager)context.GetSystemService(Context.BluetoothService);
            adapter = bluetoothManager.Adapter;
        }

        return adapter;
    }

PS: It enters "else if" part on the code above. I've tried also with BluetoothAdapter.DefaultAdapter but same result..

0

There are 0 answers