C# 如何在Xamarin表单的代码隐藏中获取BLE设备的mac地址?

C# 如何在Xamarin表单的代码隐藏中获取BLE设备的mac地址?,c#,xamarin.forms,bluetooth-lowenergy,C#,Xamarin.forms,Bluetooth Lowenergy,我正在后台连接蓝牙设备。所以我需要在我的代码中获得蓝牙mac地址,就像我们在XAML中显示mac地址一样 ListView x:Name="lv" ItemSelected="lv_ItemSelected" BackgroundColor="White" SeparatorColor="Aqua"> <ListView.ItemTemplate>

我正在后台连接蓝牙设备。所以我需要在我的代码中获得蓝牙mac地址,就像我们在XAML中显示mac地址一样

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>
但这不是一个好方法,我需要在NativeDevice中获取mac地址。地址就像我的xaml一样。我尝试了这种方法,但它给出的地址为空

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

                    Baddress = V.Address;
有关您的信息,可以在预定义的IDevice接口中访问mac地址。因此,在IDevice接口中,我需要访问object NativeDevice。这是接口

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();
}
其中IDevice接口的代码为

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();
}
公共接口设备:IDisposable
{
Guid Id{get;}
字符串名称{get;}
Plugin.BLE.Abstractions.Contracts.IDevice.UpdatersAsiaSync。
int Rssi{get;}
DeviceState状态{get;}
对象本地设备{get;}
任务GetServicesSync(CancellationToken CancellationToken=默认值);
任务请求mtuasync(int requestValue);
任务更新程序同步();
}

关于这件事我没有任何线索。有什么建议吗?

要在代码中获取address属性,您需要执行依赖项服务。首先创建一个名为INativeDevice的接口,这是该接口的代码

// 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);
public interface INativeDevice
    {
        //new object NativeDevice { get; }
        //void getaddress();
        NativeDevice ConvertToNative(object device);
    }
其次,在android特定项目中创建一个名为NativeDeviceConverter的类。这是代码

[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();
        }
    }
}
在此之后,在代码隐藏中写入此代码,以获取蓝牙设备的地址

 NativeDeviceAdd = DependencyService.Get<INativeDevice>().ConvertToNative(a.Device);
                    PropertyInfo propInfo = NativeDeviceAdd.GetType().GetProperty("Address");
                     BLEaddressnew = (string)propInfo.GetValue(NativeDeviceAdd, null);
NativeDeviceAdd=DependencyService.Get().convertonative(一种设备);
PropertyInfo-propInfo=NativeDeviceAdd.GetType().GetProperty(“地址”);
BLEaddressnew=(字符串)propInfo.GetValue(NativeDeviceAdd,null);
因此,使用此地址,您可以在后台执行扫描操作,并筛选发现的设备。

选择正确的类型或获取值
[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();
        }
    }
}
 NativeDeviceAdd = DependencyService.Get<INativeDevice>().ConvertToNative(a.Device);
                    PropertyInfo propInfo = NativeDeviceAdd.GetType().GetProperty("Address");
                     BLEaddressnew = (string)propInfo.GetValue(NativeDeviceAdd, null);