You need to enable JavaScript to run this app. 使用Plugin.BluetoothLE跨机蓝牙传输时GattServer方法报错咨询
优惠活动
大模型
产品
解决方案
定价
更多

使用Plugin.BluetoothLE跨机蓝牙传输时GattServer方法报错咨询

跨平台手机蓝牙数据传输开发问题

业务需求

  • 需要实现两台手机(含同平台iPhone间、跨操作系统设备间)的蓝牙数据传输能力
  • 应用对数据传输时间敏感度较高,实际使用场景无可用Wi-Fi网络,因此蓝牙是当前选定的最优传输方案

开发遇到的问题

  • 目前项目选用NuGet源上由aritchie开发、2020年3月完成最后更新的Plugin.BluetoothLE插件做蓝牙能力集成,但插件配套的所有示例代码均无法正常运行
  • 对照插件官方提供的GATT服务端示例实现逻辑时,触发方法缺失类编译错误:
    1. 通过CrossBleAdapter.Current.CreateGattServer()获取的实例中不存在AddService方法
    2. 通过CrossBleAdapter.Current.CreateGattServer()获取的实例中不存在Start方法

现有实现代码

以下是从表单页调用的相关蓝牙服务端实现代码:

using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Text;
using Plugin.BluetoothLE;
using Plugin.BluetoothLE.Server;

namespace BluetoothTest.Models
{
    public class BluetoothServer
    {

        public BluetoothServer()
        {

        }

        public void StartAdvertising()
        {
            //Guid[] guidArray = new Guid[1];
            List<Guid> guidArray;
            guidArray = new List<Guid>();
            guidArray.Add(Guid.NewGuid());

            CrossBleAdapter.Current.Advertiser.Start(new AdvertisementData
            {
                LocalName = "TestServer",
                ServiceUuids = guidArray
            });
        }

        public void StopAdvertising()
        {

        }

        public async void SetUpServer()
        {
            var server = CrossBleAdapter.Current.CreateGattServer();
            var service = server.AddService(Guid.NewGuid(), true);

            var characteristic = service.AddCharacteristic(
                Guid.NewGuid(),
                CharacteristicProperties.Read | CharacteristicProperties.Write | CharacteristicProperties.WriteNoResponse,
                GattPermissions.Read | GattPermissions.Write
            );

            var notifyCharacteristic = service.AddCharacteristic
            (
                Guid.NewGuid(),
                CharacteristicProperties.Indicate | CharacteristicProperties.Notify,
                GattPermissions.Read | GattPermissions.Write
            );

            IDisposable notifyBroadcast = null;
            notifyCharacteristic.WhenDeviceSubscriptionChanged().Subscribe(e =>
            {
                var @event = e.IsSubscribed ? "Subscribed" : "Unsubcribed";

                if (notifyBroadcast == null)
                {
                    this.notifyBroadcast = Observable
                        .Interval(TimeSpan.FromSeconds(1))
                        .Where(x => notifyCharacteristic.SubscribedDevices.Count > 0)
                        .Subscribe(_ =>
                        {
                            Debug.WriteLine("Sending Broadcast");
                            var dt = DateTime.Now.ToString("g");
                            var bytes = Encoding.UTF8.GetBytes(dt);
                            notifyCharacteristic.Broadcast(bytes);
                        });
                }
            });

            characteristic.WhenReadReceived().Subscribe(x =>
            {
                var write = "HELLO";

                // you must set a reply value
                x.Value = Encoding.UTF8.GetBytes(write);

                x.Status = GattStatus.Success; // you can optionally set a status, but it defaults to Success
            });
            characteristic.WhenWriteReceived().Subscribe(x =>
            {
                var write = Encoding.UTF8.GetString(x.Value, 0, x.Value.Length);
                // do something value
            });

            await server.Start(new AdvertisementData
            {
                LocalName = "TestServer"
            });
        }
    }
}

待解决诉求

  • 定位上述实现代码中触发AddService、Start方法缺失报错的具体原因
  • 推荐适配无Wi-Fi场景、可实现双手机低延迟蓝牙数据传输的更优技术方案

内容的提问来源于stack exchange,提问作者Oliver Spencer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.23 20:54:07