Xamarin蓝牙扫描

Ale*_*tny 2 c# android bluetooth xamarin

今天,我开始使用C#进行开发,并尝试扫描信标。这是我走多远。

        protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);            
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        BluetoothAdapter oBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
        BluetoothLeScanner oScanner = oBluetoothAdapter.BluetoothLeScanner;

        ScanCallback oCallback;



        if(!oBluetoothAdapter.IsEnabled)
        {
            StartActivity(new Intent(BluetoothAdapter.ActionRequestEnable));
        } 
        else
        {
            oScanner.StartScan(oCallback);
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何使用StartScan函数的回调参数。有人可以告诉我如何使用回调吗?

Jay*_*obs 5

在android上,实现将如下所示:

_Manager = (BluetoothManager)appContext.GetSystemService("bluetooth");
_Adapter = _Manager.Adapter;
_LeScanner = _Adapter.BluetoothLeScanner;
 _BluetoothScanCallback = new BluetoothScanCallback();
Run Code Online (Sandbox Code Playgroud)

然后,当您开始扫描时,将如下所示:

_LeScanner.StartScan(_BluetoothScanCallback);
Run Code Online (Sandbox Code Playgroud)

在哪里BluetoothScanCallback可以使用类似的东西来实现:

public class BluetoothScanCallback : ScanCallback
{
    public override void OnScanResult([GeneratedEnum] ScanCallbackType callbackType, ScanResult result)
    {
        base.OnScanResult(callbackType, result);
    }
}
Run Code Online (Sandbox Code Playgroud)