2 Replies Latest reply on Jul 28, 2018 3:56 PM by AnCo_2736831

    BLE Connection with iOS Device

    AnCo_2736831

      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

        • 1. Re: BLE Connection with iOS Device
          DheerajK_81

          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

          • 2. Re: BLE Connection with iOS Device
            AnCo_2736831

            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

            1 of 1 people found this helpful