Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 蓝牙扫描仪Xamarin Android_C#_Android_Xamarin_Bluetooth_Bluetooth Lowenergy - Fatal编程技术网

C# 蓝牙扫描仪Xamarin Android

C# 蓝牙扫描仪Xamarin Android,c#,android,xamarin,bluetooth,bluetooth-lowenergy,C#,Android,Xamarin,Bluetooth,Bluetooth Lowenergy,我希望我的应用程序能够读取扫描设备的特征。我可以扫描设备,但扫描会继续接收它发现的任何东西。所以我也想要一个过滤器。我的代码下方附有我的结果图片 这是我的密码 using Robotics.Mobile.Core.Bluetooth.LE; using Adapter = Robotics.Mobile.Core.Bluetooth.LE.Adapter; var appContext = Application.Context; _manager = (Bluetoo

我希望我的应用程序能够读取扫描设备的特征。我可以扫描设备,但扫描会继续接收它发现的任何东西。所以我也想要一个过滤器。我的代码下方附有我的结果图片

这是我的密码

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

我的输出结果的图片

如果我理解得很好,您需要对找到的设备进行一些筛选。 您必须编写一个蓝牙扫描仪回调,然后执行如下操作:

在回调中:

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

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

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

private void Scanner_EventScanDevice(BluetoothDevice device, int rssi)
{
 if (!scanList.Contains(device.Name))
 {
      scanList.Add(device.Name + ", RSSI = " + rssi.ToString());
              
 }
}
列表扫描列表;
专用无效扫描程序\u EventsScandevice(蓝牙设备,int rssi)
{
如果(!scanList.Contains(device.Name))
{
添加(device.Name+”,RSSI=“+RSSI.ToString());
}
}