从 BluetoothLE 设备获取本机设备信息 [英] Getting native device information from BluetoothLE device

查看:23
本文介绍了从 BluetoothLE 设备获取本机设备信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xamarin 项目中使用 Plugin.BLE nuget 包来查询我拥有的 BLE 设备.它似乎工作正常,但是当触发找到的设备事件时,会返回一个对象作为整个设备对象的一部分.该对象称为 NativeDevice.

I'm using the Plugin.BLE nuget package in my Xamarin project to interrogate the BLE devices I have. It seems to be working fine, but there is an object returned as part of the overall device object when the found device event is triggered. The object is called NativeDevice.

Intellisense 表明这是一个可以在平台上操作的对象,这正是我想要做的,以便我可以在我的 mvvm 框架中存储和处理.

Intellisense is showing that this is an object which can be manipulated on the platform which is what I am trying to do so I can store and handle within my mvvm framework.

问题是,如果我将对象转换为平台上的设备并将其存储在 var 中,则 var 始终为 null.

The problem is that if I cast the object as a Device on the platform and store that in a var, the var is always null.

我应该如何从平台上的对象获取值,以便将这些值传递回我的视图模型?

How am I supposed to get the values from the object on the platform so I can pass these back to my view model?

我的代码是这样的

(在表单项目中)

adapter.DeviceDiscovered += (s, a) =>
        {
            var adList = new List<AdvertisingRecords>();
            foreach (var r in a.Device.AdvertisementRecords)
            {
                adList.Add(new AdvertisingRecords { Data = r.Data, Type = (AdvertisingRecordType)r.Type });
            }

            var newbtd = new BluetoothDevice
            {
                AdvertisementRecords = adList,
                NativeDevice = DependencyService.Get<INativeDevice>().ConvertToNative(a.Device.NativeDevice),
                Name = a.Device.Name,
                Rssi = a.Device.Rssi,
                Id = a.Device.Id,
                State = (BluetoothStates)a.Device.State
            };
            btd.Add(newbtd);
        };

在平台上

[assembly: Xamarin.Forms.Dependency(typeof(NativeDeviceConverter))]
namespace MyApp.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, Type = dev.BluetoothDevice.Type.ToString() };
            else
                return new NativeDevice();
        }
    }
}

NativeDevice 是我在虚拟机中使用的抽象类

NativeDevice is my abstracted class that I used within the VM

推荐答案

device value null 的原因是 a.Device.NativeDevice 的类型不是 Device 类型.它是 NativeObject,所以对于 android,它是 BluetoothDevice 类型,对于 ios,它是 CBPeripheral.

The Reason why device value null is that the type of a.Device.NativeDevice is not of Device type. It is NativeObject so for android it is of type BluetoothDevice and for ios, it is CBPeripheral.

正如我从您的代码中了解到的,您希望传递一个.Device,它又是 Device 类型.所以你只需要替换它这一行

As I understand from your code you want to pass a.Device which is in turn of type Device. So you just need to replace it this line

NativeDevice = DependencyService.Get<INativeDevice>().ConvertToNative(a.Device.NativeDevice)

NativeDevice = DependencyService.Get<INativeDevice>().ConvertToNative(a.Device)

现在您可以在 android 中使用 Device.BluethoothDevice.

Now you can use Device.BluethoothDevice in android.

这篇关于从 BluetoothLE 设备获取本机设备信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆