By Phil_G

2018-04-10 07:18:59 8 Comments

I have an app which I want to test using XCTest in Xcode. For testing, I need to scan for devices inside my Testcase to execute a specific behavior.

The problem is, that I can not find any peripherals when scanning for them, although I have code which is working fine inside the original app. Could it be that XCtest does not allow bluetooth communication triggered from the UITests?

I'm at a point, where I have no further ideas how to go on. Maybe someone has a good hint for me.

My code looks as followed: The UITests are written in Swift, and I have a Helper Class to execute Bluetooth specific code in Objective-C. So first I create this Objective-C helper class in my Swift file as a global variable:

var helper : ObjectiveCTestsHelper?

In the setup function of the UITest File, I initialize this variable:

helper = ObjectiveCTestsHelper()

In the test function, I then execute the bluetooth specific function and wait for it with a dummy expectation, so that the UITest does not terminate:

let expectation = XCTestExpectation(description: "Dummy")
self.wait(for: [expectation], timeout: 1000)

The scanForDevices function looks like this:

- (void) scanForDevices {
    self.bleScanningModel = [[BluetoothScanningModel alloc] init];

    [self.bleScanningModel startScan:50.0
                          completion:^(NSDictionary *periperals) {
                              //here no peripherals

The startScan method scans for devices:

- (void)startScan:(NSTimeInterval)period
       completion:(void (^)(NSDictionary *periperals))completion {

    if (self.isScanning) {
        int a = 3;
    self.isScanning = true;

    foundPeriperals = [[NSMutableDictionary alloc] init];

    self.completionHandler = completion;
    self.repeats = repeats;

    scanPeriodTimer = [NSTimer scheduledTimerWithTimeInterval: period
                                                       target: self
                                                     selector: @selector(periodTimerFired:)
                                                     userInfo: nil
                                                      repeats: self.repeats];

    [self checkIfBluetoothIsPoweredUp:^(BOOL success) {
            [centralManager scanForPeripheralsWithServices: nil 
                                                   options: nil];

Basically, it just checks if Bluetooth is powered on, and then scans for peripherals. After 5 Seconds, a timer is fired to get back to the startScan method to execute a function based on the found peripherals. And here is my problem. I execute scanForPeripheralsWithServices but the function didDiscoverPeripheral, is not called by the central manager when a peripheral is found, so I have no peripherals to work with.

Could it be that Bluetooth is not allowed to be executed from UITests directly?

I hope someone can help me! Thank you!


Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How to download Xcode DMG or XIP file?

  • 2012-04-26 14:41:18
  • Proud Member
  • 1316083 View
  • 981 Score
  • 1 Answer
  • Tags:   xcode dmg

22 Answered Questions

[SOLVED] Xcode error "Could not find Developer Disk Image"

  • 2015-06-09 15:48:57
  • Nathan Kramer
  • 532043 View
  • 863 Score
  • 22 Answer
  • Tags:   ios iphone xcode

6 Answered Questions

[SOLVED] Where do I find the line number in the Xcode editor?

  • 2011-05-05 17:23:42
  • William Jockusch
  • 212681 View
  • 550 Score
  • 6 Answer
  • Tags:   xcode

14 Answered Questions

[SOLVED] Xcode process launch failed: Security

12 Answered Questions

[SOLVED] Hide strange unwanted Xcode logs

20 Answered Questions

[SOLVED] Git ignore file for Xcode projects

18 Answered Questions

[SOLVED] How can I indent multiple lines in Xcode?

7 Answered Questions

[SOLVED] Version vs build in Xcode

  • 2011-07-27 21:48:15
  • chris
  • 136352 View
  • 628 Score
  • 7 Answer
  • Tags:   ios xcode

9 Answered Questions

[SOLVED] How to "add existing frameworks" in Xcode 4?

11 Answered Questions

Sponsored Content