简体   繁体   English

如何在 Xamarin 形式的代码后面获取 BLE 设备的 mac 地址?

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

I'm connecting my Bluetooth BLE device in background.我正在后台连接我的蓝牙 BLE 设备。 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.所以我需要在我的代码中获取蓝牙 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>

so in our xaml I'm able to get the mac address in NativeDevice.Address.所以在我们的 xaml 中,我可以在 NativeDevice.Address 中获取 mac 地址。 So same way I need to get it in my xaml.cs.同样,我需要在我的 xaml.cs 中获取它。 I am able to get the mac address by following this approach我可以按照这种方法获取mac地址

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.但这不是一个好方法,我需要像我的 xaml 一样在 NativeDevice.Address 中获取 mac 地址。 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.供您参考,可在预定义的 IDevice 接口中访问 mac 地址。 So in the IDevice interface I need to access the object NativeDevice.所以在 IDevice 接口中我需要访问对象 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.所以我需要访问接口并在后面的代码中获取 NativeDevice.address 。 And I will be removing the XAML part so I don't need the mac address from itemsource of ListView aswell.我将删除 XAML 部分,因此我也不需要 ListView 的 itemsource 中的 mac 地址。 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.如果您想查看我的完整源代码,这是我用来实现我的 BLE 应用程序的 github 插件。这是我访问 BLE 对象的代码。

public IDevice device;
var obj = device.NativeDevice;

Where the code for IDevice interface is 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();
}

I don't have any clue regarding this.我对此一无所知。 Any suggestions?有什么建议?

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

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所以首先创建一个名为 INativeDevice 的接口,这是接口的代码

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

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.因此,使用此地址,您可以在后台执行扫描操作并过滤发现的设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM