Skip to content

Commit

Permalink
fix(ObjC): rework when conn are made and berty device are created
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Dec 12, 2018
1 parent 3184ac7 commit adf1e49
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
12 changes: 8 additions & 4 deletions core/network/ble/BertyCentralManagerDelegate.m
Expand Up @@ -87,11 +87,13 @@ - (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central {
*
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
if (![BertyUtils inDevices:peripheral]) {
// NSLog(@"centralManger: central didDiscoverPeripheral");
BertyDevice *bDevice = [BertyUtils getDevice:peripheral];
if (bDevice == nil) {
NSLog(@"centralManger: central didDiscoverPeripheral: %@ RSSI %@ advertisementData: %@", [peripheral.identifier UUIDString], [RSSI stringValue], advertisementData);
[peripheral setDelegate:self.peripheralDelegate];
BertyDevice *device = [[BertyDevice alloc] initWithPeripheral:peripheral withCentralManager:central];
[BertyUtils addDevice:device];
bDevice = [[BertyDevice alloc] initWithPeripheral:peripheral withCentralManager:central];
[BertyUtils addDevice:bDevice];
[central connectPeripheral:peripheral options:nil];
}
}
Expand All @@ -107,11 +109,13 @@ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeri
*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"centralManger: central didConnectPeripheral: %@ %@", [peripheral.identifier UUIDString], peripheral.delegate);
[peripheral setDelegate:self.peripheralDelegate];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
BertyDevice *bDevice = [BertyUtils getDevice:peripheral];
if (bDevice == nil) {
NSLog(@"centralManger: central didConnectPeripheral error unknown peripheral connected");
return;
bDevice = [[BertyDevice alloc] initWithPeripheral:peripheral withCentralManager:central];
[BertyUtils addDevice:bDevice];
}
dispatch_semaphore_signal(bDevice.connSema);
});
Expand Down
2 changes: 1 addition & 1 deletion core/network/ble/BertyPeripheralDelegate.m
Expand Up @@ -167,7 +167,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(C
* @discussion This method returns the result of a {@link writeValue:forCharacteristic:type:} call, when the <code>CBCharacteristicWriteWithResponse</code> type is used.
*/
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
NSLog(@"peripheral: %@ didWriteValueForCharacteristic: %@", [peripheral.identifier UUIDString], [characteristic.UUID UUIDString]);
// NSLog(@"peripheral: %@ didWriteValueForCharacteristic: %@", [peripheral.identifier UUIDString], [characteristic.UUID UUIDString]);
BertyDevice *bDevice = [BertyUtils getDevice:peripheral];
BertyUtils *utils = [BertyUtils sharedUtils];
if (bDevice == nil) {
Expand Down
50 changes: 34 additions & 16 deletions core/network/ble/BertyPeripheralManagerDelegate.m
Expand Up @@ -225,41 +225,59 @@ - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteReque
int i = 0;
for (CBATTRequest *request in requests) {

NSLog(@"peripheralManager:peripheral didReceiveWriteRequests: %@ %d", request.central.identifier, i);
// NSLog(@"peripheralManager:peripheral didReceiveWriteRequests: %@ %d", request.central.identifier, i);
i += 1;
BertyDevice *bDevice = [BertyUtils getDeviceFromRequest:request];
BertyUtils *utils = [BertyUtils sharedUtils];
if (bDevice == nil) {
NSLog(@"peripheral: write error unknown peripheral connected");
[peripheral respondToRequest:request withResult:CBATTErrorRequestNotSupported];
return;
CBPeripheral *cliPeripheral = nil;
NSArray<CBPeripheral *> *cliPeripherals = [centralManager retrieveConnectedPeripheralsWithServices:@[[BertyUtils sharedUtils].serviceUUID]];
for (CBPeripheral *p in cliPeripherals) {
if ([[p.identifier UUIDString] isEqualToString:[request.central.identifier UUIDString]]) {
cliPeripheral = p;
break;
}
}

if (cliPeripheral == nil) {
NSLog(@"error didReceiveWriteRequests");
[peripheral respondToRequest:request withResult:CBATTErrorRequestNotSupported];
return;
}
[cliPeripheral setDelegate:self.peripheralDelegate];

bDevice = [[BertyDevice alloc] initWithPeripheral:cliPeripheral withCentralManager:centralManager];
[BertyUtils addDevice:bDevice];
@try {
[centralManager connectPeripheral:cliPeripheral options:nil];
} @catch (NSException *exception) {
NSLog(@"didReceiveWriteRequests connect failed");
} @finally {
NSLog(@"didReceiveWriteRequests connect called");
}
}
if ([request.characteristic.UUID isEqual:utils.writerUUID]) {
sendBytesToConn([bDevice.ma UTF8String], [request.value bytes], (int)[request.value length]);
[peripheral respondToRequest:request withResult:CBATTErrorSuccess];
} else if ([request.characteristic.UUID isEqual:utils.maUUID]) {
char *value = [request.value bytes];
value[[bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse]] = 0;
[peripheral respondToRequest:request withResult:CBATTErrorSuccess];
NSLog(@"val %@ %@ %lu %@ %@", [NSString stringWithUTF8String:value], [request.characteristic.UUID UUIDString], [bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse], bDevice, bDevice.ma);
NSString *ma = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
NSLog(@"val %@ %@ %lu %@ %@", ma, [request.characteristic.UUID UUIDString], [bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse], bDevice, bDevice.ma);
if (bDevice.ma != nil) {
bDevice.ma = [NSString stringWithFormat:@"%@%@", bDevice.peerID, [NSString stringWithUTF8String:value]];
bDevice.ma = [NSString stringWithFormat:@"%@%@", bDevice.ma, ma];
} else {
bDevice.ma = [NSString stringWithUTF8String:value];
bDevice.ma = ma;
}

} else if ([request.characteristic.UUID isEqual:utils.peerUUID]) {
[peripheral respondToRequest:request withResult:CBATTErrorSuccess];
char *value = [request.value bytes];
value[[bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse]] = 0;
NSLog(@"val %@ %@ %lu %@ %@", [NSString stringWithUTF8String:value], [request.characteristic.UUID UUIDString], [bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse], bDevice, bDevice.peerID);
NSString *peerID = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
NSLog(@"val %@ %@ %lu %@ %@", peerID, [request.characteristic.UUID UUIDString], [bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse], bDevice, bDevice.peerID);
if (bDevice.peerID != nil) {
bDevice.peerID = [NSString stringWithFormat:@"%@%@", bDevice.peerID, [NSString stringWithUTF8String:value]];
bDevice.peerID = [NSString stringWithFormat:@"%@%@", bDevice.peerID, peerID];
} else {
bDevice.peerID = [NSString stringWithUTF8String:value];
bDevice.peerID = peerID;
}


NSLog(@"%lu", bDevice.peerID.length);
if (bDevice.peerID.length == 46) {
NSLog(@"COUNTDOWN %@", bDevice);
Expand Down

0 comments on commit adf1e49

Please sign in to comment.