由于未显示代码,无法找到未处理的异常错误

时间:2018-09-10 16:46:38

标签: c# android xamarin bluetooth unhandled-exception

我有一个找不到的错误。它产生以下消息:System.NullReferenceException:对象引用未设置为对象的实例。 由于正在外部执行,因此未显示任何代码。该项目基于ble并使用了以下插件:https://github.com/xabre/xamarin-bluetooth-le

private BluetoothAdapter adapter;
    protected override async void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
        adapter = BluetoothAdapter.DefaultAdapter;
        MainActivity main = new MainActivity();
        adapter.Enable();
        //main.enableBluetoothOnDevice();
        await scanAsync();
    }
    List<Plugin.BLE.Abstractions.Contracts.IDevice> deviceList = new List<Plugin.BLE.Abstractions.Contracts.IDevice>();
    public async System.Threading.Tasks.Task scanAsync()
    {            
        MainPage page1 = new MainPage();            
        var ble = CrossBluetoothLE.Current;
        var adapter = CrossBluetoothLE.Current.Adapter;
        var state = ble.State;
        ble.StateChanged += (s, e) =>
        {
        };
        adapter.DeviceDiscovered += (s, a) => deviceList.Add(a.Device);
        await adapter.StartScanningForDevicesAsync();            
        int index = deviceList.FindIndex(item => item.Name == "CTQBLE");
        if (index >= 0)
        {         
            try
                {
                await adapter.ConnectToDeviceAsync(deviceList.Where(i => i.Name == "CTQBLE").FirstOrDefault());
                bleRecieveAsync();  //Error occurs here
                page1.deviceConnected();

                }
            catch (DeviceConnectionException e)
                {
                    page1.deviceNotConnected();
                }
        }    
        else
        {
            page1.deviceNotConnected();
            return;
        }
    }

    public async void  bleRecieveAsync()
    {
        var service1 = await deviceList.Where(i => i.Name == "CTQBLE").FirstOrDefault().GetServiceAsync(Guid.Parse("abcd0021-9c79-4ed6-a382-50946d830c3f"));
        var characteristicFirmware = await service1.GetCharacteristicAsync(Guid.Parse("abcd0022-9c79-4ed6-a382-50946d830c3f"));
        var bytesFirmware = await characteristicFirmware.ReadAsync();

        var service2 = await deviceList.Where(i => i.Name == "CTQBLE").FirstOrDefault().GetServiceAsync(Guid.Parse("abcd0021-9c79-4ed6-a382-50946d830c3f"));
        var characteristicRunState = await service2.GetCharacteristicAsync(Guid.Parse("abcd0023-9c79-4ed6-a382-50946d830c3f"));
        var bytesRunState = await characteristicRunState.ReadAsync();

        var service3 = await deviceList.Where(i => i.Name == "CTQBLE").FirstOrDefault().GetServiceAsync(Guid.Parse("abcd0021-9c79-4ed6-a382-50946d830c3f"));
        var characteristicConfig = await service3.GetCharacteristicAsync(Guid.Parse("abcd0024-9c79-4ed6-a382-50946d830c3f"));
        var bytesConfig = await characteristicConfig.ReadAsync();

        var service4 = await deviceList.Where(i => i.Name == "CTQBLE").FirstOrDefault().GetServiceAsync(Guid.Parse("abcd0021-9c79-4ed6-a382-50946d830c3f"));
        var characteristicError = await service4.GetCharacteristicAsync(Guid.Parse("abcd0025-9c79-4ed6-a382-50946d830c3f"));
        var bytesError = await characteristicError.ReadAsync();

        var service5 = await deviceList.Where(i => i.Name == "CTQBLE").FirstOrDefault().GetServiceAsync(Guid.Parse("abcd0021-9c79-4ed6-a382-50946d830c3f"));
        var characteristicCommand = await service5.GetCharacteristicAsync(Guid.Parse("abcd0026-9c79-4ed6-a382-50946d830c3f"));
        var bytesCommand = await characteristicCommand.ReadAsync();
    }

0 个答案:

没有答案