1

Now I'm currently doing an application project that needs my iPhone to scan other nearby bluetooth devices and list them out. I'm wondering is my code has any problem?

Code:

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {

    var manager: CBCentralManager!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        manager = CBCentralManager (delegate: self, queue: nil)
    }


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

            print("Peripheral: \(peripheral)")
    }

    func centralManagerDidUpdateState(central: CBCentralManager) {
        print("Checking")
        switch(central.state)
        {
        case.Unsupported:
            print("BLE is not supported")
        case.Unauthorized:
            print("BLE is unauthorized")
        case.Unknown:
            print("BLE is Unknown")
        case.Resetting:
            print("BLE is Resetting")
        case.PoweredOff:
            print("BLE service is powered off")
        case.PoweredOn:
            print("BLE service is powered on")
            print("Start Scanning")
            manager.scanForPeripheralsWithServices(nil, options: nil)
        default:
            print("default state")
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }  
}

I'm using iPhone 5 (iOS 9) and I'm sure that my Bluetooth is turned on.

When I run the application in my iPhone, the console only log the following output:

Checking
BLE service is powered on
Start Scanning

But there is no Bluetooth device's name shown in the output. Even I turn on my iPad (iPad Mini 4 iOS 8) and the list still wouldn't update.

Sometimes it does scan my MacBook Pro Bluetooth and the output will have this:

Peripheral: <CBPeripheral: 0x14d70e00, identifier = 54738076-6C97-FD04-18CF-5E1AF6705865, name = vivien’s MacBook Pro, state = disconnected>

So, why is this happening? Can someone please explain to me?

vivien
  • 11
  • 3
  • If there is no app running on the ipad to act as a BLE peripheral then there will be nothing for your app to find. You can use the LightBlue app to advertise a peripheral such as a heart rate monitor – Paulw11 Dec 08 '15 at 05:04
  • @Paulw11 as what you said, my iPhone able to scan my iPad when I add virtual peripheral in LightBlue app. If delete the virtual peripheral, then it can't scan my iPad again. I thought that all phones, tablets are peripheral devices, aren't they? So, to enable scanning other iPhones and iPads, I need to run the LightBlue app at background? – vivien Dec 08 '15 at 11:04
  • To save battery iOS only advertises when it needs to; ie when an app is advertising a peripheral – Paulw11 Dec 08 '15 at 11:54
  • @Paulw11 So no matter how I still need to run the LightBlue app at background, right? Or is there any other way? – vivien Dec 11 '15 at 03:14
  • Well, you need some app advertising in the background for your device to be discoverable – Paulw11 Dec 11 '15 at 03:19
  • Is there any other way to force my app to advertise? – vivien Dec 11 '15 at 13:47
  • Your app can advertise by implementing a CBPeripheral through CBPeripheralManager. It can do this in the background. Then another instance of your app running on another device could discover the first device. – Paulw11 Dec 11 '15 at 13:58
  • @Paulw11 can my app runs both? I mean act as peripheral and also central? – vivien Dec 11 '15 at 15:12
  • does it work for a while when you reset the device? There are 5Ss out there that do this when you go into the background without manually locking the screen – Saik Caskey Jun 26 '17 at 10:02

1 Answers1

1

case 1:
You must use GKSession to scan and connect with another iOS device,not CoreBluetooth.
case 2:
Your bluetooth device is a Bluetooth 3.0 accessory.Your iPhone can discover and show it in Setting->Bluetooth.
But this message isn't delivered to your app,so your app won't discover it.
Try again with a Bluetooth 4.0 accessory.

wj2061
  • 6,778
  • 3
  • 36
  • 62
  • But even in my iPhone Setting>Bluetooth discovered list, my iPad mini and other bluetooth 4.0 devices like Nexus 5, didn't show up. So the problem now, I wanted to scan and list all other bluetooth devices only (no pairing). Can this be done using the GKSession you mentioned? – vivien Dec 08 '15 at 02:32
  • @vivien You can download LightBlue App in app Store.it's a great tool for CoreBluetooth debugging. GKSession is useful when you try to connect two iOS devices not all bluetooth devices. – wj2061 Dec 08 '15 at 02:36
  • I'd downloaded. Still only my MacBook appeared in the "peripheral nearby" list. How does a device is counted peripheral? Is it related to the peripheral thingy so that my app's scan list only shows my Macbook instead of other bluetooth devices nearby? – vivien Dec 08 '15 at 02:47
  • case1: your bluetooth device is broken.case2: your bluetooth device is already connectted to another device,so it won't broadcast ,and you can't discover.case 3: your bluetooth don't change state to discoverable. – wj2061 Dec 08 '15 at 02:49