Skip to content

Commit 766da53

Browse files
AdrianSoundyjosesimoesEllerbach
authored
Update Bluetooth samples for latest changes (#203)
* Update bluetooth samples * Remove bluetooth from category.txt * Update samples/Bluetooth/BluetoothLESample1/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample1/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample1/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample1/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/EnvironmentalSensorService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/EnvironmentalSensorService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/README.md Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample1/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/EnvironmentalSensorService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample1/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample2/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample2/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample2/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Program.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/BatteryService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/BatteryService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/BatteryService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/CurrentTimeService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/CurrentTimeService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/DeviceInformationService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/DeviceInformationService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/EnvironmentalSensorService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/DeviceInformationService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services/EnvironmentalSensorService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/BatteryService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/BatteryService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/BatteryService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/CurrentTimeService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/CurrentTimeService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/CurrentTimeService.cs Co-authored-by: José Simões <[email protected]> * Update samples/Bluetooth/BluetoothLESample3/Services2/DeviceInformationService.cs Co-authored-by: José Simões <[email protected]> * further changes from review * Update packages.config * Update packages.config * Adding ble category for better visibility in device listing Co-authored-by: José Simões <[email protected]> Co-authored-by: Laurent Ellerbach <[email protected]>
1 parent 9cb81a4 commit 766da53

32 files changed

+2054
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>e5353d20-0d64-4a0c-b0f8-65aa69dc22a7</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>BluetoothLESample1</RootNamespace>
16+
<AssemblyName>BluetoothLESample1</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib, Version=1.12.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
26+
<HintPath>packages\nanoFramework.CoreLibrary.1.12.0-preview.18\lib\mscorlib.dll</HintPath>
27+
<Private>True</Private>
28+
<SpecificVersion>True</SpecificVersion>
29+
</Reference>
30+
<Reference Include="nanoFramework.Device.Bluetooth, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
31+
<HintPath>packages\nanoFramework.Device.Bluetooth.1.0.0-preview.24\lib\nanoFramework.Device.Bluetooth.dll</HintPath>
32+
<Private>True</Private>
33+
<SpecificVersion>True</SpecificVersion>
34+
</Reference>
35+
<Reference Include="nanoFramework.Runtime.Events, Version=1.10.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
36+
<HintPath>packages\nanoFramework.Runtime.Events.1.10.0-preview.11\lib\nanoFramework.Runtime.Events.dll</HintPath>
37+
<Private>True</Private>
38+
<SpecificVersion>True</SpecificVersion>
39+
</Reference>
40+
<Reference Include="nanoFramework.Runtime.Native, Version=1.5.4.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
41+
<HintPath>packages\nanoFramework.Runtime.Native.1.5.4-preview.8\lib\nanoFramework.Runtime.Native.dll</HintPath>
42+
<Private>True</Private>
43+
<SpecificVersion>True</SpecificVersion>
44+
</Reference>
45+
<Reference Include="nanoFramework.System.Text, Version=1.1.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
46+
<HintPath>packages\nanoFramework.System.Text.1.1.3-preview.22\lib\nanoFramework.System.Text.dll</HintPath>
47+
<Private>True</Private>
48+
<SpecificVersion>True</SpecificVersion>
49+
</Reference>
50+
</ItemGroup>
51+
<ItemGroup>
52+
<None Include="packages.config" />
53+
</ItemGroup>
54+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
55+
<ProjectExtensions>
56+
<ProjectCapabilities>
57+
<ProjectConfigurationsDeclaredAsItems />
58+
</ProjectCapabilities>
59+
</ProjectExtensions>
60+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.32126.315
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "BluetoothLESample1", "BluetoothLESample1.nfproj", "{E5353D20-0D64-4A0C-B0F8-65AA69DC22A7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E5353D20-0D64-4A0C-B0F8-65AA69DC22A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E5353D20-0D64-4A0C-B0F8-65AA69DC22A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E5353D20-0D64-4A0C-B0F8-65AA69DC22A7}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{E5353D20-0D64-4A0C-B0F8-65AA69DC22A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{E5353D20-0D64-4A0C-B0F8-65AA69DC22A7}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{E5353D20-0D64-4A0C-B0F8-65AA69DC22A7}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {99DD0E07-50B5-4110-99C6-8FB438D388F8}
26+
EndGlobalSection
27+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Diagnostics;
8+
using System.Threading;
9+
10+
using nanoFramework.Device.Bluetooth;
11+
using nanoFramework.Device.Bluetooth.GenericAttributeProfile;
12+
13+
/// <summary>
14+
/// Bluetooth Sample 1 is a custom service which shows the use of:
15+
///
16+
/// - Read static value
17+
/// - Read a dynamic value using an event
18+
/// - Notifying clients of changed value
19+
///
20+
/// You will be able to connect to the service and read values or subscribe to Notified every 10 seconds.
21+
/// Suitable Phone apps: "LightBlue" or "nRF Connect"
22+
/// </summary>
23+
namespace BluetoothLESample1
24+
{
25+
public class Program
26+
{
27+
static GattLocalCharacteristic _readCharacteristic;
28+
static GattLocalCharacteristic _readWriteCharacteristic;
29+
30+
// Read/Write Characteristic value
31+
static byte _redValue = 128;
32+
static byte _greenValue = 128;
33+
static byte _blueValue = 128;
34+
35+
public static void Main()
36+
{
37+
Debug.WriteLine("Hello from Bluetooth Sample 1");
38+
39+
// Define some custom Uuids
40+
Guid serviceUuid = new Guid("A7EEDF2C-DA87-4CB5-A9C5-5151C78B0057");
41+
Guid readCharUuid = new Guid("A7EEDF2C-DA88-4CB5-A9C5-5151C78B0057");
42+
Guid readStaticCharUuid = new Guid("A7EEDF2C-DA89-4CB5-A9C5-5151C78B0057");
43+
Guid readWriteCharUuid = new Guid("A7EEDF2C-DA8A-4CB5-A9C5-5151C78B0057");
44+
45+
//The GattServiceProvider is used to create and advertise the primary service definition.
46+
//An extra device information service will be automatically created.
47+
GattServiceProviderResult result = GattServiceProvider.Create(serviceUuid);
48+
if (result.Error != BluetoothError.Success)
49+
{
50+
return;
51+
}
52+
53+
GattServiceProvider serviceProvider = result.ServiceProvider;
54+
55+
// Get created Primary service from provider
56+
GattLocalService service = serviceProvider.Service;
57+
58+
#region Static read characteristic
59+
// Now we add an characteristic to service
60+
// If the read value is not going to change then you can just use a Static value
61+
DataWriter sw = new DataWriter();
62+
sw.WriteString("This is Bluetooth sample 1");
63+
64+
GattLocalCharacteristicResult characteristicResult = service.CreateCharacteristic(readStaticCharUuid,
65+
new GattLocalCharacteristicParameters()
66+
{
67+
CharacteristicProperties = GattCharacteristicProperties.Read,
68+
UserDescription = "My Static Characteristic",
69+
StaticValue = sw.DetachBuffer()
70+
});
71+
;
72+
73+
if (characteristicResult.Error != BluetoothError.Success)
74+
{
75+
// An error occurred.
76+
return;
77+
}
78+
#endregion
79+
80+
#region Create Characteristic for dynamic Reads
81+
82+
// Add Read Characteristic for data that changes to service
83+
// We also want the connected client to be notified when value changes so we add the notify property
84+
characteristicResult = service.CreateCharacteristic(readCharUuid,
85+
new GattLocalCharacteristicParameters()
86+
{
87+
CharacteristicProperties = GattCharacteristicProperties.Read | GattCharacteristicProperties.Notify,
88+
UserDescription = "My Read Characteristic"
89+
});
90+
91+
if (characteristicResult.Error != BluetoothError.Success)
92+
{
93+
// An error occurred.
94+
return;
95+
}
96+
97+
// Get reference to our read Characteristic
98+
_readCharacteristic = characteristicResult.Characteristic;
99+
100+
// Every time the value is requested from a client this event is called
101+
_readCharacteristic.ReadRequested += ReadCharacteristic_ReadRequested;
102+
103+
// To notify client when time changes we are going to use a timer
104+
Timer notifyTimer = new Timer(NotifyCallBack, null, 10000, 10000);
105+
106+
#endregion
107+
108+
#region Create Characteristic for RGB read/write
109+
characteristicResult = service.CreateCharacteristic(readWriteCharUuid,
110+
new GattLocalCharacteristicParameters()
111+
{
112+
CharacteristicProperties = GattCharacteristicProperties.Read | GattCharacteristicProperties.Write,
113+
UserDescription = "My Read/Write Characteristic"
114+
});
115+
116+
if (characteristicResult.Error != BluetoothError.Success)
117+
{
118+
// An error occurred.
119+
return;
120+
}
121+
122+
// Get reference to our read Characteristic
123+
_readWriteCharacteristic = characteristicResult.Characteristic;
124+
125+
// Every time the value is requested from a client this event is called
126+
_readWriteCharacteristic.WriteRequested += _readWriteCharacteristic_WriteRequested;
127+
_readWriteCharacteristic.ReadRequested += _readWriteCharacteristic_ReadRequested;
128+
129+
#endregion
130+
131+
#region Start Advertising
132+
// Once all the Characteristics have been created you need to advertise the Service so
133+
// other devices can see it. Here we also say the device can be connected too and other
134+
// devices can see it.
135+
serviceProvider.StartAdvertising(new GattServiceProviderAdvertisingParameters()
136+
{
137+
DeviceName = "Sample 1 Device",
138+
IsConnectable = true,
139+
IsDiscoverable = true
140+
});
141+
#endregion
142+
143+
Thread.Sleep(Timeout.Infinite);
144+
}
145+
146+
/// <summary>
147+
/// Timer callback for notifying any connected clients who have subscribed to this
148+
/// characteristic of changed value.
149+
/// </summary>
150+
/// <param name="state">Not used</param>
151+
private static void NotifyCallBack(object state)
152+
{
153+
if (_readCharacteristic.SubscribedClients.Length > 0)
154+
{
155+
_readCharacteristic.NotifyValue(GetTimeBuffer());
156+
}
157+
}
158+
159+
/// <summary>
160+
/// Event handler for Read characteristic.
161+
/// </summary>
162+
/// <param name="sender">GattLocalCharacteristic object</param>
163+
/// <param name="ReadRequestEventArgs"></param>
164+
private static void ReadCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs ReadRequestEventArgs)
165+
{
166+
GattReadRequest request = ReadRequestEventArgs.GetRequest();
167+
168+
// Get Buffer with hour/minute/second
169+
request.RespondWithValue(GetTimeBuffer());
170+
}
171+
172+
// Separate out the Creation of Time buffer so it can be used by read characteristic and Notify
173+
private static Buffer GetTimeBuffer()
174+
{
175+
// Create DataWriter and write the data into buffer
176+
// Write Hour/minute/second of current time
177+
DateTime dt = DateTime.UtcNow;
178+
179+
// Write data in a Buffer object using DataWriter
180+
DataWriter dw = new DataWriter();
181+
dw.WriteByte((Byte)dt.Hour);
182+
dw.WriteByte((Byte)dt.Minute);
183+
dw.WriteByte((Byte)dt.Second);
184+
185+
// Detach Buffer object from DataWriter
186+
return dw.DetachBuffer();
187+
}
188+
189+
/// <summary>
190+
/// Read event handler for Read/Write characteristic.
191+
/// </summary>
192+
/// <param name="sender"></param>
193+
/// <param name="ReadRequestEventArgs"></param>
194+
private static void _readWriteCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs ReadRequestEventArgs)
195+
{
196+
GattReadRequest request = ReadRequestEventArgs.GetRequest();
197+
198+
DataWriter dw = new DataWriter();
199+
dw.WriteByte((Byte)_redValue);
200+
dw.WriteByte((Byte)_greenValue);
201+
dw.WriteByte((Byte)_blueValue);
202+
203+
request.RespondWithValue(dw.DetachBuffer());
204+
205+
Debug.WriteLine($"RGB read");
206+
}
207+
208+
/// <summary>
209+
/// Write handler for Read/Write characteristic.
210+
/// </summary>
211+
/// <param name="sender"></param>
212+
/// <param name="WriteRequestEventArgs"></param>
213+
private static void _readWriteCharacteristic_WriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs WriteRequestEventArgs)
214+
{
215+
GattWriteRequest request = WriteRequestEventArgs.GetRequest();
216+
217+
// Check expected data length, we are expecting 3 bytes
218+
if (request.Value.Length != 3)
219+
{
220+
request.RespondWithProtocolError((byte)BluetoothError.NotSupported);
221+
return;
222+
}
223+
224+
// Unpack data from buffer
225+
DataReader rdr = DataReader.FromBuffer(request.Value);
226+
_redValue = rdr.ReadByte();
227+
_greenValue = rdr.ReadByte();
228+
_blueValue = rdr.ReadByte();
229+
230+
// Respond if Write requires response
231+
if (request.Option == GattWriteOption.WriteWithResponse)
232+
{
233+
request.Respond();
234+
}
235+
236+
// Print new values, better to set a RGB led
237+
Debug.WriteLine($"Received RGB={_redValue}/{_greenValue}/{_blueValue}");
238+
}
239+
}
240+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Bluetooth Low energy: read static and dynamic values, notification, read/write value
2+
3+
This shows how to create a custom service which shows the use of:
4+
5+
| Custom service | Behavior |
6+
| --- | --- |
7+
| Read static value (value that doesn't change) | Value text |
8+
| Read a dynamic value using an event handler. | value 3 bytes (Hour/Minute/Seconds) |
9+
| Notifying clients of a changed value | Notify time every 60 seconds or when date updated. |
10+
| Read and Write a value | Read/Write 3 bytes RGB |
11+
12+
You will be able to connect to the service and read values or subscribe to be Notified ever 60 seconds.
13+
Suitable Phone apps: "LightBlue" or "nRF Connect"
14+
15+
See [main Bluetooth sample's readme](../README.md) for more information.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ble
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.12.0-preview.18" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.Device.Bluetooth" version="1.0.0-preview.24" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.Runtime.Events" version="1.10.0-preview.11" targetFramework="netnanoframework10" />
6+
<package id="nanoFramework.Runtime.Native" version="1.5.4-preview.8" targetFramework="netnanoframework10" />
7+
<package id="nanoFramework.System.Text" version="1.1.3-preview.22" targetFramework="netnanoframework10" />
8+
</packages>

0 commit comments

Comments
 (0)