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

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

我正在后台连接我的蓝牙 BLE 设备。所以我需要在我的代码中获取蓝牙 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>

所以在我们的 xaml 中,我能够在 NativeDevice.Address 中获得 mac 地址。同样,我需要在我的 xaml.cs 中获取它。我可以通过这种方法获得 mac 地址

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

但这不是好方法我需要在 NativeDevice.Address 中获取 mac 地址,就像我的 xaml 一样。我试过这种方法,但它给出的地址为空。

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

                    Baddress = V.Address;

供您参考,mac 地址可在预定义的 IDevice 界面中访问。所以在 IDevice 接口中我需要访问对象 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();
}

所以我需要访问接口并在后面的代码中获取NativeDevice.address。我将删除 XAML 部分,因此我也不需要 ListView 的 itemsource 中的 mac 地址。 This is the github plugin 如果您想查看我的完整源代码,我已经实现了我的 BLE 应用程序 code.This 是我访问 BLE 对象的代码。

public IDevice device;
var obj = device.NativeDevice;

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();
}

我对此一无所知。有什么建议吗?

// 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);

为了获取后面代码中的地址属性,需要做依赖服务。所以首先创建一个名为 INativeDevice 的接口,这是接口的代码

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

其次在 android 特定项目中创建一个 class 名为 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);

因此,使用此地址,您可以在后台进行扫描操作并过滤您发现的设备。