6

I would like to include Bluetooth feature on my app using swift. I want to list all nearby/discoverable peripheral devices including those devices are already paired

Which method i should use for listing the paired devices. Im using CoreBlutooth framework for implementing Bluetooth availability check. If Bluetooth works fine i would like to list out paired devices. And if possible please provide the method for connecting the device directly from the listed paired devices

 func startUpCentralManager() {
    println("Initializing central manager")
    centralManager = CBCentralManager(delegate: self, queue: nil)


}



func centralManagerDidUpdateState(central: CBCentralManager!) {
    println("\(__LINE__) \(__FUNCTION__)")
     println("checking state")

    if central.state != .PoweredOn {
        // In a real app, you'd deal with all the states correctly
                    return
    }
    central.scanForPeripheralsWithServices(nil,options:nil)


}

//this method is not triggering

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

    var localname: NSString = advertisementData[CBAdvertisementDataLocalNameKey]! as NSString

    println("Discovered\(peripheral.name)")

    if localname != " "
    {
        centralManager!.stopScan()
        //self.peripheral = peripheral
        peripheral.delegate = self
        centralManager!.connectPeripheral(peripheral, options: nil)


    }


}

is these methods are necessary to show the near by peripherals if not which methods and codes to be implemented

Thanks in Advance

SARATH SASI
  • 1,395
  • 1
  • 15
  • 39

1 Answers1

5

CoreBluetooth only allows you to access Bluetooth Low Energy devices. You can pair these devices if you need encryption but typically you don't.

However, there is no API to list the paired devices - you need to discover the device(s) that are offering the service you are interested in and present a list, if necessary using your own UI in your app. Once you have identified the target device you can initiate a connection.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • its ok im new to the ios development. Can you pleas help with the (swift) method that can be discover the Low Energy Devices that are nearby. and the code to connect with these device -thank you – SARATH SASI Feb 05 '15 at 13:54
  • I'm afraid that is too broad for SO. You can refer to the Core Bluetooth Programming guide from Apple and the Core Bluetooth Framework documentation – Paulw11 Feb 05 '15 at 20:02
  • Actually i need only the method to see the discoverable peripherals and method to list them – SARATH SASI Feb 06 '15 at 05:10
  • 2
    In summary you instantiate a CBCentralManager - when you get a callback to the state delegate method that it is powered on then you can start a scan - either for all peripherals or peripherals offering specific services. For each discovered peripheral your delegate method will be called – Paulw11 Feb 06 '15 at 05:24
  • Your state check should be `==.PoweredOn`, not `!=` – Paulw11 Feb 06 '15 at 11:05
  • 1
    Actually, sorry, your code was correct with the != - I didn't read it properly! Is your didUpdateState method called? Do you have any BLE peripherals around? Try the LightBlue app from the App Store - see if it can detect the peripheral – Paulw11 Feb 06 '15 at 11:17
  • no. only i have added these methods. im not sure about the methods to be included. im having peripherals around me like Bluetooth device. but these method not yet triggered to see the peripherals around me `func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!)` – SARATH SASI Feb 06 '15 at 12:00
  • Try the LightBlue app to confirm the devices are BLE, not Bluetooth 2.1. – Paulw11 Feb 06 '15 at 12:22
  • 1
    but the method is not yet called to show the peripherals. The device showing in my normal iPhone settings. `func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) { var localname: NSString = advertisementData[CBAdvertisementDataLocalNameKey]! as NSString println("Discovered\(peripheral.name)") } ` i have run the app both in iPhone and i Pad but method not yet called. i think code has some issue – SARATH SASI Feb 06 '15 at 12:34
  • 1
    If the device is appearing in your iPhone settings then the chances are it is not a BLE device. What sort of device is it? Try the free LightBlue app - your code looks correct – Paulw11 Feb 06 '15 at 12:37
  • no its a normal bluetooth headset. i would like to see the iphones or ipads around me. is it possible with CoreBlutooth Framework ? – SARATH SASI Feb 06 '15 at 12:43
  • 1
    You cannot work with Bluetooth headsets using Core Bluetooth. You can pair them with the settings and then route audio using the AVAudioSession framework. With CoreBluetooth you can communicate with other iOS devices but you need an app running on the other device to act as a peripheral. – Paulw11 Feb 06 '15 at 12:44
  • ok thats enough for me. Actually i want to communicate with other iOS devices. Just i want see the iOS device around me and i want to list them. for this which method or codes to be included in this app. By these above pasted code its only checking whether the Bluetooth is on or not and then the method for listing the peripherals is not calling-Thank you – SARATH SASI Feb 06 '15 at 12:48
  • You can't list other iOS devices unless you have an app running on those devices to act as the peripheral. – Paulw11 Feb 06 '15 at 12:57
  • but i have one doubt. its works when LightBlue installed on one device. is it possible to discover our application is around us(with in the range). is it possible to discover with same app ? – SARATH SASI Feb 06 '15 at 13:02
  • which method can be used to act our device as an peripheral -thank you – SARATH SASI Feb 06 '15 at 13:05
  • 1
    light blue can act as a virtual peripheral for testing as well as discovering peripherals. I don't really use Swift, so I can't point you to example code on how to act as a CoreBluetooth peripheral but there is Objective-C sample code around – Paulw11 Feb 06 '15 at 13:06
  • which method can be used to implement our app act like as LightBlue works. i thing objective c methods will be available in swift also. – SARATH SASI Feb 06 '15 at 13:18
  • Yes, the frameworks are the same. You need to set up a CBPeripheralManager, create a service and one or more characteristics and then advertise the service. – Paulw11 Feb 06 '15 at 13:26
  • Here I answered how to deal with paired devices: https://stackoverflow.com/questions/34592179/how-get-the-list-of-paired-bluetooth-devices-in-swift/51456455#51456455 – Wojciech Kulik Jul 21 '18 at 17:08