簡體   English   中英

如何在 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() 

但這不是一個好方法,我需要像我的 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 地址。 如果您想查看我的完整源代碼,這是我用來實現我的 BLE 應用程序的 github 插件。這是我訪問 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特定項目中創建一個名為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);

因此,使用此地址,您可以在后台執行掃描操作並過濾發現的設備。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM