Skip to content

Commit c0745dc

Browse files
AdrianSoundyjosesimoesEllerbach
authored
Update to allow adding of extra services (#19)
* Update to allow add extra services * Update nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattServiceProvider.cs Co-authored-by: José Simões <[email protected]> * Update nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattServiceProvider.cs Co-authored-by: Laurent Ellerbach <[email protected]> * Update versions Co-authored-by: José Simões <[email protected]> Co-authored-by: Laurent Ellerbach <[email protected]>
1 parent b3b9457 commit c0745dc

File tree

10 files changed

+180
-16
lines changed

10 files changed

+180
-16
lines changed

nanoFramework.Device.Bluetooth.nuspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ This package requires a target with nanoFramework.Device.Bluetooth v$nativeVersi
2020
<tags>nanoFramework C# csharp netmf netnf nanoFramework.Device.Bluetooth</tags>
2121
<dependencies>
2222
<dependency id="nanoFramework.CoreLibrary" version="1.12.0-preview.18" />
23-
<dependency id="nanoFramework.Runtime.Events" version="1.10.0-preview.11" />
23+
<dependency id="nanoFramework.Runtime.Native" version="1.5.4-preview.8" />
24+
<dependency id="nanoFramework.Runtime.Events" version="1.10.0-preview.11" />
2425
<dependency id="nanoFramework.System.Text" version="1.1.3-preview.22" />
2526
</dependencies>
2627
</metadata>

nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattCharacteristicUuids.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ namespace nanoFramework.Device.Bluetooth.GenericAttributeProfile
1414
/// </summary>
1515
public static class GattCharacteristicUuids
1616
{
17-
/// <summary>
18-
/// Gets the Bluetooth SIG-defined Heart Rate Measurement Characteristic UUID (0x2A37)
19-
/// </summary>
20-
public static Guid HeartRateMeasurement { get => Utilities.CreateUuidFromShortCode(0x2A37); }
21-
2217
/// <summary>
2318
/// Gets the Bluetooth SIG-defined Battery Level Characteristic UUID (0x2A19).
19+
/// Percentage 0% to 100%, byte
2420
/// </summary>
2521
public static Guid BatteryLevel { get => Utilities.CreateUuidFromShortCode(0x2A19); }
2622

@@ -69,6 +65,17 @@ public static class GattCharacteristicUuids
6965
/// </summary>
7066
public static Guid HeartRateControlPoint { get => Utilities.CreateUuidFromShortCode(0x2A39); }
7167

68+
/// <summary>
69+
/// Gets the Bluetooth SIG-defined Heart Rate Measurement Characteristic UUID (0x2A37)
70+
/// </summary>
71+
public static Guid HeartRateMeasurement { get => Utilities.CreateUuidFromShortCode(0x2A37); }
72+
73+
/// <summary>
74+
/// Gets the Bluetooth SIG-defined Humidity Characteristic UUID (0x2A6F)
75+
/// Uint16, Percentage 0-100 expressed in 0.01 units, 50%=5000
76+
/// </summary>
77+
public static Guid Humidity { get => Utilities.CreateUuidFromShortCode(0x2A6F); }
78+
7279
/// <summary>
7380
/// Gets the Bluetooth SIG-defined Intermediate Cuff Pressure Characteristic UUID (0x2A36).
7481
/// </summary>
@@ -84,6 +91,18 @@ public static class GattCharacteristicUuids
8491
/// </summary>
8592
public static Guid MeasurementInterval { get => Utilities.CreateUuidFromShortCode(0x2A21); }
8693

94+
/// <summary>
95+
/// Gets the Bluetooth SIG-defined Pressure Characteristic UUID (0x2A6D).
96+
/// Uint32, Unit is Pascal with resolution of 0.1 Pa
97+
/// </summary>
98+
public static Guid Pressure { get => Utilities.CreateUuidFromShortCode(0x2A6D); }
99+
100+
/// <summary>
101+
/// Gets the Bluetooth SIG-defined Rainfall Characteristic UUID (0x2A78).
102+
/// Uint16, Unit is Meters with resolution of 1mm
103+
/// </summary>
104+
public static Guid Rainfall { get => Utilities.CreateUuidFromShortCode(0x2A78); }
105+
87106
/// <summary>
88107
/// Gets the Bluetooth SIG-defined Record Access Control Point Characteristic UUID (0x2A52).
89108
/// </summary>
@@ -109,6 +128,12 @@ public static class GattCharacteristicUuids
109128
/// </summary>
110129
public static Guid SensorLocation { get => Utilities.CreateUuidFromShortCode(0x2A5D); }
111130

131+
/// <summary>
132+
/// Gets the Bluetooth SIG-defined Temperature Characteristic UUID (0x2A6E).
133+
/// Int16, -273.15 to 327.67 Celsius. Resolution 0.01, 89.5c = 8950
134+
/// </summary>
135+
public static Guid Temperature { get => Utilities.CreateUuidFromShortCode(0x2a6e); }
136+
112137
/// <summary>
113138
/// Gets the Bluetooth SIG-defined Temperature Measurement Characteristic UUID (0x2a1c).
114139
/// </summary>

nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattDescriptorUuid.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,30 @@ public static class GattDescriptorUuids
4343
/// Gets the Bluetooth SIG-defined Server Characteristic Configuration Descriptor UUID.
4444
/// </summary>
4545
public static Guid ServerCharacteristicConfiguration { get => Utilities.CreateUuidFromShortCode((ushort)Utilities.GattNativeDescriptorUuid.ServerCharacteristicConfiguration); }
46+
47+
/// <summary>
48+
/// Gets the Bluetooth SIG-defined Value trigger settings Descriptor UUID.
49+
/// </summary>
50+
public static Guid ValueTriggerSetting { get => Utilities.CreateUuidFromShortCode((ushort)Utilities.GattNativeDescriptorUuid.ValueTriggerSetting); }
51+
52+
/// <summary>
53+
/// Gets the Bluetooth SIG-defined Environmental Sensing Configuration Descriptor UUID.
54+
/// </summary>
55+
public static Guid EssConfiguration { get => Utilities.CreateUuidFromShortCode((ushort)Utilities.GattNativeDescriptorUuid.EssConfiguration); }
56+
57+
/// <summary>
58+
/// Gets the Bluetooth SIG-defined Environmental Sensing Measurement Descriptor UUID.
59+
/// </summary>
60+
public static Guid EssMeasurement { get => Utilities.CreateUuidFromShortCode((ushort)Utilities.GattNativeDescriptorUuid.EssMeasurement); }
61+
62+
/// <summary>
63+
/// Gets the Bluetooth SIG-defined Environmental Sensing Trigger Setting Descriptor UUID.
64+
/// </summary>
65+
public static Guid EssTriggerSetting { get => Utilities.CreateUuidFromShortCode((ushort)Utilities.GattNativeDescriptorUuid.EssTriggerSetting); }
66+
67+
/// <summary>
68+
/// Gets the Bluetooth SIG-defined Time Trigger Setting Descriptor UUID.
69+
/// </summary>
70+
public static Guid TimeTriggerSetting { get => Utilities.CreateUuidFromShortCode((ushort)Utilities.GattNativeDescriptorUuid.TimeTriggerSetting); }
4671
}
4772
}

nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattLocalCharacteristic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public GattClientNotificationResult NotifyValue(Buffer value, GattSubscribedClie
205205
/// <summary>
206206
/// Gets a vector list of all the descriptors for this local characteristic.
207207
/// </summary>
208-
public GattLocalDescriptor[] Descriptors { get => (GattLocalDescriptor[])_descriptors.ToArray(); }
208+
public GattLocalDescriptor[] Descriptors { get => (GattLocalDescriptor[])_descriptors.ToArray(typeof(GattLocalDescriptor)); }
209209

210210
/// <summary>
211211
/// Gets the presentation formats for this local characteristic.

nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattLocalService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ internal GattLocalService(Guid serviceUuid)
2020
{
2121
_serviceUuid = serviceUuid.ToByteArray();
2222
_characteristics = new ArrayList();
23-
_characteristics.Add(new GattLocalCharacteristic(serviceUuid, new GattLocalCharacteristicParameters()));
2423
}
2524

2625
/// <summary>

nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattServiceProvider.cs

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66
using System;
77
using System.Text;
8+
using System.Collections;
89
using System.Runtime.CompilerServices;
10+
using nanoFramework.Runtime.Native;
911

1012
namespace nanoFramework.Device.Bluetooth.GenericAttributeProfile
1113
{
1214
/// <summary>
13-
/// This class is used to advertise a GATT service.
15+
/// This class is used to advertise GATT services.
1416
/// </summary>
1517
public sealed class GattServiceProvider
1618
{
17-
private readonly GattLocalService _service;
19+
private readonly ArrayList _services;
1820

1921
GattServiceProviderAdvertisementStatus _status = GattServiceProviderAdvertisementStatus.Created;
2022

@@ -35,11 +37,48 @@ public sealed class GattServiceProvider
3537

3638
internal GattServiceProvider(Guid serviceUuid)
3739
{
38-
_service = new GattLocalService(serviceUuid);
40+
_services = new ArrayList();
41+
42+
// Add primary
43+
AddService(serviceUuid);
44+
45+
// Add default Device Information service
46+
AddDeviceInformationService();
3947

4048
NativeInitService();
4149
}
4250

51+
/// <summary>
52+
/// Add default Device Information Service
53+
/// </summary>
54+
private void AddDeviceInformationService()
55+
{
56+
// Add and initialised Device Information defaults
57+
GattLocalService dinfService = AddService(GattServiceUuids.DeviceInformation);
58+
59+
// ManufacturerNameString Characteristic (0x2A29)
60+
DataWriter manufacturerName = new DataWriter();
61+
manufacturerName.WriteString("nanoFramework");
62+
63+
dinfService.CreateCharacteristic(GattCharacteristicUuids.ManufacturerNameString,
64+
new GattLocalCharacteristicParameters()
65+
{
66+
StaticValue = manufacturerName.DetachBuffer(),
67+
CharacteristicProperties = GattCharacteristicProperties.Read
68+
});
69+
70+
// ModelNumberString Characteristic (0x2A24)
71+
DataWriter modelNumber = new DataWriter();
72+
modelNumber.WriteString(SystemInfo.Platform);
73+
74+
dinfService.CreateCharacteristic(GattCharacteristicUuids.ModelNumberString,
75+
new GattLocalCharacteristicParameters()
76+
{
77+
StaticValue = modelNumber.DetachBuffer(),
78+
CharacteristicProperties = GattCharacteristicProperties.Read
79+
});
80+
}
81+
4382
/// <summary>
4483
/// Start advertising the GATT service.
4584
/// </summary>
@@ -96,10 +135,46 @@ public static GattServiceProviderResult Create(Guid serviceUuid)
96135
public GattServiceProviderAdvertisementStatus AdvertisementStatus { get => _status; }
97136

98137
/// <summary>
99-
/// Gets the GATT service.
138+
/// Gets the GATT primary service.
139+
/// </summary>
140+
/// <returns>The primary service.</returns>
141+
public GattLocalService Service { get => Services[0]; }
142+
143+
/// <summary>
144+
/// Get an array of all associated services for this service provider.
100145
/// </summary>
101-
/// <returns>The GATT service.</returns>
102-
public GattLocalService Service { get => _service; }
146+
/// <remarks>
147+
/// The primary service will be index 0 followed by the Device Information at index 1.
148+
/// Any other Services added to provider will follow these in the order they were created.
149+
/// </remarks>
150+
public GattLocalService[] Services { get { return (GattLocalService[])_services.ToArray(typeof(GattLocalService)); } }
151+
152+
/// <summary>
153+
/// Creates a new service or replaces an existing service.
154+
/// Created service is added to this service provider.
155+
/// </summary>
156+
/// <param name="serviceUuid">Uuid for the service to be created/replaced.</param>
157+
/// <returns>
158+
/// Returns the created service.
159+
/// </returns>
160+
public GattLocalService AddService(Guid serviceUuid)
161+
{
162+
for (int index = 0; index < _services.Count; index++)
163+
{
164+
if (((GattLocalService)_services[index]).Uuid.Equals(serviceUuid))
165+
{
166+
// Replace existing service on index
167+
GattLocalService replacementService = new GattLocalService(serviceUuid);
168+
_services[index] = replacementService;
169+
return replacementService;
170+
}
171+
}
172+
173+
// Add new service
174+
GattLocalService newService = new GattLocalService(serviceUuid);
175+
_services.Add(newService);
176+
return newService;
177+
}
103178

104179
#region external calls to native implementations
105180

nanoFramework.Device.Bluetooth/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
////////////////////////////////////////////////////////////////
1919
// update this whenever the native assembly signature changes //
20-
[assembly: AssemblyNativeVersion("100.0.0.1")]
20+
[assembly: AssemblyNativeVersion("100.0.0.2")]
2121
////////////////////////////////////////////////////////////////
2222

2323

nanoFramework.Device.Bluetooth/Utilities.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,37 @@ public enum GattNativeDescriptorUuid : ushort
6060
/// <summary>
6161
/// ReportReference
6262
/// </summary>
63-
ReportReference = 0x2908
63+
ReportReference = 0x2908,
64+
65+
/// <summary>
66+
/// Number of Digitals
67+
/// </summary>
68+
NumberDigitals = 0x2909,
69+
70+
/// <summary>
71+
/// Value trigger settings
72+
/// </summary>
73+
ValueTriggerSetting = 0x290A,
74+
75+
/// <summary>
76+
/// Environmental Sensing Configuration
77+
/// </summary>
78+
EssConfiguration = 0x290B,
79+
80+
/// <summary>
81+
/// Environmental Sensing Measurement
82+
/// </summary>
83+
EssMeasurement = 0x290C,
84+
85+
/// <summary>
86+
/// Environmental Sensing Trigger Setting
87+
/// </summary>
88+
EssTriggerSetting = 0x290D,
89+
90+
/// <summary>
91+
/// Time Trigger Setting
92+
/// </summary>
93+
TimeTriggerSetting = 0x290E
6494
}
6595

6696
/// <summary>

nanoFramework.Device.Bluetooth/nanoFramework.Device.Bluetooth.nfproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,22 @@
8686
<Reference Include="mscorlib, Version=1.12.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
8787
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0-preview.18\lib\mscorlib.dll</HintPath>
8888
<Private>True</Private>
89+
<SpecificVersion>True</SpecificVersion>
8990
</Reference>
9091
<Reference Include="nanoFramework.Runtime.Events, Version=1.10.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
9192
<HintPath>..\packages\nanoFramework.Runtime.Events.1.10.0-preview.11\lib\nanoFramework.Runtime.Events.dll</HintPath>
9293
<Private>True</Private>
94+
<SpecificVersion>True</SpecificVersion>
95+
</Reference>
96+
<Reference Include="nanoFramework.Runtime.Native, Version=1.5.4.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
97+
<HintPath>..\packages\nanoFramework.Runtime.Native.1.5.4-preview.8\lib\nanoFramework.Runtime.Native.dll</HintPath>
98+
<Private>True</Private>
99+
<SpecificVersion>True</SpecificVersion>
93100
</Reference>
94101
<Reference Include="nanoFramework.System.Text, Version=1.1.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
95102
<HintPath>..\packages\nanoFramework.System.Text.1.1.3-preview.22\lib\nanoFramework.System.Text.dll</HintPath>
96103
<Private>True</Private>
104+
<SpecificVersion>True</SpecificVersion>
97105
</Reference>
98106
</ItemGroup>
99107
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />

nanoFramework.Device.Bluetooth/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.12.0-preview.18" targetFramework="netnanoframework10" />
44
<package id="nanoFramework.Runtime.Events" version="1.10.0-preview.11" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.Runtime.Native" version="1.5.4-preview.8" targetFramework="netnanoframework10" />
56
<package id="nanoFramework.System.Text" version="1.1.3-preview.22" targetFramework="netnanoframework10" />
67
<package id="Nerdbank.GitVersioning" version="3.4.231" developmentDependency="true" targetFramework="netnanoframework10" />
78
</packages>

0 commit comments

Comments
 (0)