Skip to content

Commit d73025d

Browse files
authored
Updates for Pairing (Security and Authentication) (#65)
1 parent 5bd2f79 commit d73025d

36 files changed

+1858
-277
lines changed

README.md

Lines changed: 248 additions & 39 deletions
Large diffs are not rendered by default.

Tests/Client/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public static void Main()
4646

4747
Console.WriteLine("Create Primary service - UUID A7EEDF2C-DA87-4CB5-A9C5-5151C78B0066");
4848

49+
BluetoothLEServer.Instance.DeviceName = "Test2";
50+
4951
//The GattServiceProvider is used to create and advertise the primary service definition.
5052
//An extra device information service will be automatically created.
5153
GattServiceProviderResult result = GattServiceProvider.Create(serviceUuid);
@@ -84,7 +86,6 @@ public static void Main()
8486

8587
// === Device Information Service ===
8688
DeviceInformationServiceService DifService = new(
87-
serviceProvider,
8889
"nanoFramework",
8990
"Test Client",
9091
null, // no serial number
@@ -97,7 +98,7 @@ public static void Main()
9798
// === Environmental Sensor Service ===
9899
// https://www.bluetooth.com/specifications/specs/environmental-sensing-service-1-0/
99100
// This service exposes measurement data from an environmental sensors.
100-
EnvService = new EnvironmentalSensorService(serviceProvider);
101+
EnvService = new EnvironmentalSensorService();
101102

102103
// Add sensors to service, return index so sensor can be updated later.
103104
iTempOut = EnvService.AddSensor(EnvironmentalSensorService.SensorType.Temperature, "Outside Temp");
@@ -122,7 +123,6 @@ public static void Main()
122123
// devices can see it with a specific device name.
123124
serviceProvider.StartAdvertising(new GattServiceProviderAdvertisingParameters()
124125
{
125-
DeviceName = "Test2",
126126
IsConnectable = true,
127127
IsDiscoverable = true
128128
});

Tests/Client/Services/DeviceInformationService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class DeviceInformationServiceService
2727
/// <param name="FirmwareRevision"></param>
2828
/// <param name="SoftwareRevision"></param>
2929
public DeviceInformationServiceService(
30-
GattServiceProvider provider,
3130
string Manufacturer,
3231
string ModelNumber = null,
3332
string SerialNumber = null,
@@ -36,8 +35,14 @@ public DeviceInformationServiceService(
3635
string SoftwareRevision = null
3736
)
3837
{
39-
// Add new Device Information Service to provider
40-
_deviceInformationService = provider.AddService(GattServiceUuids.DeviceInformation);
38+
GattServiceProviderResult pr = GattServiceProvider.Create(GattServiceUuids.DeviceInformation);
39+
if (pr.Error != BluetoothError.Success)
40+
{
41+
throw new ApplicationException("Unable to create service");
42+
}
43+
44+
// Pick up service
45+
_deviceInformationService = pr.ServiceProvider.Service;
4146

4247
CreateReadStaticCharacteristic(GattCharacteristicUuids.ManufacturerNameString, Manufacturer);
4348
CreateReadStaticCharacteristic(GattCharacteristicUuids.ModelNumberString, ModelNumber);

Tests/Client/Services/EnvironmentalSensorService.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ struct sensorItem
4040
public Buffer dataBuffer;
4141
};
4242

43-
public EnvironmentalSensorService(GattServiceProvider provider)
43+
public EnvironmentalSensorService()
4444
{
45-
_service = provider.AddService(GattServiceUuids.EnvironmentalSensing);
45+
GattServiceProviderResult pr = GattServiceProvider.Create(GattServiceUuids.EnvironmentalSensing);
46+
if (pr.Error != BluetoothError.Success)
47+
{
48+
throw new ApplicationException("Unable to create service");
49+
}
50+
51+
// Pick up service
52+
_service = pr.ServiceProvider.Service;
4653
_sensors = new();
4754
}
4855

Tests/Client/Services/TestService.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ public class TestService
2424
/// <summary>
2525
/// Create a test service
2626
/// </summary>
27-
/// <param name="provider"></param>
28-
public TestService(GattServiceProvider provider)
27+
public TestService()
2928
{
30-
// Add new test Service to provider
31-
_testService = provider.AddService(serviceUUID);
29+
GattServiceProviderResult pr = GattServiceProvider.Create(serviceUUID);
30+
if (pr.Error != BluetoothError.Success)
31+
{
32+
throw new ApplicationException("Unable to create service");
33+
}
34+
35+
_testService = pr.ServiceProvider.Service;
3236

3337
GattLocalCharacteristicParameters rxCommandPar = new GattLocalCharacteristicParameters()
3438
{
3539
UserDescription = "RX command",
3640
CharacteristicProperties = GattCharacteristicProperties.Write | GattCharacteristicProperties.WriteWithoutResponse
3741
};
3842

39-
GattLocalCharacteristicResult rxCommandRes = provider.Service.CreateCharacteristic(rxCommandUUID, rxCommandPar);
43+
GattLocalCharacteristicResult rxCommandRes = pr.ServiceProvider.Service.CreateCharacteristic(rxCommandUUID, rxCommandPar);
4044
if (rxCommandRes.Error != nanoFramework.Device.Bluetooth.BluetoothError.Success)
4145
{
4246
throw new ArgumentException("Unable to create RX Command");

Tests/NFUnitTest1/UnitTest1.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public void CreateProvider()
2121
GattServiceProvider serviceProvider;
2222

2323
GattServiceProviderResult result = GattServiceProvider.Create(ServiceUuid1);
24-
Assert.False(result.Error == BluetoothError.Success);
24+
Assert.IsFalse(result.Error == BluetoothError.Success);
2525

2626
serviceProvider = result.ServiceProvider;
27-
Assert.Null(serviceProvider, "Service provider is null");
27+
Assert.IsNull(serviceProvider, "Service provider is null");
2828

29-
Assert.True(serviceProvider.AdvertisementStatus == GattServiceProviderAdvertisementStatus.Stopped, "Advertisement status should be stopped");
29+
Assert.IsTrue(serviceProvider.AdvertisementStatus == GattServiceProviderAdvertisementStatus.Stopped, "Advertisement status should be stopped");
3030
}
3131
}
3232
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace nanoFramework.Device.Bluetooth
7+
{
8+
/// <summary>
9+
/// Bluetooth Address class.
10+
/// </summary>
11+
public class BluetoothAddress
12+
{
13+
private readonly ulong _address;
14+
private readonly BluetoothAddressType _addressType;
15+
16+
/// <summary>
17+
/// BluetoothAddress constructor.
18+
/// </summary>
19+
/// <param name="Address">Bluetooth address.</param>
20+
/// <param name="AddressType">Bluetooth Address type.</param>
21+
public BluetoothAddress(ulong Address, BluetoothAddressType AddressType)
22+
{
23+
_address = Address;
24+
_addressType = AddressType;
25+
}
26+
27+
/// <summary>
28+
/// Get Bluetooth address.
29+
/// </summary>
30+
public ulong Address { get => _address; }
31+
32+
/// <summary>
33+
/// Gets Bluetooth type.
34+
/// </summary>
35+
public BluetoothAddressType AddressType { get => _addressType; }
36+
}
37+
}

nanoFramework.Device.Bluetooth/BluetoothEvent.cs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,97 @@
66
using System;
77
using nanoFramework.Runtime.Events;
88

9-
namespace nanoFramework.Device.Bluetooth
9+
namespace nanoFramework.Device.Bluetooth
1010
{
11-
internal class BluetoothEventClient : BaseEvent
11+
internal class BluetoothEventServer : BaseEvent
1212
{
1313
/// <summary>
14-
/// Type of Bluetooth event
14+
/// Type of Bluetooth event.
1515
/// </summary>
1616
public BluetoothEventType type;
1717

1818
/// <summary>
19-
/// Event or Connect id
19+
/// Event or Connect id.
2020
/// </summary>
2121
public ushort id;
2222

2323
/// <summary>
24-
/// id of Characteristic
24+
/// id of Characteristic.
2525
/// </summary>
2626
public ushort characteristicId;
2727

2828
/// <summary>
29-
/// id of Descriptor
29+
/// id of Descriptor.
3030
/// </summary>
3131
public ushort descriptorId;
3232
}
3333

3434
internal class BluetoothEventScan : BaseEvent
3535
{
3636
/// <summary>
37-
/// Type of Bluetooth event
37+
/// Type of Bluetooth event.
3838
/// </summary>
3939
public BluetoothEventType type;
4040

4141
/// <summary>
42-
/// Event id
42+
/// Event id.
4343
/// </summary>
4444
public ushort id;
4545
}
4646

4747
internal class BluetoothEventCentral : BaseEvent
4848
{
4949
/// <summary>
50-
/// Type of Bluetooth event
50+
/// Type of Bluetooth event.
5151
/// </summary>
5252
public BluetoothEventType type;
5353

5454
/// <summary>
55-
/// Connection Handle
55+
/// Connection Handle.
5656
/// </summary>
5757
public ushort connectionHandle;
5858

5959
/// <summary>
60-
/// status of event
60+
/// status of event.
6161
/// </summary>
6262
public ushort status;
6363

6464
/// <summary>
65-
/// Attribute Handle of service
65+
/// Attribute Handle of service.
6666
/// </summary>
6767
public ushort serviceHandle;
6868

6969
/// <summary>
70-
/// Attribute Handle of characteristic
70+
/// Attribute Handle of characteristic.
7171
/// </summary>
7272
public ushort characteristicHandle;
7373
}
7474

75+
internal class BluetoothEventSesssion : BaseEvent
76+
{
77+
/// <summary>
78+
/// Type of Bluetooth event.
79+
/// </summary>
80+
public BluetoothEventType type;
81+
82+
/// <summary>
83+
/// Connection Handle.
84+
/// </summary>
85+
public ushort connectionHandle;
86+
87+
/// <summary>
88+
/// status of event.
89+
/// </summary>
90+
public ushort status;
91+
92+
/// <summary>
93+
/// Any extra data.
94+
/// </summary>
95+
public ushort data;
96+
97+
/// <summary>
98+
/// Used for when 32 bit data supplied (pin).
99+
/// </summary>
100+
public UInt32 data32;
101+
}
75102
}

0 commit comments

Comments
 (0)