Xamarin 跨平台上的 BLE

BLE on Xamarin Cross Platform

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

您将照常需要 DependencyService to retrieve the list of available BLE devices and just display to the ListView

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

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

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

并连接到read/write/notify特征:

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