Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Issue with Plugin.BLE do not scan devices

$
0
0

Hi!

I'm facing a issue trying to connect to a Bluetooth LE device.

I downloaded the Plugin.BLE and I used the sample included without any problem, I connect and read/write data.

Now, I try to deploy on my App, I follow the instructions on the github repository, add permissions to AndroidManifest.xml. , but when I execute adapter.StartScanningForDevicesAsync(), nothing happens, I don't get the list of devices. There are several devices all around and also the one I want to connect to.

This is my code

public partial class PageSettingsDevice : ContentPage
{
IBluetoothLE ble;
IAdapter adapter;
ObservableCollection deviceList;
IDevice device;
private CancellationTokenSource _cancellationTokenSource;

    public PageSettingsDevice()
    {
        InitializeComponent();

        ble = CrossBluetoothLE.Current;
        adapter = CrossBluetoothLE.Current.Adapter;
        deviceList = new ObservableCollection<IDevice>();

        //adapter.DeviceDiscovered += _bleAdapterDeviceDiscovered;
    }

    private bool status()
    {
        var state = ble.State;

        DisplayAlert("Notice", state.ToString(), "OK !");
        if (state == BluetoothState.Off)
        {
            DisplayAlert("Atención", "Your Bluetooth is off ! Turn it on !", "Aceptar");
        }

        if (ble.State == BluetoothState.Off || ble.State == BluetoothState.TurningOff || ble.State == BluetoothState.Unauthorized || ble.State == BluetoothState.Unknown)
            return false;
        else
            return true;
    }

    async void OnDiscoverDeviceBlueToothClicked(object sender, EventArgs e)
    {
        try
        {
            if (status())
            {
                adapter.DeviceDiscovered += (s, a) =>
                {
                    if (device.Name.Contains("Coltech"))
                            adapter.ConnectToDeviceAsync(device);
                };

                //We have to test if the device is scanning 
                if (!ble.Adapter.IsScanning)
                {
                    _cancellationTokenSource = new CancellationTokenSource();

                    adapter.ScanMode = ScanMode.LowLatency;
                    await adapter.StartScanningForDevicesAsync(null,null,false,_cancellationTokenSource.Token);

                    var list = adapter.ConnectedDevices;
                }
            }                
        }
        catch (Exception ex)
        {
            DisplayAlert("Notice", ex.Message.ToString(), "Error !");
        }
    }

    async void _bleAdapterDeviceDiscovered(object sender, DeviceEventArgs e)
    {
        if (device != null)
        {
            if (device.Name.Contains("Coltech"))
                await adapter.ConnectToDeviceAsync(device);
        }
    }
}

Any help is wellcomed, thanks


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>