Bluetooth device name is always null by using Plugin.BLE

BacNongTienBacNongTien VNMember ✭✭

Hi all,

I'm going to create a small POC app to discover all the bluetooth devices around. I found the Plugin.BLE component

I did the same thing with the sample code but only Device address displayed. The Native device name is always null.

Could you please give me some solution or figure out what the issue is?

Current Code is:

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

    public MainPage()
    {
        InitializeComponent();

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

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

    }

    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) =>
            {
                var x = DependencyService.Get<INativeDeviceConverter>().GetNativeDeviceName(a.Device);
                if (!list.Contains(a.Device))
                    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("Erro", ex.Message, "OK");
        }

    }

Thanks in advance,

Answers

  • PaulNTUPaulNTU USMember ✭✭✭

    Bluetooth is fun isn't it. You can't guarantee that you will get a device name of a Bluetooth device, sometimes it simply takes a few queries(Scans results) to get the name and sometimes they just dont announce a friendly name.

    Your best bet is to use the address property and replace that with the name if it is not null and is provided by the device.

  • BacNongTienBacNongTien VNMember ✭✭
    > @PaulNTU said:
    > Bluetooth is fun isn't it. You can't guarantee that you will get a device name of a Bluetooth device, sometimes it simply takes a few queries(Scans results) to get the name and sometimes they just dont announce a friendly name.
    >
    > Your best bet is to use the address property and replace that with the name if it is not null and is provided by the device.

    I have a Xamarin.android sample. BluetoothChat project, I ran and I could have a devices list with name. But Plugin.BLE is returning empty for device name.

    I have found some question in stackoverflow or github, they also had a same issue for Plugin.BLE

    Any one please?
Sign In or Register to comment.