Judson Abraham
Judson Abraham

Reputation: 412

How to get the mac address of BLE device in code behind in Xamarin forms?

I'm connecting my Bluetooth BLE device in background. So I need to get the Bluetooth mac address in my code behind just like how we are displaying the mac address in our XAML.

ListView x:Name="lv" ItemSelected="lv_ItemSelected" BackgroundColor="White" SeparatorColor="Aqua"> 
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label TextColor="Black" Text="{Binding NativeDevice.Address}"/>
                                <Label TextColor="Black" Text="{Binding NativeDevice.Name}"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

so in our xaml I'm able to get the mac address in NativeDevice.Address. So same way I need to get it in my xaml.cs. I am able to get the mac address by following this approach

var vailditems = adapter.DiscoveredDevices.Where(i => i.NativeDevice.ToString() 

But this is not good approach I need to get the mac address in NativeDevice.Address just like my xaml. I tried this approach but it is giving the address as null.

public class NativeDevice
        {
            
            public string Name { get; set; }
            public string Address { get; set; }
        }
NativeDevice V = new NativeDevice();

                    Baddress = V.Address;

For your information the mac address is accessible in IDevice interface which is predefined. So in the IDevice interface I need to access the object NativeDevice. This is the interface.

public interface IDevice : IDisposable
{
   
    Guid Id { get; }
   
    string Name { get; }
   
    int Rssi { get; }
   
    object NativeDevice { get; }
   
    DeviceState State { get; }
    
    IList<AdvertisementRecord> AdvertisementRecords { get; }

    
   
    Task<IService> GetServiceAsync(Guid id, CancellationToken cancellationToken = default);
    
    Task<IList<IService>> GetServicesAsync(CancellationToken cancellationToken = default);
   
    Task<bool> UpdateRssiAsync();
}

So I need to access the interface and get the NativeDevice.address in code behind. And I will be removing the XAML part so I don't need the mac address from itemsource of ListView aswell. This is the github plugin I have used to implement my BLE app if you want to have a look at my full source code.This is my code where I'm accessing the BLE object.

public IDevice device;
var obj = device.NativeDevice;

Where the code for IDevice interface is

public interface IDevice : IDisposable
{
   
    Guid Id { get; }
   
    string Name { get; }
  Plugin.BLE.Abstractions.Contracts.IDevice.UpdateRssiAsync.
    int Rssi { get; }
   
    DeviceState State { get; }
   
     object NativeDevice { get; }
    Task<IList<IService>> GetServicesAsync(CancellationToken cancellationToken = default);
   
    Task<int> RequestMtuAsync(int requestValue);
  
    Task<bool> UpdateRssiAsync();
}

I don't have any clue regarding this. Any suggestions?

Upvotes: 4

Views: 3130

Answers (2)

Judson Abraham
Judson Abraham

Reputation: 412

For getting the address property in the code behind you need to do dependency service. So first create an interface called INativeDevice this is the code for the interface

public interface INativeDevice
    {
        //new object NativeDevice { get; }
        //void getaddress();
        NativeDevice ConvertToNative(object device);
    }

secondly create a class in android specific project called NativeDeviceConverter this is the code

[assembly: Xamarin.Forms.Dependency(typeof(NativeDeviceConverter))]
namespace Workshop.Droid.Injected
{
   public class NativeDeviceConverter:INativeDevice
    {
       
        public NativeDevice ConvertToNative(object device)
        {
            var dev = device as Device;
            if (dev != null)
                return new NativeDevice { Name = !string.IsNullOrEmpty(dev.BluetoothDevice.Name) ? dev.BluetoothDevice.Name : string.Empty, Address = dev.BluetoothDevice.Address };
            else
                return new NativeDevice();
        }
    }
}

After this write this code in your code behind to fetch the Address of your Bluetooth device

 NativeDeviceAdd = DependencyService.Get<INativeDevice>().ConvertToNative(a.Device);
                    PropertyInfo propInfo = NativeDeviceAdd.GetType().GetProperty("Address");
                     BLEaddressnew = (string)propInfo.GetValue(NativeDeviceAdd, null);

So with this Address you can do scan operation in background and filter your discovered device.

Upvotes: 3

Jason
Jason

Reputation: 89179

// this assumes that you already have the BLE object
// from the BLE plugin
var obj = myBLEobject.NativeDevice;
// we want the "Address" property
PropertyInfo propInfo = obj.GetType().GetProperty("Address"); 
string address = (string)propInfo.GetValue(obj, null);

Upvotes: 5

Related Questions