3

I am currently using swift to make an app that can generate a list of bluetooth devices nearby. However, I cannot find any documents that use swift to do that. All of the files are in Objective C and I am wondering if I can just make an objective C file and connect directly to the storyboard? (my project file is in swift).

Also, do I have to include any other library from outside? (ex: serialGATT) or coreBluetooth.framework is good enough?

Caroline
  • 31
  • 1
  • 2
  • You will only be able to discover BLE devices. For this you can use the CoreBluetooth library. Implementing the CbCentralManager delegate functions in Swift should be pretty straightforward. Refer to the Core Bluetooth Programming guide and the CBCentralManager class reference e – Paulw11 Jun 22 '15 at 07:58
  • Now i have set up the bluetooth, and now my BLE device is sending image data. what API function can i use to receive data from the BLE device? In the end I still use objective C instead of swift – Caroline Jun 24 '15 at 09:09
  • With CoreBluetooth the only data transfer services you have a readCharacteristic/writeCharacteristic. You will need to break your image down to 20 byte packets. – Paulw11 Jun 24 '15 at 09:26
  • can you say more details about it? so you mean every time Bluetooth can only send 20 bytes of data? – Caroline Jun 24 '15 at 09:37
  • readValueForCharacteristic: /readValueForDescriptor: I found these two API but I'm not sure which one should I use in my case. I just care about the data that the BLE device is sending (it is a camera) – Caroline Jun 24 '15 at 09:40
  • That is correct; Using GATT (The only profile supported by Core Bluetooth) you need to send approximately 20 bytes at a time. For example - https://developer.apple.com/library/ios/samplecode/BTLE_Transfer/Introduction/Intro.html – Paulw11 Jun 24 '15 at 09:41
  • answered here https://stackoverflow.com/questions/40636726/nearby-bluetooth-devices-using-swift-3-0 – Dylan Mar 08 '17 at 14:44

1 Answers1

1

You'll have to import CoreBluetooth

import CoreBluetooth

Add the CBCentralManagerDelegate to your controller. (For a simple app I attached it to my View Controller)

    class ViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDelegate {

You should create a local variable centralManager (or similar) and then initialize in your viewDidLoad function

    centralManager = CBCentralManager(delegate: self, queue: nil)

Finally you can create a new function called centralManagerDidUpdateState which will act as a callback when the Bluetooth state changes (it's always called on startup in your app.

    // If we're powered on, start scanning
        func centralManagerDidUpdateState(_ central: CBCentralManager) {
            print("Central state update")
            if central.state != .poweredOn {
                print("Central is not powered on")
            } else {
                print("Central scanning for", ParticlePeripheral.particleLEDServiceUUID);
                centralManager.scanForPeripherals(withServices: [ParticlePeripheral.particleLEDServiceUUID],
                                                  options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
            }
        }

The important call is centralManager.scanForPeripherals This will start the scanning process. In my case I'm filtering devices that only have ParticlePeripheral.particleLEDServiceUUID in their advertising packets.

That should get you scanning and on your way. I wrote a full end-to-end tutorial on how to use Swift with Bluetooth. It will go into much more detail. Here it is.

jaredwolff
  • 777
  • 8
  • 13