Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect other iPhones nearby using Swift

Tags:

ios

swift

I am trying to build an app that is able to detect another iPhone with the same app within a certain proximity. I don't want to use location. Is this possible?

like image 372
Matthew Morris Avatar asked Apr 23 '18 21:04

Matthew Morris


2 Answers

Assuming you're looking for devices pretty close to each other, generally on the order of a single room, maybe a bit further if you're lucky (sometimes quite a lot less if you're not), what you're looking for is Bluetooth LE (Low Energy).

  • Choose a service UUID (type uuidgen from the commandline; it'll make you one)
  • Using CBPeripheralManager, advertise your service ID (see startAdvertising)
  • Using CBCentralManager, search for the same service ID (see scanForPeripherals)
  • Register yourself as a background Bluetooth app, both as bluetooth-central and bluetooth-periphral (see Core Bluetooth Background Execution Modes for details)

With this setup, your app will advertise your service, and your app will automatically be launched anytime the phone sees something advertising the service.

See the Core Bluetooth Programming Guide for details. Bluetooth development is a bit finicky and there are lots of little corner cases you often need to deal with, but the basics are pretty straightforward, especially for this kind of use case.

If you just want to find people who are actively (in the foreground) using your app and communicate with them, take a look at GameKit. It's built on top of Core Bluetooth, but does a ton of the work for you. It's especially designed for getting people together to play games, but it can do a lot more than that.

One note: Do not confuse Bluetooth with Bluetooth LE. They are radically different and basically unrelated protocols. Bluetooth is used for things likes streaming music, and you have basically no access to it at all in iOS. BLE is used for exchanging small bits of data with low-power peripherals like heart rate monitors and the like. Core Bluetooth only deals with BLE.

like image 112
Rob Napier Avatar answered Sep 18 '22 11:09

Rob Napier


You could also use iBeacons for this (An abstraction on top of BLE). iBeacons are a little easier to use than BLE, and like Core Bluetooth you can set up beacon region monitoring so your app will get launched if a beacon (or group of beacons) you are listening for comes into range. (But beacon monitoring uses the location manager)

like image 39
Duncan C Avatar answered Sep 22 '22 11:09

Duncan C