c# - Xamarin 蓝牙扫描

标签 c# android xamarin bluetooth

今天我开始使用 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);
        }
    }

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

最佳答案

在 Android 上,实现将是这样的:

_Manager = (BluetoothManager)appContext.GetSystemService("bluetooth");
_Adapter = _Manager.Adapter;
_LeScanner = _Adapter.BluetoothLeScanner;
 _BluetoothScanCallback = new BluetoothScanCallback();

然后当您开始扫描时,它会是这样的:

_LeScanner.StartScan(_BluetoothScanCallback);

其中 BluetoothScanCallback 将使用以下内容实现:

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

关于c# - Xamarin 蓝牙扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182618/

相关文章:

c# - 队列管理系统

Android 库项目未使用正确的调试变体资源

java - 为什么我的 View 的宽度和高度为 0

c# - 如何隐藏 Xamarin.Forms UWP 中的默认工具栏按钮(三个点)

xamarin - 在 VS2015 中,如何创建使用 .net 标准的 Xamarin xaml 表单项目?

c# - C# 类库的线程本地存储

c# - 使用 lambda 表达式获取数组元素相等的子集

java - 将 Google 身份验证的客户端 Web ID 放入 Strings.xml 中是否安全?

ios - 竞争条件 GCD

c# - VS2008安装包自动包含运行时库/框架