質問編集履歴
4
スクリーンショットを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -443,3 +443,7 @@
|
|
443
443
|
|
444
444
|
|
445
445
|
![](81a3f95ec96774cea0e75129d8f4e158.png)
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
![イメージ説明](7f30386024e41b854e127ce794c2c566.png)
|
3
mainstoryboardのスクリーンショットを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -431,3 +431,15 @@
|
|
431
431
|
|
432
432
|
|
433
433
|
```
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
![イメージ説明](ebfcb4001307c674b3188ea2a5988533.png)
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
![イメージ説明](18f642286837bbe9df546777229bf741.png)
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
![](81a3f95ec96774cea0e75129d8f4e158.png)
|
2
コード全文書き加えました
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,23 +40,221 @@
|
|
40
40
|
|
41
41
|
|
42
42
|
|
43
|
+
以下がコードの全文です。
|
44
|
+
|
43
45
|
|
44
46
|
|
45
47
|
```
|
46
48
|
|
47
|
-
|
49
|
+
// ViewController.swift
|
50
|
+
|
51
|
+
|
52
|
+
|
48
|
-
|
53
|
+
import UIKit
|
54
|
+
|
49
|
-
|
55
|
+
import CoreBluetooth
|
56
|
+
|
57
|
+
|
58
|
+
|
50
|
-
|
59
|
+
class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource,CBCentralManagerDelegate,CBPeripheralDelegate {
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
var myUuids: NSMutableArray = NSMutableArray()
|
64
|
+
|
65
|
+
var myServiceUuids: NSMutableArray = NSMutableArray()
|
66
|
+
|
51
|
-
var myCharacteristicUUID:NSMutableArray
|
67
|
+
var myCharacteristicUUID:NSMutableArray=NSMutableArray()
|
68
|
+
|
69
|
+
var myService: NSMutableArray = NSMutableArray()
|
70
|
+
|
71
|
+
var myIndexPath: NSMutableArray = NSMutableArray()
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
var targetService:CBService!
|
52
76
|
|
53
77
|
var relayedFoundUUID:NSMutableArray = NSMutableArray()
|
54
78
|
|
79
|
+
|
80
|
+
|
55
|
-
|
81
|
+
var manager: CBCentralManager!
|
82
|
+
|
56
|
-
|
83
|
+
var peripheral: CBPeripheral!
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
@IBOutlet weak var tableViewOutlet: UITableView!
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
override func viewDidLoad() {
|
92
|
+
|
93
|
+
super.viewDidLoad()
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
self.manager = CBCentralManager(delegate: self, queue: nil)
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
@IBAction func startButton(_ sender: AnyObject) {
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
self.manager.scanForPeripherals(withServices: nil, options: nil)
|
112
|
+
|
113
|
+
print("start scan peripherals")
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
@IBAction func stopButton(_ sender: AnyObject) {
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
self.manager.stopScan()
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
if central.state == CBManagerState.poweredOn {
|
138
|
+
|
139
|
+
print("BLE Powered ON")
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
self.peripheral = peripheral
|
154
|
+
|
155
|
+
self.peripheral.delegate = self
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
self.manager.connect(peripheral, options: nil)
|
160
|
+
|
161
|
+
self.manager.stopScan()
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
print("connected to peripheral")
|
176
|
+
|
177
|
+
self.peripheral.discoverServices(nil)
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
186
|
+
|
187
|
+
|
188
|
+
|
57
|
-
|
189
|
+
if error != nil {
|
190
|
+
|
58
|
-
|
191
|
+
print("Failed to discover services")
|
192
|
+
|
193
|
+
|
194
|
+
|
59
|
-
|
195
|
+
} else{
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
for service in peripheral.services! {
|
200
|
+
|
201
|
+
myServiceUuids.add(service.uuid)
|
202
|
+
|
203
|
+
myService.add(service)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
//
|
212
|
+
|
213
|
+
DispatchQueue.main.async(execute: {
|
214
|
+
|
215
|
+
self.tableViewOutlet.reloadData()
|
216
|
+
|
217
|
+
})
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
myIndexPath.add(indexPath.row)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
let targetService = myService[indexPath.row]
|
238
|
+
|
239
|
+
print("target_service: \(targetService)")
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
self.peripheral.discoverCharacteristics(nil, for:targetService as! CBService)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
performSegue(withIdentifier: "mySegue", sender: nil)
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
// targetService に対応する characteristicを探すメソッド
|
60
258
|
|
61
259
|
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor targetService: CBService, error: Error?) {
|
62
260
|
|
@@ -66,16 +264,10 @@
|
|
66
264
|
|
67
265
|
print("Characteristic NOT found : \(error)")
|
68
266
|
|
69
|
-
|
267
|
+
|
70
|
-
|
71
|
-
return
|
72
268
|
|
73
269
|
}
|
74
270
|
|
75
|
-
// characteristicが見つかった時の処理
|
76
|
-
|
77
|
-
print("Characteristics found:")
|
78
|
-
|
79
271
|
|
80
272
|
|
81
273
|
let foundCharacteristics = targetService.characteristics
|
@@ -86,11 +278,7 @@
|
|
86
278
|
|
87
279
|
|
88
280
|
|
89
|
-
myCharacteristicUUID.add(c.uuid)
|
281
|
+
self.myCharacteristicUUID.add(c.uuid)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
print("myCharacteristicUUID : \(myCharacteristicUUID)")
|
94
282
|
|
95
283
|
|
96
284
|
|
@@ -104,27 +292,29 @@
|
|
104
292
|
|
105
293
|
|
106
294
|
|
107
|
-
// segue
|
295
|
+
// prepare for segue
|
108
296
|
|
109
297
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
110
298
|
|
111
|
-
|
299
|
+
|
112
300
|
|
113
301
|
if (segue.identifier == "mySegue") {
|
114
302
|
|
115
303
|
|
116
304
|
|
305
|
+
print("myCharacteristicUUID: \(myCharacteristicUUID)")
|
306
|
+
|
307
|
+
|
308
|
+
|
117
309
|
let mySecondViewController : SecondViewController = segue.destination as! SecondViewController
|
118
310
|
|
119
|
-
|
120
|
-
|
311
|
+
|
312
|
+
|
121
|
-
mySecondViewController.relayedFoundUUID = myCharacteristicUUID
|
313
|
+
mySecondViewController.relayedFoundCharacteristicUUID = myCharacteristicUUID
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
314
|
+
|
126
|
-
|
127
|
-
|
315
|
+
|
316
|
+
|
317
|
+
|
128
318
|
|
129
319
|
}
|
130
320
|
|
@@ -132,34 +322,112 @@
|
|
132
322
|
|
133
323
|
}
|
134
324
|
|
325
|
+
|
326
|
+
|
327
|
+
|
328
|
+
|
135
|
-
|
329
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
330
|
+
|
331
|
+
|
332
|
+
|
136
|
-
|
333
|
+
return myServiceUuids.count
|
334
|
+
|
137
|
-
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
|
340
|
+
|
138
|
-
|
341
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:"firstTable" )
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
// Cellに値を設定.
|
352
|
+
|
353
|
+
cell.textLabel?.sizeToFit()
|
354
|
+
|
355
|
+
cell.textLabel?.textColor = UIColor.red
|
356
|
+
|
357
|
+
cell.textLabel?.text = "\(myServiceUuids[indexPath.row])"
|
358
|
+
|
139
|
-
|
359
|
+
cell.textLabel?.font = UIFont.systemFont(ofSize: 16)
|
140
|
-
|
141
|
-
|
142
|
-
|
360
|
+
|
361
|
+
|
362
|
+
|
143
|
-
|
363
|
+
return cell
|
364
|
+
|
144
|
-
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
|
370
|
+
|
145
|
-
v
|
371
|
+
override func didReceiveMemoryWarning() {
|
372
|
+
|
373
|
+
super.didReceiveMemoryWarning()
|
374
|
+
|
375
|
+
// Dispose of any resources that can be recreated.
|
376
|
+
|
377
|
+
}
|
146
378
|
|
147
379
|
|
148
380
|
|
381
|
+
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
```
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
```
|
390
|
+
|
391
|
+
// SecondViewController.swift
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
import UIKit
|
396
|
+
|
397
|
+
import CoreBluetooth
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
class SecondViewController: UIViewController,CBPeripheralDelegate {
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
var relayedFoundCharacteristicUUID:NSMutableArray!
|
408
|
+
|
409
|
+
|
410
|
+
|
149
411
|
override func viewDidLoad() {
|
150
412
|
|
151
413
|
super.viewDidLoad()
|
152
414
|
|
153
|
-
|
154
|
-
|
155
|
-
|
415
|
+
|
156
|
-
|
416
|
+
|
157
|
-
print("relayedFoud
|
417
|
+
print("relayedFoundCharacteristicUUID:\(relayedFoundCharacteristicUUID)")
|
158
|
-
|
159
|
-
|
160
|
-
|
418
|
+
|
419
|
+
|
420
|
+
|
161
|
-
}
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
162
|
-
|
425
|
+
@IBOutlet weak var secondTableOutlet: UITableView!
|
426
|
+
|
427
|
+
|
428
|
+
|
163
|
-
|
429
|
+
}
|
430
|
+
|
431
|
+
|
164
432
|
|
165
433
|
```
|
1
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
|
38
38
|
|
39
|
-
となり、値を読み取ることができません。
|
39
|
+
となり、値を読み取ることができません。 色々と試してみると、myCharacteristicUUIDもfuncの外に受け渡すことができていないことが分かりました。BLEから取り出した情報をそのfuncの外でも利用できる様にするには何か方法があるのでしょうか?
|
40
40
|
|
41
41
|
|
42
42
|
|