BLE Connection with iOS Device

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
AnCo_2736831
Level 4
Level 4
25 likes received 10 likes received 10 likes given

Hi All,

I have followed the Xcode BLE tutorials from Alan Hawse for how to make an iOS App using BLE (link: http://www.cypress.com/training/how-make-ios-app-control-robot-using-bluetooth-low-energy-ble

I downloaded the Xcode 8 version code from this github page:

bleapp/Xcode/BleRemote at master · cypresssemiconductorco/bleapp · GitHub

When I used Xcode 8, this program worked, but when I converted it to Xcode 9, the BLE device (Robot) would not show up in the scan for devices screen. It would just show up blank. I tried using print statements in the place where the iOS device scans for BLE devices, but the print statement doesn't show up in the output, meaning that the iOS smartphone is not finding any PSoC devices when it scans.

I may have narrowed down the cause of the problem to something called the "NotificationCenter", which has these globals:

struct RCNotifications {

    static let FoundDevice = "com.cypress.bleremote.founddevice"

    static let ConnectionComplete = "com.cypress.bleremote.connectioncomplete"

    static let DisconnectedDevice = "com.cypress.bleremote.disconnecteddevice"

   

    static let UpdatedSpeed = "com.cypress.bleremote.updatedspeed"

    static let UpdatedTach = "com.cypress.bleremote.updatedtach"

}

By the way, do any of you know what these globals mean? For I do not know why it is calling "com.cypress.bleremote.founddevice" when that is not my bundle identifier, and if I change it to be the same as my bundle identifier (com.app.bleremote), it doesn't really make a difference.

For the NotificationCenter code, this is how it looks like when I call one of these globals (in Xcode 9):

        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: RCNotifications.FoundDevice), object: nil, queue: OperationQueue.main) { _ in

            self.tableView.reloadData()

          print("Device Found")

            }

This is the line of code I mentioned earlier where I put a print statement that didn't appear in the output window.

And this is how that same code looked like in Xcode 8 before I auto-fixed everything:

NSNotificationCenter.defaultCenter().addObserverForName(RCNotifications.FoundDevice, object: nil, queue: NSOperationQueue.mainQueue()) { _ in

self.tableView.reloadData()

          print("Device Found")

}

Do any of you know what went wrong from the changes to Xcode 9?

Thanks,

Andrew Collins

0 Likes
1 Solution
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

The globals just represent the different broadcast events and doesn't seem to be the problem here because the strings are just used for comparison during callback. You can use either your bundle name or the default, it doesn't matter.

The issue might have something to do with Bluetooth advertisement process. I currently have only XCode 8. Will try to reproduce the error in XCode 9 and will debug to find the cause of the issue. Can you try putting print statements in the code where it starts searching for devices too?

Regards,

Dheeraj

View solution in original post

0 Likes
2 Replies
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

The globals just represent the different broadcast events and doesn't seem to be the problem here because the strings are just used for comparison during callback. You can use either your bundle name or the default, it doesn't matter.

The issue might have something to do with Bluetooth advertisement process. I currently have only XCode 8. Will try to reproduce the error in XCode 9 and will debug to find the cause of the issue. Can you try putting print statements in the code where it starts searching for devices too?

Regards,

Dheeraj

0 Likes

Hi Dheeraj,

Thanks for your advice! I put a print statement in this code (from Xcode 8):

    // called when you see an advertising packet

    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)

    {

        if searchDevices(peripheral) == nil {

            print("Found a new Periphal advertising motorservice")

            let newCar = RcCar(connection: BleConnection(peripheral: peripheral))

            cars.append(newCar)

            NSNotificationCenter.defaultCenter().postNotificationName(RCNotifications.FoundDevice, object: nil)

        }

    }

    // called when you see an advertising packet
    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
    {
        if searchDevices(peripheral) == nil {
            print("Found a new Periphal advertising motorservice")
            let newCar = RcCar(connection: BleConnection(peripheral: peripheral))
            cars.append(newCar)
            NSNotificationCenter.defaultCenter().postNotificationName(RCNotifications.FoundDevice, object: nil)
        }
    }

When I converted it to Xcode 9, it turns out that the auto-fix turned it into a private function, so that was the cause of the problem.

Now everything is running so smoothly. Thanks so much for your help!

With gratitude,

Andrew Collins