Добавьте bluetooth-lowenergy сервисы и доступные для записи характеристики к смартфону Xamarin - PullRequest
0 голосов
/ 30 мая 2018

Я занимаюсь разработкой мобильного приложения, которое использует bluetooth-lowenergy, мне нужно создать службы и характеристики в приложении

Спасибо

1 Ответ

0 голосов
/ 31 мая 2018

это после двух дней поиска, и прочитав эту страницу BLE Android я решил свою проблему

здесь класс, используемый для создания сервисов

using System;
using Android.App;
using Android.Bluetooth;
using Android.Bluetooth.LE;
using Android.OS;
using Java.Util;
using SitBle.Droid;
using SitBle.Interfaces;
using Exception = System.Exception;

[assembly: Xamarin.Forms.Dependency(typeof(Advertiser))]
namespace SitBle.Droid
{
public class Advertiser : IAdvertiser
{
    private BluetoothAdvertiseCallback _advertiseCallback;
    private readonly UUID _serviceUuid = UUID.FromString("795090c7-420d-4048-a24e-18e60180e23c");
    private readonly UUID _characteristicCounterUuid = UUID.FromString("31517c58-66bf-470c-b662-e352a6c80cba");
    private readonly UUID _characteristicInteractorUuid = UUID.FromString("0b89d2d4-0ea6-4141-86bb-0c5fb91ab14a");
    private readonly UUID _descriptorConfigUuid = UUID.FromString("00002902-0000-1000-8000-00805f9b34fb");


    public void Advertise()
    {
        try
        {
            _advertiseCallback = new BluetoothAdvertiseCallback();
            var androidBluetoothGattServerCallback = new AndroidBluetoothGattServerCallback();
            var adapter = BluetoothAdapter.DefaultAdapter;
            var advertiseSettingBuilder = new AdvertiseSettings.Builder()
                .SetAdvertiseMode(AdvertiseMode.Balanced).SetConnectable(true).SetTimeout(0)
                .SetTxPowerLevel(AdvertiseTx.PowerMedium);
            var advertiseSetting = advertiseSettingBuilder.Build();
            var adverisingDataBuilder = new AdvertiseData.Builder().SetIncludeDeviceName(false).AddServiceUuid(new ParcelUuid(_serviceUuid));
            adapter.BluetoothLeAdvertiser.StartAdvertising(advertiseSetting, adverisingDataBuilder.Build(), _advertiseCallback);

            var appContext = Application.Context;
            var mBluetoothManager = (BluetoothManager)appContext.GetSystemService("bluetooth");

            var mGattServer = mBluetoothManager.OpenGattServer(appContext, androidBluetoothGattServerCallback);
            mGattServer.AddService(CreateService());

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    private BluetoothGattService CreateService()
    {
        BluetoothGattService service = new BluetoothGattService(_serviceUuid, GattServiceType.Primary);

        // Counter characteristic (read-only, supports subscriptions)
        BluetoothGattCharacteristic counter = new BluetoothGattCharacteristic(_characteristicCounterUuid,GattProperty.Read|GattProperty.Notify,GattPermission.Read);

        BluetoothGattDescriptor counterConfig = new BluetoothGattDescriptor(_descriptorConfigUuid, GattDescriptorPermission.Read|GattDescriptorPermission.Write);
        counter.AddDescriptor(counterConfig);

        // Interactor characteristic
        BluetoothGattCharacteristic interactor = new BluetoothGattCharacteristic(_characteristicInteractorUuid, GattProperty.Read|GattProperty.Write|GattProperty.Notify, GattPermission.Write);
        service.AddCharacteristic(counter);
        service.AddCharacteristic(interactor);
        return service;
    }
}

public class BluetoothAdvertiseCallback : AdvertiseCallback
{

    public BluetoothAdvertiseCallback()
    {

    }

    public override void OnStartSuccess(AdvertiseSettings settingsInEffect)
    {
        Console.WriteLine("Success");
    }

    public override void OnStartFailure(AdvertiseFailure errorCode)
    {
        Console.WriteLine("Fail");
    }

}


public class AndroidBluetoothGattServerCallback : BluetoothGattServerCallback
{

    public AndroidBluetoothGattServerCallback()
    {

    }
}
}
...