连续蓝牙扫描以接收原始广告包

时间:2019-04-30 23:43:55

标签: c# android xamarin bluetooth

我正在创建一个持续扫描蓝牙设备并根据其原始广告数据包对其进行过滤的应用程序。现在,我要从中接收数据包的设备没有配对功能,它就像信标一样。现在的问题是:我需要从设备传输每个数据包(每秒1个数据包),但是蓝牙扫描是一个昂贵的过程,并且OS不允许扫描过程持续很长时间。即使我强迫应用程序扫描更长的时间,我在一分钟内也只能收到10-12个数据包,而且这会因Android设备和版本而异。

扫描过程会接收每个蓝牙设备,检查它是否是我的设备,如果是,则将其保存到数据库。有没有办法我只能从设备中获取数据包而不与之配对?

 Intent receiverIntent = new Intent(context, typeof(MyReceiver)); // explicite intent
        receiverIntent.SetAction("com.company.ACTION_FOUND");
        PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 1, receiverIntent, PendingIntentFlags.UpdateCurrent);

        BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
        BluetoothLeScanner bluetoothLeScanner = adapter.BluetoothLeScanner;
        ScanFilter filter = GetScanFilter();
        List<ScanFilter> scanFilters = new List<ScanFilter>();
        scanFilters.Add(filter);

        mScanHandler.RemoveCallbacksAndMessages(null);
        mScanHandler.Post(new Java.Lang.Runnable(() =>
        {
            try
            {
                if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                {
                    bluetoothLeScanner.StartScan(scanFilters, GetScanSettings(), pendingIntent);
                }
                else
                {
                    bluetoothLeScanner.StartScan(scanFilters, GetScanSettings(), scanCallback);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

        }));
    }

    private ScanFilter GetScanFilter()
    {
        ScanFilter.Builder builder = new ScanFilter.Builder();
        return builder.Build();
    }

    private ScanSettings GetScanSettings()
    {
        ScanSettings.Builder builder = new ScanSettings.Builder();
        builder.SetScanMode(Android.Bluetooth.LE.ScanMode.LowLatency);
        return builder.Build();
    }


public class MScanCallBack : ScanCallback
{
    public override void OnScanResult([GeneratedEnum] ScanCallbackType callbackType, Android.Bluetooth.LE.ScanResult result)
    {
        base.OnScanResult(callbackType, result);
        Console.WriteLine("Scanning process: Device Found");
        if (result != null)
        {
            var bytes = result.ScanRecord.GetBytes();
            if (bytes[2] == 0xB0 && bytes[3] == 0xA0)
            {
              //Save data
            }
        }

    }

    public override void OnBatchScanResults(IList<Android.Bluetooth.LE.ScanResult> results)
    {
        base.OnBatchScanResults(results);
        Console.WriteLine("Scanning process: OnBatchScanResults");
    }

    public override void OnScanFailed([GeneratedEnum] ScanFailure errorCode)
    {
        base.OnScanFailed(errorCode);
        Console.WriteLine("Scanning process: Scan Failed" + errorCode.ToString());
    }
}

[BroadcastReceiver]
[IntentFilter(new[] { "com.company.ACTION_FOUND" })]
public class MyReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Console.WriteLine("Scanning process: Device Found");
        var action = intent.Action;
        if (intent.HasExtra(BluetoothLeScanner.ExtraListScanResult))
        {
            var x = intent.GetParcelableArrayListExtra(BluetoothLeScanner.ExtraListScanResult);
            var res = x.Cast<Android.Bluetooth.LE.ScanResult>().ToList();
            foreach (var result in res)
            {
                var bytes = result.ScanRecord.GetBytes();
                if (bytes[2] == 0xB0 && bytes[3] == 0xA0)
                {
                  //Save Data
                }
            }
        }
    }
}

0 个答案:

没有答案