Xamarin蓝牙扫描

时间:2015-12-09 15:28:43

标签: 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 功能的回调参数。可以sombody请告诉我如何使用回调权利?

1 个答案:

答案 0 :(得分:4)

在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);
    }
}