Xamarin.Forms - BLE任何设备连接都丢失了

时间:2017-10-30 12:46:55

标签: xamarin bluetooth-lowenergy

在上一个项目中,我使用了BLE插件。

adapter.DeviceDiscovered += (s, a) =>

    {
       myDeviceList.Add(a.Device);
    }

        await adapter.StartScanningForDevicesAsync();

enter image description here

但是现在我只是在寻找设备,并将您直接找到的内容添加到此列表中。

我希望此扫描能够持续工作,如果有任何设备丢失,它可以在此处自动删除。

BLE有StartScanningForDevicesAsync类,但我不知道这对我有用。

 event EventHandler<DeviceErrorEventArgs> DeviceConnectionLost;
        //
        // Summary:
        //     Occurs when a device has been disconnected. This occurs on intendet disconnects
        //     after Plugin.BLE.Abstractions.Contracts.IAdapter.DisconnectDeviceAsync(Plugin.BLE.Abstractions.Contracts.IDevice).

这可能吗?

1 个答案:

答案 0 :(得分:0)

我想,你可以使用这样的东西(伪代码+ C#):

StartTimer(TimeSpan.FromSeconds(30),
            () =>
            {
                if (!isScanning)
                { 
                    new Handler().PostDelayed(StopScan, 10000); // stop 
                                      scanning after 10 sec
                    isScanning = true;
                    StartScan();
                }
               return true; // this result will tell to fire onTimer event again
           });

这里的StartScan和StopScan - 用于与BLE通信的功能