Skip to content

Commit c2667c0

Browse files
Merge pull request #23 from Capstone-Projects-2023-Spring/iosDataConnection
Able to show/change the data received from Adafruit Bluefruit LE
2 parents 65d3035 + 93fda5b commit c2667c0

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

iosApp/VC/Controllers/BluetoothManager.swift

+22-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BluetoothManager: UIViewController, CBCentralManagerDelegate, CBPeripheral
2121
var centralManager: CBCentralManager!
2222
var peripheral: CBPeripheral!
2323
let BLEServiceUUID = CBUUID(string: "00110011-4455-6677-8899-aabbccddeeff")
24+
private var rxCharacteristic: CBCharacteristic?
2425
var callObserver = CXCallObserver()
2526

2627
// Singleton instance
@@ -67,6 +68,9 @@ class BluetoothManager: UIViewController, CBCentralManagerDelegate, CBPeripheral
6768

6869
// When connect to the Peripheral
6970
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
71+
self.peripheral = peripheral
72+
peripheral.delegate = self
73+
peripheral.discoverServices([CBUUID(string: "00110011-4455-6677-8899-aabbccddeeff")])
7074
delegate?.didConnectPeripheral()
7175
}
7276

@@ -76,28 +80,34 @@ class BluetoothManager: UIViewController, CBCentralManagerDelegate, CBPeripheral
7680
}
7781

7882
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
79-
guard let services = peripheral.services else { return }
83+
guard let services = peripheral.services else {
84+
return
85+
}
8086
for service in services {
81-
peripheral.discoverCharacteristics(nil, for: service)
87+
peripheral.discoverCharacteristics([CBUUID(string: "00112233-4455-6677-8899-abbccddeefff")], for: service)
8288
}
8389
}
8490

8591
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
86-
guard let characteristics = service.characteristics else { return }
92+
guard let characteristics = service.characteristics else {
93+
return
94+
}
8795
for characteristic in characteristics {
88-
print(characteristic.uuid)
89-
// Handle the available characteristics as needed
96+
if characteristic.uuid == CBUUID(string: "00112233-4455-6677-8899-abbccddeefff") {
97+
self.rxCharacteristic = characteristic
98+
peripheral.setNotifyValue(true, for: characteristic)
99+
}
90100
}
91101
}
92102

93103
/*
94-
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
95-
if call.hasConnected {
96-
print("Call connected")
97-
} else if call.hasEnded {
98-
print("Call ended")
99-
}
100-
}
104+
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
105+
if call.hasConnected {
106+
print("Call connected")
107+
} else if call.hasEnded {
108+
print("Call ended")
109+
}
110+
}
101111
*/
102112

103113
// Read/Write/Handle the data

iosApp/VC/Controllers/VCHomeViewController.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class VCHomeViewController: UIViewController, BluetoothManagerDelegate {
3232
// Add a label for the data
3333
dataFromAdafruit = UILabel()
3434
dataFromAdafruit.translatesAutoresizingMaskIntoConstraints = false
35-
dataFromAdafruit.text = "DEFAULT_DATA"
35+
dataFromAdafruit.text = "0"
3636
dataFromAdafruit.textColor = UIColor.gray
3737
view.addSubview(dataFromAdafruit)
3838

@@ -46,27 +46,31 @@ final class VCHomeViewController: UIViewController, BluetoothManagerDelegate {
4646
// Start scanning for the peripheral device
4747
BluetoothManager.shared.delegate = self
4848
BluetoothManager.shared.startScanning()
49+
print("Device started in console")
4950
}
5051

5152
// When the Adafruit Bluefruit LE connects -> Change the connection status text & color to green
5253
func didConnectPeripheral() {
54+
print("Device connected in console")
5355
peripheralStatusLabel.text = "Device connected"
5456
peripheralStatusLabel.textColor = UIColor.green
5557
}
5658

5759
// When the Adafruit Bluefruit LE disconnects -> Change the connection status text & color back to red
5860
func didDisconnectPeripheral() {
61+
print("Device disconnected in console")
5962
peripheralStatusLabel.text = "Device disconnected"
6063
peripheralStatusLabel.textColor = UIColor.red
6164
}
6265

6366
func didReceiveData(_ data: Data) {
67+
print("Device received data from Adafruit Bluefruit LE")
6468
// Convert the data to a string
6569
let receivedString = String(data: data, encoding: .utf8)
6670
// Update the label's text with the received string
6771
dataFromAdafruit.text = receivedString
6872
dataFromAdafruit.textColor = UIColor.black
6973
}
70-
74+
7175

7276
}

0 commit comments

Comments
 (0)