【问题标题】:How to get the mac address of BLE device in code behind in Xamarin forms?如何在 Xamarin 表单后面的代码中获取 BLE 设备的 mac 地址?
【发布时间】:2020-10-30 16:24:22
【问题描述】:

我正在后台连接我的蓝牙 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() 

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

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

                    Baddress = V.Address;

为了您的信息,可以在预定义的 IDevice 接口中访问 mac 地址。所以在 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 应用程序。这是我访问 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();
}

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

【问题讨论】:

标签: c# xamarin.forms bluetooth-lowenergy


【解决方案1】:
// 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);

【讨论】:

  • 这是一个简单的解决方案,它对我有用,但不确定在移动环境中使用反射是否好?
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多