Xamarin Can';t在蓝牙上做广告

Xamarin Can';t在蓝牙上做广告,xamarin,bluetooth,Xamarin,Bluetooth,我想在我的Xamarin.Forms应用程序中创建一个Gatt服务器,以便其他设备可以通过蓝牙进行扫描。我正在使用这个插件: 这是我创建Gatt服务器和公布数据的代码: server = CrossBleAdapter.Current.CreateGattServer(); var service = server.AddService(serviceGuid, true); var characteristic = service.AddCharacteristic(

我想在我的Xamarin.Forms应用程序中创建一个Gatt服务器,以便其他设备可以通过蓝牙进行扫描。我正在使用这个插件:

这是我创建Gatt服务器和公布数据的代码:

server = CrossBleAdapter.Current.CreateGattServer();
var service = server.AddService(serviceGuid, true);

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

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

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

    if (notifyBroadcast == null)
    {
        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("SendingBroadcast");
                            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);
                Debug.WriteLine("in WhenWriteReceived() value: " + write);
            // do something value
        });

            await server.Start(new AdvertisementData
            {
                LocalName = "DariusServer",
                ServiceUuids = new List<Guid>() { serverServiceGuid }
            });
server=crossleadapter.Current.CreateGattServer();
var service=server.AddService(serviceGuid,true);
var characteristic=service.AddCharacteristic(
特色指南,
特征属性。读取|
CharacteristicProperties.Write | CharacteristicProperties.WritenorResponse,
GattPermissions.Read | GattPermissions.Write
);
var notifyCharacteristic=service.AddCharacteristic
(
通知特征GUID,
特征属性。指出|特征属性。通知,
GattPermissions.Read | GattPermissions.Write
);
IDisposable notifyBroadcast=null;
notifyCharacteristic.WhenDeviceSubscriptionChanged().Subscribe(e=>
{
var@event=e.IsSubscribed?“Subscribed”:“unsubscribed”;
if(notifyBroadcast==null)
{
可观察的
.间隔(时间跨度从秒(1))
.Where(x=>notifyCharacteristic.SubscribedDevices.Count>0)
.Subscribe(=>
{
Debug.WriteLine(“发送广播”);
var dt=DateTime.Now.ToString(“g”);
var bytes=Encoding.UTF8.GetBytes(“发送广播”);
广播(字节);
});
}
});
characteristic.WhenReceived().Subscribe(x=>
{
var write=“你好”;
//必须设置回复值
x、 Value=Encoding.UTF8.GetBytes(写入);
x、 Status=GattStatus.Success;//您可以选择设置状态,但默认为Success
});
characteristic.WhenWriteReceived().Subscribe(x=>
{
var write=Encoding.UTF8.GetString(x.Value,0,x.Value.Length);
WriteLine(“在WhenWriteReceived()值中:“+write”);
//做有价值的事
});
等待服务器启动(新的AdvertisementData)
{
LocalName=“DariusServer”,
ServiceUuids=新列表(){serverServiceGuid}
});
我正在使用此应用程序扫描我的广告数据:


我无法使用它发现我的应用程序。我不知道我做错了什么?我正在用一台真正的设备SM-T350平板电脑进行测试,我花了无数个小时才让这个插件正常工作,但运气不佳。但本机代码适用于任何其他有相同问题的人:

private async Task AndroidBluetooth()
{
    try
    {
            await Task.Delay(5000); // just to make sure bluetooth is ready to go, this probably isn't needed, but good for peace of mind during testing

            BluetoothLeAdvertiser advertiser = BluetoothAdapter.DefaultAdapter.BluetoothLeAdvertiser;

            var advertiseBuilder = new AdvertiseSettings.Builder();
            var parameters = advertiseBuilder.SetConnectable(true)
                                             .SetAdvertiseMode(AdvertiseMode.Balanced)
                                             //.SetTimeout(10000)
                                             .SetTxPowerLevel(AdvertiseTx.PowerHigh)
                                             .Build();

            AdvertiseData data = (new AdvertiseData.Builder()).AddServiceUuid(new ParcelUuid(Java.Util.UUID.FromString("your UUID here"))).Build();
            MyAdvertiseCallback callback = new MyAdvertiseCallback();
            advertiser.StartAdvertising(parameters, data, callback);
        }
        catch(Exception e)
        {

        }
    }

    public class MyAdvertiseCallback : AdvertiseCallback
    {
        public override void OnStartFailure([GeneratedEnum] AdvertiseFailure errorCode)
        {
            // put a break point here, in case something goes wrong, you can see why
            base.OnStartFailure(errorCode);
        }

        public override void OnStartSuccess(AdvertiseSettings settingsInEffect)
        {
            base.OnStartSuccess(settingsInEffect);
        }
    }
}
需要注意的是,如果我包含设备名称,它将不起作用,因为如果使用服务UUID(我认为最多31个字节),蓝牙传输将太大