Skip to content

Commit a97d0a8

Browse files
committed
Can send values from Arduino to iOS
1 parent 1cbcd19 commit a97d0a8

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Arduino/ZBLE/ZBLE.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ void loop() {
1010

1111
void serialEvent(){
1212
if(Serial.available() > 0){
13+
//Read the value sent to the shield
1314
String v = Serial.readString();
14-
Serial.println(v);
15+
Serial.println(v); //This will send back the value to your phone and print it
16+
//on the iOS console.
1517
}
1618
}
1719

iOS/BluetoothController/BluetoothController/Bluetooth.m

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ @implementation Bluetooth
1111
@synthesize cbcManager;
1212

1313
//Might have to change these values
14-
#define ARDUINO_SHIELD_NAME @"ZBModlue"
15-
#define SHIELD_RW_CHARACTERISTIC @"FFC1"
14+
#define ARDUINO_SHIELD_NAME @"ZBModlue" //Name of the BLE shield
15+
#define SHIELD_WRITE_CHARACTERISTIC @"FFC1" //Name of the write characteristic of the shield
16+
#define SHIELD_READ_CHARACTERISTIC @"FFC2" //Name of the read characteristic of the shield
1617

1718
//Init the Bluetooth Service
1819
-(void) start{
@@ -61,11 +62,23 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)err
6162
//In the case of the ZBModule BLE Shield, the characteristic where values have to be sent is called FFC1
6263
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
6364
for (CBCharacteristic *characteristic in service.characteristics) {
64-
if([characteristic.UUID.UUIDString isEqualToString:SHIELD_RW_CHARACTERISTIC]){
65-
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
66-
NSLog(@"Found %@ characteristic", SHIELD_RW_CHARACTERISTIC);
65+
if([characteristic.UUID.UUIDString isEqualToString:SHIELD_WRITE_CHARACTERISTIC]){
66+
NSLog(@"Found %@ characteristic (write)", SHIELD_WRITE_CHARACTERISTIC);
6767
self.characteristic = characteristic;
6868
}
69+
else if([characteristic.UUID.UUIDString isEqualToString:SHIELD_READ_CHARACTERISTIC]){
70+
NSLog(@"Found %@ characteristic (read)", SHIELD_READ_CHARACTERISTIC);
71+
//Set notifications for that characteristic
72+
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
73+
}
74+
}
75+
}
76+
77+
//Step #5: Read notifications from the characteristics (when a value is received)
78+
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
79+
if (characteristic.value != nil) {
80+
NSString *str = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
81+
NSLog(@"Received value: %@", str);
6982
}
7083
}
7184

0 commit comments

Comments
 (0)