1

I'm currently trying to make 2 iOS devices connect with each other so I can send gps locations between the two. Right now I'm using corebluetooth to achieve this.

What I did so far is I created 2 separate projects, one being the receiver one being the sender. I followed some instructions from apple itself to broadcast the iPad. However it doesn't seem to be found by the project that is trying to search for Bluetooth devices. I ran both apps on separate physical devices with bluetooth turned on. Below the code that I've used (keep in mind this is some quick code I wrote to try and see if I can actually find the device.) This is also the first time I work with bluetooth so it's a learning curve for sure.

FYI: I'm planning on using the GATT protocol. I'm also wondering if it can be done without a pairing handshake in the way that I get a popup to accept the connection. I would like the receiver to automatically receive and broadcasted signal and start obtaining the data that is being sent without additional steps needed. Or would I need something like Multipeer Connectivity Framework for this?

Receiver

import UIKit
import CoreBluetooth
import CoreLocation

class ViewController: UIViewController {
    private let uuidBeacon = "A26962FC-F9DD-4251-B339-A25B3E808213"
    var peripheral: CBPeripheralManager!
    var peripheralData: NSMutableDictionary!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        peripheral = CBPeripheralManager(delegate: self, queue: nil)
    }

    override func viewDidAppear(_ animated: Bool) {
        guard let region = createBeaconRegion() else { return }
        advertiseDevice(region: region)
    }

    private func createBeaconRegion() -> CLBeaconRegion? {
        let proximityUUID = UUID(uuidString: uuidBeacon)
        let major : CLBeaconMajorValue = 100
        let minor : CLBeaconMinorValue = 1
        let beaconID = "com.example.myDeviceRegion"

        return CLBeaconRegion(proximityUUID: proximityUUID!,
                              major: major, minor: minor, identifier: beaconID)
    }

    func advertiseDevice(region : CLBeaconRegion) {
        peripheralData = region.peripheralData(withMeasuredPower: nil)
    }

}

extension ViewController: CBPeripheralManagerDelegate {

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        switch peripheral.state {
        case .poweredOn:
            peripheral.startAdvertising(((peripheralData as NSDictionary) as! [String : Any]))
        case .poweredOff:
            break

        //            peripheral.stopAdvertising()
        default:
            break
        }
    }

}

Broadcaster

import UIKit
import CoreBluetooth
import CoreLocation

class ViewController: UIViewController {
    var centralManager: CBCentralManager!
    var sensorTag: CBPeripheral?
    var keepScanning: Bool = false

    private var timerScanInterval: TimeInterval = 5

    override func viewDidLoad() {
        super.viewDidLoad()

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

}

extension ViewController: CBCentralManagerDelegate {

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .poweredOn:
            keepScanning = true
            _ = Timer(timeInterval: timerScanInterval, target: self, selector: #selector(pauseScan), userInfo: nil, repeats: false)
            centralManager.scanForPeripherals(withServices: nil, options: nil)
            break
        case .poweredOff:
            break
        case .unsupported:
            break
        case .unauthorized:
            break
        case .resetting:
            break
        case .unknown:
            break
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        peripheral.discoverServices(nil)
        if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
            print(peripheralName)
        }
    }

    @objc private func pauseScan() {
        centralManager.stopScan()
        _ = Timer(timeInterval: 2, target: self, selector: #selector(resumeScan), userInfo: nil, repeats: false)
    }

    @objc private func resumeScan() {
        if keepScanning {
            centralManager.scanForPeripherals(withServices: nil, options: nil)
            _ = Timer(timeInterval: timerScanInterval, target: self, selector: #selector(pauseScan), userInfo: nil, repeats: false)
        }
    }

}

extension ViewController: CBPeripheralDelegate {

}
NoSixties
  • 2,443
  • 2
  • 28
  • 65
  • Are you getting any error? – guru Aug 06 '18 at 09:56
  • No error as far as I can tell it just doesn't seem to find the specific device – NoSixties Aug 06 '18 at 10:16
  • I am not sure about your problem but you can check this link: https://stackoverflow.com/questions/32140863/ios-diddiscoverperipheral-not-called-in-background-mode – guru Aug 06 '18 at 10:20
  • I'm not trying to do it in background mode but it's worth a shot – NoSixties Aug 06 '18 at 10:26
  • You seem to be confused between advertising an iBeacon and advertising a CBPeripheral with a service and an attribute. Your first block of code does the former while your second block of code is looking for the latter. I am also a little confused about your overall goal; two devices that are close enough to communicate via Bluetooth are basically in the same location from a GPS perspective – Paulw11 Aug 06 '18 at 10:34
  • @Paulw11 But the iBeacon is still being advertised over bluetooth I figured that would mean it should still be detectable. The goal is that a user could have a iPad Wi-Fi only. – NoSixties Aug 06 '18 at 10:44
  • If you want to discover an iBeacon then you use Core Location to look for a CLBeaconRegion. Wifi is separate to Bluetooth but both have a fairly limited range; for the two devices to be connected directly by either they would need to be within about 30m of each other – Paulw11 Aug 06 '18 at 10:46

0 Answers0