Skip to content

Commit

Permalink
fix(ble): fix infinite w8 on iOS when ble isn't available
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 18, 2018
1 parent 0705711 commit f7c4b81
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
8 changes: 5 additions & 3 deletions client/react-native/ios/Berty/Info.plist
Expand Up @@ -6,6 +6,8 @@
<string>en</string>
<key>CFBundleDisplayName</key>
<string>Berty</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Berty would like to access your bluetooth</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
Expand All @@ -32,7 +34,7 @@
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string/>
<string></string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
Expand All @@ -49,9 +51,9 @@
<key>NSCameraUsageDescription</key>
<string>Berty would like to use your camera</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string/>
<string></string>
<key>NSMicrophoneUsageDescription</key>
<string>Berty would like to your microphone (for videos)</string>
<string>Berty would like to use your microphone (for videos)</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Berty would like to save photos to your photo gallery</string>
<key>NSPhotoLibraryUsageDescription</key>
Expand Down
8 changes: 3 additions & 5 deletions core/network/ble/BertyPeripheralManagerDelegate.m
Expand Up @@ -70,7 +70,7 @@ - (void)peripheralManagerDidUpdateState:(nonnull CBPeripheralManager *)periphera
*/
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(nullable NSError *)error {
if (error) {
NSLog(@"peripheralManagerDidStartAdvertising errorCode: %ld domain: %@ userInfo: %@", error.code, error.domain, error.userInfo);
NSLog(@"peripheralManagerDidStartAdvertising errorCode: %ld domain: %@ userInfo: %@", (long)error.code, error.domain, error.userInfo);
return;
}
NSLog(@"peripheralManagerDidStartAdvertising success");
Expand Down Expand Up @@ -193,7 +193,7 @@ - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteReque
} else if ([request.characteristic.UUID isEqual:utils.maUUID]) {
[peripheral respondToRequest:request withResult:CBATTErrorSuccess];
NSString *ma = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
NSLog(@"val %@ %@ %lu %@ %@", ma, [request.characteristic.UUID UUIDString], [bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse], bDevice, bDevice.ma);
NSLog(@"val %@ %@ %lu %@ %@", ma, [request.characteristic.UUID UUIDString], (long)[bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse], bDevice, bDevice.ma);
if (bDevice.ma != nil) {
bDevice.ma = [NSString stringWithFormat:@"%@%@", bDevice.ma, ma];
} else {
Expand All @@ -202,16 +202,14 @@ - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteReque
} else if ([request.characteristic.UUID isEqual:utils.peerUUID]) {
[peripheral respondToRequest:request withResult:CBATTErrorSuccess];
NSString *peerID = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
NSLog(@"val %@ %@ %lu %@ %@", peerID, [request.characteristic.UUID UUIDString], [bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse], bDevice, bDevice.peerID);
NSLog(@"val %@ %@ %lu %@ %@", peerID, [request.characteristic.UUID UUIDString], (long)[bDevice.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse], bDevice, bDevice.peerID);
if (bDevice.peerID != nil) {
bDevice.peerID = [NSString stringWithFormat:@"%@%@", bDevice.peerID, peerID];
} else {
bDevice.peerID = peerID;
}

NSLog(@"%lu", bDevice.peerID.length);
if (bDevice.peerID.length == 46) {
NSLog(@"COUNTDOWN %@", bDevice);
[bDevice.latchRdy countDown];
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions core/network/ble/ble.h
Expand Up @@ -17,8 +17,8 @@ void init(void);
int startAdvertising(void);
int startScanning(void);
int isDiscovering(void);
int centralManagerIsOn(void);
int peripheralManagerIsOn(void);
int centralManagerGetState(void);
int peripheralManagerGetState(void);
void addService(void);
void setMa(char *ma);
void setPeerID(char *peerID);
Expand Down
8 changes: 4 additions & 4 deletions core/network/ble/ble.m
Expand Up @@ -72,12 +72,12 @@ void setPeerID(char *peerID) {
[BertyUtils setPeerID:[NSString stringWithUTF8String:peerID]];
}

int centralManagerIsOn(void) {
return (int)[BertyUtils sharedUtils].CentralIsOn;
int centralManagerGetState(void) {
return centralManager.state;
}

int peripheralManagerIsOn(void) {
return (int)[BertyUtils sharedUtils].PeripharalIsOn;
int peripheralManagerGetState(void) {
return peripheralManager.state;
}

void connDevice(CBPeripheral *peripheral) {
Expand Down
50 changes: 29 additions & 21 deletions core/network/ble/darwin.go
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"time"
"unsafe"
"sync"

peer "github.com/libp2p/go-libp2p-peer"
tpt "github.com/libp2p/go-libp2p-transport"
Expand All @@ -22,6 +21,16 @@ import (
*/
import "C"

const (
CBManagerStateUnknown = iota
CBManagerStateResetting
CBManagerStateUnsupported
CBManagerStateUnauthorized
CBManagerStatePoweredOff
CBManagerStatePoweredOn
)


//export sendBytesToConn
func sendBytesToConn(bleUUID *C.char, bytes unsafe.Pointer, length C.int) {
goBleUUID := C.GoString(bleUUID)
Expand Down Expand Up @@ -108,33 +117,32 @@ func SetPeerID(peerID string) {
C.setPeerID(cPeerID)
}

func waitForOn() bool {
var wg sync.WaitGroup
func waitForOn() int {
realState := 0
cState := int(C.centralManagerGetState())
if cState == CBManagerStatePoweredOn {
realState = 1
} else if cState == CBManagerStateUnsupported {
return -1
}

centralIsOn := false
peripheralIsOn := false
wg.Add(2)
go func() {
defer wg.Done()
if int(C.centralManagerIsOn()) == 1 {
centralIsOn = true
}
}()
go func() {
defer wg.Done()
if int(C.peripheralManagerIsOn()) == 1 {
peripheralIsOn = true
}
}()
wg.Wait()
pState := int(C.peripheralManagerGetState())
if pState == CBManagerStatePoweredOn {
realState = 1
} else if pState == CBManagerStateUnsupported {
return -1
}

return centralIsOn && peripheralIsOn
return realState
}

func NewListener(lAddr ma.Multiaddr, hostID peer.ID, t *Transport) (*Listener, error) {
C.init()

for !waitForOn() {
for res := waitForOn(); res == 1; res = waitForOn() {
if res == -1 {
return nil, fmt.Errorf("error ble not supported")
}
logger().Debug("Ble centralManager or peripheralManager isn't on wait 1sec and retry")
time.Sleep(1 * time.Second)
}
Expand Down

0 comments on commit f7c4b81

Please sign in to comment.