简体   繁体   English

蓝牙 LE 扫描仪 Xamarin Android

[英]Bluetooth LE scanner Xamarin Android

What I want to do with my application is to be able to read the characteristics of the scanned devices.我想用我的应用程序做的是能够读取扫描设备的特征。 I'm able to scan devices, but the scan will keep taking in whatever it finds.我能够扫描设备,但扫描将继续接收它发现的任何内容。 So I would like to have an filter for that as well.所以我也想要一个过滤器。 An attached picture of my results are located below my codes.我的结果的附加图片位于我的代码下方。

Here are my codes这是我的代码

using Robotics.Mobile.Core.Bluetooth.LE;
using Adapter = Robotics.Mobile.Core.Bluetooth.LE.Adapter;

var appContext = Application.Context;
            _manager = (BluetoothManager)appContext.GetSystemService(BluetoothService); // ("bluetooth");
            _adapter = _manager.Adapter;

            _bleAdapter = new Adapter();
            _bleAdapter.DeviceDiscovered += _bleAdapter_DeviceDiscovered;
            _bleAdapter.ScanTimeoutElapsed += _bleAdapter_ScanTimeoutElapsed;
        }

        private void _bleAdapter_ScanTimeoutElapsed(object sender, EventArgs e)
        {
            _bleAdapter.StopScanningForDevices();
            DisplayInformation("Bluetooth scan timeout elapsed, no ble devices were found");
        }

        private void _bleAdapter_DeviceDiscovered(object sender, DeviceDiscoveredEventArgs e)
        {
            var msg = string.Format(@"Device found: {0}
  {1} - {2}", e.Device.Name, e.Device.ID, e.Device.Rssi);
            DisplayInformation(msg);
        }

        private void ButtonScanBleClick(object sender, EventArgs e)
        {
            if (!_bleAdapter.IsScanning)
                _bleAdapter.StartScanningForDevices();
        }

        private void DisplayInformation(string line)
        {
            _textboxResults.Text = $"{line}\r\n{_textboxResults.Text}";
            Console.WriteLine(line);
        }

Picture of my output result Here我的输出结果图片在这里

If I understand well, you want some filter for found devices.如果我理解得很好,您需要对找到的设备进行一些过滤。 You have to write a bluetooth scanner callback, and then something like this:你必须写一个蓝牙扫描器回调,然后是这样的:

in callback:在回调中:

public void OnLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
        {
            if (device.Name != null)
            {
                EventScanDevice(device, rssi);                
            }
        }

in MainActivity:在主活动中:

List<string> scanList;

private void Scanner_EventScanDevice(BluetoothDevice device, int rssi)
{
 if (!scanList.Contains(device.Name))
 {
      scanList.Add(device.Name + ", RSSI = " + rssi.ToString());
              
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM