未将对象引用设置为对象的实例 .net maui

问题描述 投票:0回答:1

我正在尝试开发一个 .NET MAUI 应用程序,使用 Shiny.BluetoothLE 连接到另一台设备。

BluePage.xaml

using Shiny;
using Shiny.BluetoothLE;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Text;

namespace shinyTest;

public partial class BluePage : ContentPage
{
    private IDisposable _scanSubscription;
    private readonly IBleManager _bleManager;
    private List<string> lst;

    public BluePage()
    {
        InitializeComponent();
        _bleManager = Shiny.Hosting.Host.GetService<IBleManager>();
    }
    private async void scan_btn_Clicked(object sender, EventArgs e)
    {
            _scanSubscription = _bleManager.Scan()
        .Where(device => device.Rssi > -70) // Filter based on signal strength (optional)
        .Subscribe(async device =>
        {
            try
            {
                string connectionStatus;
                if (device.Rssi > -60) // Adjust threshold based on your needs
                {
                    connectionStatus = "Likely Connected";
                    //  await DisplayAlert("Response", $"{device.Peripheral.Name}", "Ok");
                    var connectedDevice = device.Peripheral.ConnectAsync();
                    if (connectedDevice != null)
                    {
                        string msg = "Hello World!";
                        byte[] buffer = Encoding.UTF8.GetBytes(msg);

                        var uuid = device.Peripheral.Uuid.ToString();
                        var charUUID = Guid.Parse("0000FFF2-0000-1000-8000-00805f9b34fb");
                        await DisplayAlert("Response", $"{uuid}", "Ok");

                        using (var trans = device.Peripheral.TryBeginTransaction())
                        {
                            await trans.Write(device.Peripheral,uuid, charUUID.ToString(), buffer);
                            await DisplayAlert("Response", "Message sent!!", "Ok");
                        }
                    }
                    else
                    {
                        await DisplayAlert("Error", "No devices found", "Ok");
                    }
                }
                else
                {
                    connectionStatus = "Disconnected";
                    await DisplayAlert("Response", $"{connectionStatus}", "Ok");
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Exception", $"{ex.Message}", "Ok");
            }
            
        });
        }    
}

连接的设备显示正确,但当我尝试编写某些内容时,它遇到了此异常 ---“对象引用未设置到对象的实例”

帮助表示赞赏!

c# android .net xamarin maui
1个回答
0
投票
private async void scan_btn_Clicked(object sender, EventArgs e)
{
    _scanSubscription = _bleManager.Scan()
        .Where(device => device.Rssi > -70) // Filter based on signal strength
        .Subscribe(async device =>
        {
            try
            {
                if (device?.Peripheral == null)
                {
                    await DisplayAlert("Error", "Peripheral is null", "Ok");
                    return;
                }

                string connectionStatus = device.Rssi > -60 ? "Likely Connected" : "Disconnected";
                await DisplayAlert("Response", $"{connectionStatus}", "Ok");

                if (device.Rssi > -60)
                {
                    // Attempt to connect to the device
                    var connectedDevice = await device.Peripheral.ConnectAsync();
                    if (connectedDevice != null)
                    {
                        string msg = "Hello World!";
                        byte[] buffer = Encoding.UTF8.GetBytes(msg);
                        var uuid = device.Peripheral.Uuid.ToString();
                        var charUUID = Guid.Parse("0000FFF2-0000-1000-8000-00805f9b34fb");
                        await DisplayAlert("Response", $"{uuid}", "Ok");

                        using (var trans = await device.Peripheral.TryBeginTransaction())
                        {
                            if (trans != null)
                            {
                                await trans.Write(device.Peripheral, uuid, charUUID.ToString(), buffer);
                                await DisplayAlert("Response", "Message sent!!", "Ok");
                            }
                            else
                            {
                                await DisplayAlert("Error", "Transaction failed", "Ok");
                            }
                        }
                    }
                    else
                    {
                        await DisplayAlert("Error", "Failed to connect to device", "Ok");
                    }
                }
                else
                {
                    await DisplayAlert("Response", "Weak Signal", "Ok");
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Exception", $"{ex.Message}", "Ok");
            }
        });
}

当您尝试对其执行操作时,您的设备外围设备似乎不为空并且已连接。在尝试使用外设之前,您可能需要确保设备确实已连接 一旦显式检查设备外设是否为空。

© www.soinside.com 2019 - 2024. All rights reserved.