forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
159 lines (130 loc) · 6.39 KB
/
Program.cs
File metadata and controls
159 lines (130 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Diagnostics;
using System.Threading;
using nanoFramework.Device.Bluetooth;
using nanoFramework.Device.Bluetooth.GenericAttributeProfile;
/// <summary>
/// Bluetooth Sample 2 is a custom service which shows the use of:
///
/// - Adding security to characteristics
/// - 1st characteristic you can read without pairing but will need to be paired to write
/// - 2nd characteristic you can write without pairing but will need to be paired to Read
/// - Both characteristic read/write same value.
///
/// You will be able to connect to the service and read values or subscribe to Notified ever 10 seconds.
/// Suitable Phone apps: "LightBlue" or "nRF Connect"
/// </summary>
namespace BluetoothLESample2
{
public class Program
{
static GattLocalCharacteristic _readWriteCharacteristic1;
static GattLocalCharacteristic _readWriteCharacteristic2;
// value used to read/write
static Int32 _value;
public static void Main()
{
Debug.WriteLine("Hello from Bluetooth Sample 2");
Guid serviceUuid = new Guid("A7EEDF2C-DA8C-4CB5-A9C5-5151C78B0057");
Guid writeCharUuid1 = new Guid("A7EEDF2C-DA8D-4CB5-A9C5-5151C78B0057");
Guid writeCharUuid2 = new Guid("A7EEDF2C-DA8E-4CB5-A9C5-5151C78B0057");
//The GattServiceProvider is used to create and advertise the primary service definition.
//An extra device information service will be automatically created.
GattServiceProviderResult result = GattServiceProvider.Create(serviceUuid);
if (result.Error != BluetoothError.Success)
{
return;
}
GattServiceProvider serviceProvider = result.ServiceProvider;
// Get created Primary service from provider
GattLocalService service = serviceProvider.Service;
#region Characteristic 1
// Add Read Characteristic for data that changes to service
// We also want the connected client to be notified when value changes so we add the notify property
GattLocalCharacteristicResult characteristicResult = service.CreateCharacteristic(writeCharUuid1,
new GattLocalCharacteristicParameters()
{
CharacteristicProperties = GattCharacteristicProperties.Read | GattCharacteristicProperties.Write,
UserDescription = "My Read/Write Characteristic 1",
ReadProtectionLevel = GattProtectionLevel.Plain,
WriteProtectionLevel = GattProtectionLevel.EncryptionRequired
});
;
if (characteristicResult.Error != BluetoothError.Success)
{
// An error occurred.
return;
}
// Get reference to our read Characteristic
_readWriteCharacteristic1 = characteristicResult.Characteristic;
// Set up event handlers for read/write
_readWriteCharacteristic1.WriteRequested += _writeCharacteristic_WriteRequested;
_readWriteCharacteristic1.ReadRequested += _readWriteCharacteristic_ReadRequested;
#endregion
#region Characteristic 1
// Add Read Characteristic for data that changes to service
// We also want the connected client to be notified when value changes so we add the notify property
characteristicResult = service.CreateCharacteristic(writeCharUuid2,
new GattLocalCharacteristicParameters()
{
CharacteristicProperties = GattCharacteristicProperties.Read | GattCharacteristicProperties.Write,
UserDescription = "My Read/Write Characteristic 2",
ReadProtectionLevel = GattProtectionLevel.EncryptionRequired,
WriteProtectionLevel = GattProtectionLevel.Plain
});
;
if (characteristicResult.Error != BluetoothError.Success)
{
// An error occurred.
return;
}
// Get reference to our read Characteristic
_readWriteCharacteristic2 = characteristicResult.Characteristic;
// Set up event handlers for read/write
_readWriteCharacteristic2.WriteRequested += _writeCharacteristic_WriteRequested;
_readWriteCharacteristic2.ReadRequested += _readWriteCharacteristic_ReadRequested;
#endregion
// Once all the Characteristics have been created you need to advertise the Service so
// other devices can see it. Here we also say the device can be connected too and other
// devices can see it.
serviceProvider.StartAdvertising(new GattServiceProviderAdvertisingParameters()
{
DeviceName = "Sample2",
IsConnectable = true,
IsDiscoverable = true
});
Debug.WriteLine($"Sample 2 now Advertising");
Thread.Sleep(Timeout.Infinite);
}
private static void _readWriteCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs ReadRequestEventArgs)
{
GattReadRequest request = ReadRequestEventArgs.GetRequest();
DataWriter dw = new();
dw.WriteInt32(_value);
request.RespondWithValue(dw.DetachBuffer());
}
private static void _writeCharacteristic_WriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs WriteRequestEventArgs)
{
GattWriteRequest request = WriteRequestEventArgs.GetRequest();
// Check expected data length, we are expecting 4 bytes (Int32)
if (request.Value.Length != 4)
{
request.RespondWithProtocolError((byte)BluetoothError.NotSupported);
return;
}
// Unpack data from buffer
DataReader rdr = DataReader.FromBuffer(request.Value);
_value = rdr.ReadInt32();
// Respond if Write requires response
if (request.Option == GattWriteOption.WriteWithResponse)
{
request.Respond();
}
Debug.WriteLine($"Received value={_value}");
}
}
}