Repository navigation
Expand file tree
/
Copy pathUnity3D_BLENativeManager.m
More file actions
222 lines (191 loc) · 5.87 KB
/
Copy pathUnity3D_BLENativeManager.m
File metadata and controls
222 lines (191 loc) · 5.87 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
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
/*
* Unity3D_BLE — A Unity3D desktop plugin for BlueTooth Low Energy
*
* Created by Pekka Nikander <pekka.nikander@iki.fi> on 08/08/2018.
* Placed in public domain.
*/
#import <CoreBluetooth/CoreBluetooth.h>
#import "Unity3D_BLENativeManager.h"
#include <assert.h>
#include <stdio.h>
@interface BLENativeManager () <CBCentralManagerDelegate> {
@public
void *cs_context; // C# BLE context
CBCentralManager *cbmanager;
void (*deviceFoundCallback)(
void *, CBPeripheral *, NativeAdvertisementData */*XXX*/, long int);
}
@end
@implementation BLENativeManager
- (id)init
{
self = [super init];
NSLog(@"BLENativeManager init: %@", self);
return self;
}
- (id)initWithCSContext: (void*)context manager: (CBCentralManager *)cbmanager
{
NSLog(@"Unity3D_BLE: Initialise");
if (self) {
self->cs_context = context;
[cbmanager retain];
self->cbmanager = cbmanager;
}
return self;
}
- (void)dealloc
{
NSLog(@"BLENativeManager dealloc: %@", self);
if (self->cbmanager) {
[self->cbmanager release];
self->cbmanager = NULL;
}
self->cs_context = NULL;
[super dealloc];
}
- (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)cbmanager
{
NSLog(@"centralManagerDidUpdateState: %ld", cbmanager.state);
// XXX
}
- (void) centralManager:(CBCentralManager *)cbmanager
didDiscoverPeripheral:(CBPeripheral *)cbperipheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSLog(@"Discovered peripheral: cs_context=%ld name=%@ RSSI=%@",
(long)cs_context, cbperipheral.name, RSSI);
if (self->deviceFoundCallback) {
self->deviceFoundCallback(
cs_context,
cbperipheral,
advertisementData/*XXX*/,
[RSSI integerValue]);
}
}
- (void) centralManager:(CBCentralManager *)cbmanager
didConnectPeripheral:(CBPeripheral *)cbperipheral
{
NSLog(@"Connected to peripheral: %@", cbperipheral);
/*
* We must trigger discovering for services from here,
* since it is possible that the user has set the services
* it is interested about already before connecting.
*/
if (cbperipheral.delegate) {
[cbperipheral.delegate peripheral: cbperipheral didModifyServices: @[]];
}
}
- (void) centralManager:(CBCentralManager *)cbmanager
didDisconnectPeripheral:(CBPeripheral *)cbperipheral
error:(NSError *)error
{
NSLog(@"Disconnected from peripheral: %@: error=%@", cbperipheral, error);
BLENativePeripheral *p = cbperipheral.delegate;
if (p->cs_connected) {
NSLog(@"Attempting to reconnect to peripheral: %@", cbperipheral);
[cbmanager connectPeripheral: p->cbperipheral options: nil];
}
}
@end
/*
* BLE Unity3D plain C API
*/
void BLENativeInitLog(void)
{
/* Redirect NSlog to a known good file */
char filename[PATH_MAX];
sprintf(filename, "%s/%s", getenv("HOME"), "Library/Logs/Unity/Plugin.log");
freopen(filename, "a+", stderr);
NSLog(@"Log initialised.");
}
/**
* Create a new instance of the native BLE manager.
*
* Called by the C# SafeHandle runtime, without any arguments. Hence, we
* must initialise the member variables separately.
*/
BLENativeManager *BLENativeCreateManager(void)
{
// XXX Test if refactoring init works
BLENativeManager *this = [[BLENativeManager alloc] init];
return this;
}
void BLENativeInitialise(BLENativeManager *this, void *context)
{
NSLog(@"BLENativeInitialise");
[this initWithCSContext: context
manager: [[CBCentralManager alloc]
initWithDelegate: this queue: nil]];
}
void BLENativeDeInitialise(BLENativeManager *this)
{
NSLog(@"BLENativeDeInitialise");
if (this->deviceFoundCallback) {
BLENativeScanStop(this); // Clears deviceFoundCallback
}
BLENativeDisconnectAll(this);
// cbmanager released in dealloc
[this release]; // For alloc in BLECreateContext
}
void BLENativeScanStart(
BLENativeManager *this,
const char *serviceUUIDstring,
BLENativeScanDeviceFoundCallback callback)
{
this->deviceFoundCallback = callback;
NSDictionary *options = @{ CBCentralManagerScanOptionAllowDuplicatesKey: @NO };
NSArray<CBUUID*> *serviceUUIDs = nil;
if ((NULL != serviceUUIDstring) && (0 != strlen(serviceUUIDstring))) {
CBUUID *serviceUUID =
[CBUUID UUIDWithString:
[NSString stringWithCString:serviceUUIDstring
encoding:NSUTF8StringEncoding]];
serviceUUIDs = @[serviceUUID];
}
NSLog(@"BLENativeScanStart: for %@", serviceUUIDs);
[this->cbmanager scanForPeripheralsWithServices: serviceUUIDs options: options];
}
void BLENativeScanStop(BLENativeManager *this)
{
NSLog(@"BLENativeScanStop");
this->deviceFoundCallback = NULL;
[this->cbmanager stopScan];
}
NativeConnection *BLENativeConnect(
BLENativeManager *this, BLENativePeripheral *p)
{
[p retain];
NSLog(@"Connecting to peripheral: %p name=%@", p->cbperipheral, p->cbperipheral.name);
assert(p->cbperipheral);
p->cs_connected = YES;
[this->cbmanager connectPeripheral: p->cbperipheral options:nil];
[p tryLocateMyServiceWithDiscovery: NO];
return p;
}
void BLENativeDisconnect(BLENativeManager *this, NativeConnection *connection)
{
BLENativePeripheral *p = connection; // XXX
NSLog(@"Disconnecting from peripheral: %p name=%@", p->cbperipheral, p->cbperipheral.name);
p->cs_connected = NO;
assert(p->cbperipheral);
[this->cbmanager cancelPeripheralConnection: p->cbperipheral];
[p release];
}
void BLENativeDisconnectAll(BLENativeManager *this)
{
#if 0
// TBD
for (CBPeripheral *p in peris) {
BLENativeDisconnect(this, p);
}
#endif
}
#if UNITY_TEST_THREADING
/* XXX REMOVE */
void BLENativeLinuxHelper(BLENativeManager *this)
{
NSLog(@"LinuxHelper: Sleeping");
sleep(1);
}
#endif