首页 > 解决方案 > Xamarin C# , I Cant Run Plugin.BLE Codes

问题描述

I'm trying to make a very simple and basic mobile app for controlling my Arduino with Bluetooth. Don't have many C# experiences, but it was going well until the Bluetooth part comes. I've searched everywhere and found this https://github.com/xabre/xamarin-bluetooth-le and started to do instructions in the readme. But I've got errors every time.

first, I copied these codes to androidmanifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

after that, I added these codes to mainpage.xml.cs

    public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        var ble = CrossBluetoothLE.Current;
        var adapter = CrossBluetoothLE.Current.Adapter;
        var state = ble.State;
    }
}

But that caused 2 errors: Error CS0103 The name 'CrossBluetoothLE' does not exist in the current context BlueT

and I added using Plugin.BLE; so errors disappeared. and I continued to write;

adapter.DeviceDiscovered += (s,a) => deviceList.Add(a.Device);
await adapter.StartScanningForDevicesAsync();

after I paste these, I got that errors:

Error CS4033 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task Error CS0103 The name 'deviceList' does not exist in the current context

I tried everything but couldn't solve this problem, and I'm sorry like I said, don't have many # or Android programming experiences, just want to send these slider and button data to my Arduino: This is my mobile controller app

标签: c#androidxamarinbluetoothbluetooth-lowenergy

解决方案


推荐阅读