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?