【问题标题】:BLE on Xamarin Cross PlatformXamarin 跨平台上的 BLE
【发布时间】:2017-03-02 11:21:47
【问题描述】:

在 Xamarin.Form 跨平台中,有没有办法在弹出对话框或菜单列表中显示可用 BLE 设备列表(扫描后),然后从弹出对话框中连接所需的 BLE 设备框并在为显示数据而创建的 UI 上显示数据??????

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    您将需要DependencyService 来检索可用的 BLE 设备列表并像往常一样显示到ListView

    此外,Xamarin 制作的以下组件可以帮助您轻松找到适用于 iOS 和 Android 的 BLE 设备。

    https://components.xamarin.com/view/Monkey.Robotics

    【讨论】:

      【解决方案2】:

      Xamarin 不提供开箱即用的跨平台 BLE 支持。有一些第三方库提供了跨平台的 BLE 功能,其中一个是我在一个原生 BLE 项目上花了 1-2 年之后写的:https://github.com/nexussays/ble.net

      有一个完整的适用于 iOS、Android 和 UWP (https://github.com/nexussays/ble.net/tree/master/test/ble.net.sampleapp) 的 Xamarin.Forms 项目,它提供了您所要求的确切功能——扫描、显示结果、连接、显示对话框等。但要简要介绍API在这里...

      你可以这样扫描:

      await adapter.ScanForDevices(
         ( IBlePeripheral peripheral ) =>
         {
            // check if this is the device you want to connect to
            // e.g., query peripheral.Advertisement.Services
         },
         cancellationTokenSource.Token );
      

      并连接到读/写/通知特性:

      var connection = await adapter.ConnectToDevice( peripheral, TimeSpan.FromSeconds( 5 ));
      if(connection.IsSuccessful())
      {
         var gatt = connection.GattServer;
         var value = await gatt.ReadCharacteristicValue( someServiceGuid, someCharacteristicGuid );
         await gatt.WriteCharacteristicValue( someServiceGuid, someCharacteristicGuid, new byte[]{ 1, 2, 3 } );
         // etc...
      }
      else
      {
         Debug.WriteLine( "Error connecting to device. result={0:g}", connection.ConnectionResult );
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-04
        相关资源
        最近更新 更多