Skip to content

Commit b33e0b9

Browse files
authored
Updated samples for latest Bluetooth including pairing (#285)
1 parent e15b7d7 commit b33e0b9

37 files changed

+694
-89
lines changed

samples/Bluetooth/BluetoothLESample1/BluetoothLESample1.nfproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
</ItemGroup>
2424
<ItemGroup>
2525
<None Include="packages.config" />
26+
<None Include="README.md" />
2627
</ItemGroup>
2728
<ItemGroup>
2829
<Reference Include="mscorlib, Version=1.14.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
2930
<HintPath>packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
3031
<Private>True</Private>
3132
</Reference>
32-
<Reference Include="nanoFramework.Device.Bluetooth, Version=1.1.35.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
33-
<HintPath>packages\nanoFramework.Device.Bluetooth.1.1.35\lib\nanoFramework.Device.Bluetooth.dll</HintPath>
34-
<Private>True</Private>
33+
<Reference Include="nanoFramework.Device.Bluetooth">
34+
<HintPath>packages\nanoFramework.Device.Bluetooth.1.1.37\lib\nanoFramework.Device.Bluetooth.dll</HintPath>
3535
</Reference>
3636
<Reference Include="nanoFramework.Runtime.Events, Version=1.11.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
3737
<HintPath>packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>

samples/Bluetooth/BluetoothLESample1/Program.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public static void Main()
3636
{
3737
Debug.WriteLine("Hello from Bluetooth Sample 1");
3838

39+
// BluetoothLEServer is a singleton object so gets its instance. The Object is created when you first access it
40+
// and can be disposed to free up memory.
41+
BluetoothLEServer server = BluetoothLEServer.Instance;
42+
43+
// Give device a name
44+
server.DeviceName = "Sample1";
45+
3946
// Define some custom Uuids
4047
Guid serviceUuid = new Guid("A7EEDF2C-DA87-4CB5-A9C5-5151C78B0057");
4148
Guid readCharUuid = new Guid("A7EEDF2C-DA88-4CB5-A9C5-5151C78B0057");
@@ -134,7 +141,6 @@ public static void Main()
134141
// devices can see it.
135142
serviceProvider.StartAdvertising(new GattServiceProviderAdvertisingParameters()
136143
{
137-
DeviceName = "Sample1",
138144
IsConnectable = true,
139145
IsDiscoverable = true
140146
});

samples/Bluetooth/BluetoothLESample1/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 🌶️🌶️ - Bluetooth Low energy: read static and dynamic values, notification, read/write value
22

3-
This shows how to create a custom service which shows the use of:
3+
This shows how to create a Bluetooth Server with a custom service.
4+
5+
Shows the use of:-
46

57
| Custom service | Behavior |
68
| --- | --- |
@@ -12,4 +14,4 @@ This shows how to create a custom service which shows the use of:
1214
You will be able to connect to the service and read values or subscribe to be Notified ever 60 seconds.
1315
Suitable Phone apps: "LightBlue" or "nRF Connect"
1416

15-
See [main Bluetooth sample's readme](../README.md) for more information.
17+
See [main Bluetooth sample's readme](../README.md) for more information.

samples/Bluetooth/BluetoothLESample1/packages.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.14.2" targetFramework="netnanoframework10" />
4-
<package id="nanoFramework.Device.Bluetooth" version="1.1.35" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.Device.Bluetooth" version="1.1.37" targetFramework="netnano1.0" />
55
<package id="nanoFramework.Runtime.Events" version="1.11.6" targetFramework="netnanoframework10" />
66
<package id="nanoFramework.Runtime.Native" version="1.6.6" targetFramework="netnanoframework10" />
77
<package id="nanoFramework.System.Collections" version="1.5.18" targetFramework="netnanoframework10" />

samples/Bluetooth/BluetoothLESample2/BluetoothLESample2.nfproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
</ItemGroup>
2424
<ItemGroup>
2525
<None Include="packages.config" />
26+
<None Include="README.md" />
2627
</ItemGroup>
2728
<ItemGroup>
2829
<Reference Include="mscorlib, Version=1.14.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
2930
<HintPath>packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
3031
<Private>True</Private>
3132
</Reference>
32-
<Reference Include="nanoFramework.Device.Bluetooth, Version=1.1.35.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
33-
<HintPath>packages\nanoFramework.Device.Bluetooth.1.1.35\lib\nanoFramework.Device.Bluetooth.dll</HintPath>
34-
<Private>True</Private>
33+
<Reference Include="nanoFramework.Device.Bluetooth">
34+
<HintPath>packages\nanoFramework.Device.Bluetooth.1.1.37\lib\nanoFramework.Device.Bluetooth.dll</HintPath>
3535
</Reference>
3636
<Reference Include="nanoFramework.Runtime.Events, Version=1.11.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
3737
<HintPath>packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>

samples/Bluetooth/BluetoothLESample2/Program.cs

+116-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//
55

66
using System;
7-
using System.Diagnostics;
87
using System.Threading;
98

109
using nanoFramework.Device.Bluetooth;
@@ -14,30 +13,65 @@
1413
/// Bluetooth Sample 2 is a custom service which shows the use of:
1514
///
1615
/// - Adding security to characteristics
17-
/// - 1st characteristic you can read without pairing but will need to be paired to write
18-
/// - 2nd characteristic you can write without pairing but will need to be paired to Read
19-
/// - Both characteristic read/write same value.
16+
/// - 1st characteristic you can read & write without pairing
17+
/// - 2nd characteristic you can read & write but will required device to paired so encryption is enabled. If you try to access this characteristic
18+
/// a pairing will be forced. You can use a just works type of pairing for this.
19+
/// - 3nd characteristic you can read & write but will required device to paired and authenticated. If you try to access this characteristic
20+
/// a pairing will be forced. A pin number of 654321 will need to be entered to pair succesfully.
21+
///
22+
/// - All characteristics read/write same value.
2023
///
2124
/// You will be able to connect to the service and read values or subscribe to Notified ever 10 seconds.
22-
/// Suitable Phone apps: "LightBlue" or "nRF Connect"
25+
/// Suitable Phone apps: "LightBlue" or "nRF Connect". If using a smaller then six digits for pin then use leading zeroes in these apps.
2326
/// </summary>
2427
namespace BluetoothLESample2
2528
{
2629
public class Program
2730
{
2831
static GattLocalCharacteristic _readWriteCharacteristic1;
2932
static GattLocalCharacteristic _readWriteCharacteristic2;
33+
static GattLocalCharacteristic _readWriteCharacteristic3;
3034

3135
// value used to read/write
32-
static Int32 _value;
36+
static Int32 _value = 57;
37+
38+
// Default pin
39+
const int PASSKEY = 654321;
3340

3441
public static void Main()
3542
{
36-
Debug.WriteLine("Hello from Bluetooth Sample 2");
43+
Console.WriteLine();
44+
Console.WriteLine("Hello from Bluetooth Sample 2");
3745

3846
Guid serviceUuid = new Guid("A7EEDF2C-DA8C-4CB5-A9C5-5151C78B0057");
39-
Guid writeCharUuid1 = new Guid("A7EEDF2C-DA8D-4CB5-A9C5-5151C78B0057");
40-
Guid writeCharUuid2 = new Guid("A7EEDF2C-DA8E-4CB5-A9C5-5151C78B0057");
47+
Guid plain_CharUuid1 = new Guid("A7EEDF2C-DA8D-4CB5-A9C5-5151C78B0057");
48+
Guid encrypt_CharUuid2 = new Guid("A7EEDF2C-DA8E-4CB5-A9C5-5151C78B0057");
49+
Guid auth_CharUuid3 = new Guid("A7EEDF2C-DA8F-4CB5-A9C5-5151C78B0057");
50+
51+
// BluetoothLEServer is a singleton object so gets its instance. The Object is created when you first access it
52+
// and can be disposed to free up memory.
53+
BluetoothLEServer server = BluetoothLEServer.Instance;
54+
55+
// Give device a name
56+
server.DeviceName = "Sample2";
57+
58+
// Set up an event handler for handling pairing requests
59+
server.Pairing.PairingRequested += Pairing_PairingRequested;
60+
server.Pairing.PairingComplete += Pairing_PairingComplete;
61+
62+
// Set up event for a session status change, client connects/disconnects
63+
server.Session.SessionStatusChanged += Session_SessionStatusChanged;
64+
65+
// The IOCapabilities define the input /output capabilities of the device and the type of pairings that is available.
66+
// See Bluetooth pairing matrix for more information.
67+
// With NoInputNoOutput on both ends then only "Just works" pairing is available and only first 2 characteristics will be accesable.
68+
server.Pairing.IOCapabilities = DevicePairingIOCapabilities.NoInputNoOutput;
69+
70+
// By making it a display we force an Authenication, Remove following comment to try this out
71+
server.Pairing.IOCapabilities = DevicePairingIOCapabilities.DisplayOnly;
72+
73+
// Start the Bluetooth server.
74+
server.Start();
4175

4276
//The GattServiceProvider is used to create and advertise the primary service definition.
4377
//An extra device information service will be automatically created.
@@ -55,13 +89,13 @@ public static void Main()
5589
#region Characteristic 1
5690
// Add Read Characteristic for data that changes to service
5791
// We also want the connected client to be notified when value changes so we add the notify property
58-
GattLocalCharacteristicResult characteristicResult = service.CreateCharacteristic(writeCharUuid1,
92+
GattLocalCharacteristicResult characteristicResult = service.CreateCharacteristic(plain_CharUuid1,
5993
new GattLocalCharacteristicParameters()
6094
{
6195
CharacteristicProperties = GattCharacteristicProperties.Read | GattCharacteristicProperties.Write,
6296
UserDescription = "My Read/Write Characteristic 1",
6397
ReadProtectionLevel = GattProtectionLevel.Plain,
64-
WriteProtectionLevel = GattProtectionLevel.EncryptionRequired
98+
WriteProtectionLevel = GattProtectionLevel.Plain
6599
});
66100
;
67101

@@ -74,23 +108,22 @@ public static void Main()
74108
// Get reference to our read Characteristic
75109
_readWriteCharacteristic1 = characteristicResult.Characteristic;
76110

77-
// Set up event handlers for read/write
111+
// Set up event handlers for read/write #1
78112
_readWriteCharacteristic1.WriteRequested += _writeCharacteristic_WriteRequested;
79113
_readWriteCharacteristic1.ReadRequested += _readWriteCharacteristic_ReadRequested;
80114
#endregion
81115

82-
#region Characteristic 1
116+
#region Characteristic 2 - Encryption required
83117
// Add Read Characteristic for data that changes to service
84118
// We also want the connected client to be notified when value changes so we add the notify property
85-
characteristicResult = service.CreateCharacteristic(writeCharUuid2,
119+
characteristicResult = service.CreateCharacteristic(encrypt_CharUuid2,
86120
new GattLocalCharacteristicParameters()
87121
{
88122
CharacteristicProperties = GattCharacteristicProperties.Read | GattCharacteristicProperties.Write,
89123
UserDescription = "My Read/Write Characteristic 2",
90124
ReadProtectionLevel = GattProtectionLevel.EncryptionRequired,
91-
WriteProtectionLevel = GattProtectionLevel.Plain
125+
WriteProtectionLevel = GattProtectionLevel.EncryptionRequired
92126
});
93-
;
94127

95128
if (characteristicResult.Error != BluetoothError.Success)
96129
{
@@ -101,26 +134,89 @@ public static void Main()
101134
// Get reference to our read Characteristic
102135
_readWriteCharacteristic2 = characteristicResult.Characteristic;
103136

104-
// Set up event handlers for read/write
137+
// Set up event handlers for read/write #2
105138
_readWriteCharacteristic2.WriteRequested += _writeCharacteristic_WriteRequested;
106139
_readWriteCharacteristic2.ReadRequested += _readWriteCharacteristic_ReadRequested;
107140
#endregion
108141

142+
#region Characteristic 3 - Authentication & Encryption required
143+
// Add Read Characteristic for data that changes to service
144+
// We also want the connected client to be notified when value changes so we add the notify property
145+
characteristicResult = service.CreateCharacteristic(auth_CharUuid3,
146+
new GattLocalCharacteristicParameters()
147+
{
148+
CharacteristicProperties = GattCharacteristicProperties.Read | GattCharacteristicProperties.Write,
149+
UserDescription = "My Read/Write Characteristic 3",
150+
ReadProtectionLevel = GattProtectionLevel.EncryptionAndAuthenticationRequired,
151+
WriteProtectionLevel = GattProtectionLevel.EncryptionAndAuthenticationRequired
152+
});
153+
154+
if (characteristicResult.Error != BluetoothError.Success)
155+
{
156+
// An error occurred.
157+
return;
158+
}
159+
160+
// Get reference to our read Characteristic
161+
_readWriteCharacteristic3 = characteristicResult.Characteristic;
162+
163+
// Set up event handlers for read/write #3
164+
_readWriteCharacteristic3.WriteRequested += _writeCharacteristic_WriteRequested;
165+
_readWriteCharacteristic3.ReadRequested += _readWriteCharacteristic_ReadRequested;
166+
#endregion
167+
109168
// Once all the Characteristics have been created you need to advertise the Service so
110169
// other devices can see it. Here we also say the device can be connected too and other
111170
// devices can see it.
112171
serviceProvider.StartAdvertising(new GattServiceProviderAdvertisingParameters()
113172
{
114-
DeviceName = "Sample2",
115173
IsConnectable = true,
116174
IsDiscoverable = true
117175
});
118176

119-
Debug.WriteLine($"Sample 2 now Advertising");
177+
Console.WriteLine($"Sample 2 Advertising");
120178

121179
Thread.Sleep(Timeout.Infinite);
122180
}
123181

182+
private static void Session_SessionStatusChanged(object sender, GattSessionStatusChangedEventArgs args)
183+
{
184+
Console.WriteLine($"Session_SessionStatusChanged status->{args.Status} Error->{args.Error}");
185+
if (args.Status == GattSessionStatus.Active)
186+
{
187+
Console.WriteLine($"Client connected, address {BluetoothLEServer.Instance.Session.DeviceId:X}");
188+
}
189+
else
190+
{
191+
Console.WriteLine("Client disconnected");
192+
}
193+
}
194+
195+
private static void Pairing_PairingComplete(object sender, DevicePairingEventArgs args)
196+
{
197+
DevicePairing dp = sender as DevicePairing;
198+
199+
Console.WriteLine($"PairingComplete:{args.Status} IOCaps:{dp.IOCapabilities} IsPaired:{dp.IsPaired} IsAuthenticated:{dp.IsAuthenticated}");
200+
}
201+
202+
private static void Pairing_PairingRequested(object sender, DevicePairingRequestedEventArgs args)
203+
{
204+
Console.WriteLine($"CustomPairing_PairingRequested {args.PairingKind}");
205+
206+
switch (args.PairingKind)
207+
{
208+
// Passkey displayed on current device or just a know secret passkey
209+
// Tell BLE what passkey is, so it can be checked against what has been entered on other device
210+
case DevicePairingKinds.DisplayPin:
211+
Console.WriteLine("DisplayPin");
212+
213+
// We don't actually display pin here but just tell Bluetooth what the pin is so it can
214+
// compare with pin supplied by client
215+
args.Accept(PASSKEY);
216+
break;
217+
}
218+
}
219+
124220
private static void _readWriteCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs ReadRequestEventArgs)
125221
{
126222
GattReadRequest request = ReadRequestEventArgs.GetRequest();
@@ -152,8 +248,7 @@ private static void _writeCharacteristic_WriteRequested(GattLocalCharacteristic
152248
request.Respond();
153249
}
154250

155-
Debug.WriteLine($"Received value={_value}");
251+
Console.WriteLine($"Received value={_value}");
156252
}
157-
158253
}
159254
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# 🌶️🌶️ - Bluetooth Low energy: read/write with encryption a value
22

3-
This sample adds security to the Characteristic access. This will force the Server/Client to bond/pair which is
4-
used to generate key pairs for communications. All access is now encrypted.
3+
This sample adds pairing to the Characteristic access. This will force the Server/Client to pair which is
4+
used to generate key pairs for secure communications and a passkey for Authentication.
5+
6+
All access is now encrypted.
7+
8+
You can connect to this Server without pairing but you will on be able to read 1st Characteristic.
59

610
| Custom service | Behavior |
711
| --- | --- |
812
| Read and Write (requires encryption) a value | Read/Write Int32 |
913
| Read (requires encryption) and Write a value | Read/Write Int32 (same value) |
14+
| Read (requires encryption and authentication) and Write a value | Read/Write Int32 (same value) |
1015

1116
The 1st Characteristic allows the read but the write requires it to be paired.
1217
The 2nd Characteristic allows writes but requires to be paired for the read.
18+
The 3rd Characteristic allows writes but requires to be paired with authentication for the read.
1319

14-
See [main Bluetooth sample's readme](../README.md) for more information.
20+
See [main Bluetooth sample's readme](../README.md) for more information.

samples/Bluetooth/BluetoothLESample2/packages.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.14.2" targetFramework="netnanoframework10" />
4-
<package id="nanoFramework.Device.Bluetooth" version="1.1.35" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.Device.Bluetooth" version="1.1.37" targetFramework="netnano1.0" />
55
<package id="nanoFramework.Runtime.Events" version="1.11.6" targetFramework="netnanoframework10" />
66
<package id="nanoFramework.Runtime.Native" version="1.6.6" targetFramework="netnanoframework10" />
77
<package id="nanoFramework.System.Collections" version="1.5.18" targetFramework="netnanoframework10" />

samples/Bluetooth/BluetoothLESample3/BluetoothLESample3.nfproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
</ItemGroup>
2828
<ItemGroup>
2929
<None Include="packages.config" />
30+
<None Include="README.md" />
3031
</ItemGroup>
3132
<ItemGroup>
3233
<Reference Include="mscorlib, Version=1.14.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
3334
<HintPath>packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
3435
<Private>True</Private>
3536
</Reference>
36-
<Reference Include="nanoFramework.Device.Bluetooth, Version=1.1.35.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
37-
<HintPath>packages\nanoFramework.Device.Bluetooth.1.1.35\lib\nanoFramework.Device.Bluetooth.dll</HintPath>
38-
<Private>True</Private>
37+
<Reference Include="nanoFramework.Device.Bluetooth">
38+
<HintPath>packages\nanoFramework.Device.Bluetooth.1.1.37\lib\nanoFramework.Device.Bluetooth.dll</HintPath>
3939
</Reference>
4040
<Reference Include="nanoFramework.Runtime.Events, Version=1.11.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
4141
<HintPath>packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>

0 commit comments

Comments
 (0)