-
Notifications
You must be signed in to change notification settings - Fork 0
/
BleApi.cs
240 lines (185 loc) · 9.31 KB
/
BleApi.cs
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using UnityEngine;
public class BleApi
{
// dll calls
public enum ScanStatus { PROCESSING, AVAILABLE, FINISHED };
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DeviceUpdate
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string id;
[MarshalAs(UnmanagedType.I1)]
public bool isConnectable;
[MarshalAs(UnmanagedType.I1)]
public bool isConnectableUpdated;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string name;
[MarshalAs(UnmanagedType.I1)]
public bool nameUpdated;
}
[DllImport("BleWinrtDll.dll", EntryPoint = "StartDeviceScan")]
public static extern void StartDeviceScan();
[DllImport("BleWinrtDll.dll", EntryPoint = "PollDevice")]
public static extern ScanStatus PollDevice(ref DeviceUpdate device, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "StopDeviceScan")]
public static extern void StopDeviceScan();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct Service
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string uuid;
};
[DllImport("BleWinrtDll.dll", EntryPoint = "ScanServices", CharSet = CharSet.Unicode)]
public static extern void ScanServices(string deviceId);
[DllImport("BleWinrtDll.dll", EntryPoint = "PollService")]
public static extern ScanStatus PollService(out Service service, bool block);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct Characteristic
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string uuid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string userDescription;
};
[DllImport("BleWinrtDll.dll", EntryPoint = "ScanCharacteristics", CharSet = CharSet.Unicode)]
public static extern void ScanCharacteristics(string deviceId, string serviceId);
[DllImport("BleWinrtDll.dll", EntryPoint = "PollCharacteristic")]
public static extern ScanStatus PollCharacteristic(out Characteristic characteristic, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "SubscribeCharacteristic", CharSet = CharSet.Unicode)]
public static extern bool SubscribeCharacteristic(string deviceId, string serviceId, string characteristicId, bool block);
//[DllImport("BleWinrtDll.dll", EntryPoint = "SubscribeCharacteristic_Read", CharSet = CharSet.Unicode)]
//public static extern bool SubscribeCharacteristic_Read(string deviceId, string serviceId, string characteristicId, bool block);
//[DllImport("BleWinrtDll.dll", EntryPoint = "SubscribeCharacteristic_Read", CharSet = CharSet.Unicode)]
//public static extern bool SubscribeCharacteristic_Write(string deviceId, string serviceId, string characteristicId, bool block);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BLEData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
public byte[] buf;
[MarshalAs(UnmanagedType.I2)]
public short size;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string deviceId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string serviceUuid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string characteristicUuid;
};
[DllImport("BleWinrtDll.dll", EntryPoint = "PollData")]
public static extern bool PollData(out BLEData data, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "SendData")]
public static extern bool SendData(in BLEData data, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "Quit")]
public static extern void Quit();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct ErrorMessage
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string msg;
};
[DllImport("BleWinrtDll.dll", EntryPoint = "GetError")]
public static extern void GetError(out ErrorMessage buf);
//HR
public enum ScanStatusHR { PROCESSING, AVAILABLE, FINISHED };
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DeviceUpdateHR
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string id;
[MarshalAs(UnmanagedType.I1)]
public bool isConnectable;
[MarshalAs(UnmanagedType.I1)]
public bool isConnectableUpdated;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string name;
[MarshalAs(UnmanagedType.I1)]
public bool nameUpdated;
}
[DllImport("BleWinrtDll.dll", EntryPoint = "StartDeviceScan")]
public static extern void StartDeviceScanHR();
[DllImport("BleWinrtDll.dll", EntryPoint = "PollDevice")]
public static extern ScanStatusHR PollDeviceHR(ref DeviceUpdateHR device, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "StopDeviceScan")]
public static extern void StopDeviceScanHR();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct ServiceHR
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string uuid;
};
[DllImport("BleWinrtDll.dll", EntryPoint = "ScanServices", CharSet = CharSet.Unicode)]
public static extern void ScanServicesHR(string deviceId);
[DllImport("BleWinrtDll.dll", EntryPoint = "PollService")]
public static extern ScanStatusHR PollServiceHR(out ServiceHR service, bool block);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CharacteristicHR
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string uuid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string userDescription;
};
[DllImport("BleWinrtDll.dll", EntryPoint = "Characteristic_ValueChanged", CharSet = CharSet.Unicode)]
public static extern void Characteristic_ValueChanged(string deviceId, string serviceId);
[DllImport("BleWinrtDll.dll", EntryPoint = "ScanCharacteristics", CharSet = CharSet.Unicode)]
public static extern void ScanCharacteristicsHR(string deviceId, string serviceId);
[DllImport("BleWinrtDll.dll", EntryPoint = "PollCharacteristic")]
public static extern ScanStatusHR PollCharacteristicHR(out CharacteristicHR characteristichr, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "SubscribeCharacteristic", CharSet = CharSet.Unicode)]
public static extern bool SubscribeCharacteristicHR(string deviceIdhr, string serviceIdhr, string characteristicIdhr, bool block);
//[DllImport("BleWinrtDll.dll", EntryPoint = "SubscribeCharacteristic_Read", CharSet = CharSet.Unicode)]
//public static extern bool SubscribeCharacteristic_ReadHR(string deviceIdhr, string serviceIdhr, string characteristicIdhr, bool block);
//[DllImport("BleWinrtDll.dll", EntryPoint = "SubscribeCharacteristic_Write", CharSet = CharSet.Unicode)]
//public static extern bool SubscribeCharacteristic_WriteHR(string deviceId, string serviceId, string characteristicId, bool block);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BLEDataHR
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
public byte[] buf;
[MarshalAs(UnmanagedType.I2)]
public short size;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string deviceId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string serviceUuid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string characteristicUuid;
};
[DllImport("BleWinrtDll.dll", EntryPoint = "PollData")]
public static extern bool PollDataHR(out BLEDataHR datahr, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "SendData")]
public static extern bool SendDataHR(in BLEDataHR datahr, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "Quit")]
public static extern void QuitHR();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct ErrorMessageHR
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string msg;
};
[DllImport("BleWinrtDll.dll", EntryPoint = "GetError")]
public static extern void GetErrorHR(out ErrorMessageHR buf);
[DllImport("BleWinrtDll.dll", EntryPoint = "PollData")]
public static extern bool PollDataCP(out BLEDataCP datacp, bool block);
[DllImport("BleWinrtDll.dll", EntryPoint = "SendData")]
public static extern bool SendDataCP(in BLEDataCP datacp, bool block);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BLEDataCP
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
public byte[] buf;
[MarshalAs(UnmanagedType.I2)]
public short size;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string deviceId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string serviceUuid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string characteristicUuid;
};
}