【问题标题】:Bluetooth Scanner for iOS and Android (Xamarin Forms)适用于 iOS 和 Android 的蓝牙扫描仪(Xamarin Forms)
【发布时间】:2020-05-16 08:35:41
【问题描述】:

我们正在实施适用于 iOS 和 Android 的 Xamarin Forms 应用程序。应用程序需要具有使用相机和蓝牙设备的条码扫描功能。虽然相机部分已经完成,但我们仍在寻找蓝牙设备集成。我们尝试了 Socket Scanner,它在 iOS 上完美运行,但在 Android 上却不行。是否有其他可以在 iOS 和 Android 上运行的蓝牙扫描仪?如果不是,我们应该分别为 iOS 和 Android 实现蓝牙。如果可用,请提供 SDK 和硬件的链接。

谢谢。

【问题讨论】:

    标签: xamarin.forms xamarin.android bluetooth xamarin.ios bluetooth-lowenergy


    【解决方案1】:

    对于 Xamarin 中的蓝牙,您可以使用来自 Nuget 的插件 Plugin.BLE

    首先,别忘了给特定平台添加权限

    在安卓中

    将以下行添加到 AndroidManifest.xml

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    

    在 iOS 中

    将以下行添加到 info.plist

    <key>UIBackgroundModes</key>
    <array>
        <!--for connecting to devices (client)-->
        <string>bluetooth-central</string>
    
        <!--for server configurations if needed-->
        <string>bluetooth-peripheral</string>
    </array>
    
    <!--Description of the Bluetooth request message (required on iOS 10, deprecated)-->
    <key>NSBluetoothPeripheralUsageDescription</key>
    <string>App want to access the bluetooth</string>
    
    <!--Description of the Bluetooth request message (required on iOS 13)-->
    <key>NSBluetoothAlwaysUsageDescription</key>
    <string>App want to access the bluetooth</string>
    

    用法

    var ble = CrossBluetoothLE.Current;
    var state = ble.State;
    var adapter = CrossBluetoothLE.Current.Adapter;
    

    更多详情和使用方法可以查看https://github.com/xabre/xamarin-bluetooth-le

    【讨论】:

    • 使用这个插件我们可以连接到任何类型的蓝牙条码扫描器吗?此外,我们可以在我们的应用程序上编写/显示结果吗??
    • 可以查看ble状态;发现设备;连接/断开连接;发现服务;发现特征;查看特征细节;读/写和注册特征通知;
    最近更新 更多