Xamarin Plugin.BLE device not found (mobile app)

2022-07-13T01:45:20.877+00:00

Microsoft Visual Studio Enterprise 2019
버전 16.11.17

Assembly Name : Smart_Card

  1. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.smart_card" android:installLocation="auto">
<uses-sdk android:minSdkVersion="22" android:targetSdkVersion="30" />
<application android:label="Smart_Card.Android" android:theme="@STYLE /MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
</manifest>

  1. MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Smart_Card.MainPage">

<StackLayout>  
    <Button Text="Search" Clicked="searchDevice"/>  
    <ListView x:Name="DevicesList"  
                  CachingStrategy="RecycleElement"  
                  ItemSelected="DevicesList_OnItemSelected">  
        <ListView.ItemTemplate>  
            <DataTemplate>  
                <ViewCell>  
                    <StackLayout>  
                        <Label Text="{Binding name}"></Label>  
                    </StackLayout>  
                </ViewCell>  
            </DataTemplate>  
        </ListView.ItemTemplate>  
    </ListView>  
      
    <!-- -->  
    <StackLayout Orientation="Horizontal">  
        <Button x:Name="btnStatus" Text="Status" Clicked="btnStatus_Clicked"/>  
        <Button x:Name="btnScan" Text="Scan" Clicked="btnScan_Clicked"/>  
        <Label x:Name="txtErrorBle" Text=""></Label>  
    </StackLayout>  
      
</StackLayout>  

</ContentPage>

  1. MainPage.xaml.cs

using System;
using Xamarin.Forms;

using Plugin.BLE;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.Exceptions;
using System.Collections.ObjectModel;

namespace Smart_Card
{
public partial class MainPage : ContentPage
{
IAdapter adapter;
IBluetoothLE bluetoothBLE;
ObservableCollection<IDevice> list;
IDevice device;

    //  
    //IAdapter adapter;  
    IBluetoothLE ble;  
    ObservableCollection<IDevice> deviceList;  
    //IDevice device;  

    public MainPage()  
    {  
        InitializeComponent();  

        bluetoothBLE = CrossBluetoothLE.Current;  
        adapter = CrossBluetoothLE.Current.Adapter;  

        list = new ObservableCollection<IDevice>();  
        DevicesList.ItemsSource = list;  

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

        //-  
        DevicesList.ItemsSource = deviceList;  

    }  

    private async void searchDevice(object sender, EventArgs e)  
    {  
        if (bluetoothBLE.State == BluetoothState.Off)  
        {  
            await DisplayAlert("Message", "Bluetooth is not available.", "OK");  
        }  
        else  
        {  
            list.Clear();  

            adapter.ScanTimeout = 10000;  
            adapter.ScanMode = ScanMode.Balanced;  

            adapter.DeviceDiscovered += (obj, a) =>  
            {  
                if (a.Device.Name != null)  
                {  
                    list.Add(a.Device);  
                }  
            };  
            await adapter.StartScanningForDevicesAsync();  
        }  
    }  

    private async void DevicesList_OnItemSelected(object sender, SelectedItemChangedEventArgs e)  
    {  
        device = DevicesList.SelectedItem as IDevice;  

        var result = await DisplayAlert("Message", "Do you want to connect to this device?", "Connect", "Cancel");  

        if (!result)  
            return;  

        //Stop Scanner  
        await adapter.StopScanningForDevicesAsync();  

        try  
        {  
            await adapter.ConnectToDeviceAsync(device);  

            await DisplayAlert("Conectado", "Status:" + device.State, "OK");  

        }  
        catch (DeviceConnectionException ex)  
        {  
            await DisplayAlert("Error", ex.Message, "OK");  
        }  

    }  


    //======  
    private void btnStatus_Clicked(object sender, EventArgs e)  
    {  
        var state = ble.State;  
        DisplayAlert("Notice", state.ToString(), "OK");  

        //adapter.ScanTimeout = 10000;  
        //adapter.ScanMode = ScanMode.Balanced;  

        if (state == BluetoothState.Off)  
        {  
            txtErrorBle.BackgroundColor = Color.Red;  
            txtErrorBle.TextColor = Color.White;  
            txtErrorBle.Text = "Your Bluetooth is off ! Turn it on !";  
        }  
    }  

    private async void btnScan_Clicked(object sender, EventArgs e)  
    {  
        try  
        {  
            deviceList.Clear();  
            adapter.DeviceDiscovered += (s, a) =>  
            {  
                deviceList.Add(a.Device);  
            };  

            if (!ble.Adapter.IsScanning)  
            {  
                await adapter.StartScanningForDevicesAsync();  
            }  
            await DisplayAlert("deviceList", deviceList.ToString(), "Confirm");  
        }  
        catch (Exception ex)  
        {  
            await DisplayAlert("Notice", ex.Message.ToString(), "Error");  
        }  
    }  


}  

}

The program runs,
The Bluetooth device is not being found.
Could you please let me know which part needs to be corrected.
Maybe it's a version issue?

kdg000@empas.com

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,072 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Agustin Bonilla 1 Reputation point
    2022-10-26T15:27:10.763+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.