Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Xamarin.Forms wont connect to a Bluetooth device with Plugin.BLE

$
0
0

Firstly i dont like to ask questions, because all of my problems i can solve it, but this is strange. I do multiple ways to connect to a bluetooth device, but wont connect in Xamarin.Forms. I give all the permissions (what the BLE documentation wrote) to Android and ios but wont work. Nothing happens.

I dont know what is the problem.

What do im wrong?

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class bc_layout_new : ContentPage
{
IBluetoothLE ble;
IAdapter adapter;
IDevice device;
CancellationTokenSource _cancellationTokenSource;

    public bc_layout_new()
    {
        InitializeComponent();
        ble = CrossBluetoothLE.Current;
        adapter = CrossBluetoothLE.Current.Adapter;
        _cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
    }

    private async void ConnectToMyDevice()
    {
        await adapter.StopScanningForDevicesAsync();
        using (device = await adapter.ConnectToKnownDeviceAsync(
            new Guid("12345678-1234-1234-1234-123456789ABC"), new ConnectParameters(true, false),
            cancellationToken: _cancellationTokenSource.Token))
        {

            if (device.State == DeviceState.Connected)
            {
                Debug.WriteLine("OK");
            }
            else
            {
                Debug.WriteLine("Error");
            }

        }
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        if (ble.State == BluetoothState.On)
        {
            Device.BeginInvokeOnMainThread(ConnectToMyDevice);
        }
        else if (ble.State == BluetoothState.Off)
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                bool res = await DisplayAlert("Notice", "Disconnected!", "OK", "Cancel");
            });
        }
    }
}

Viewing all articles
Browse latest Browse all 91519

Trending Articles



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