0

I'm trying to translate an Android app I developed that finds all peripherals around the central and returns them to the user. Then the user can connect to one (or more) of them to do other things.

Problem: Once a device gets connected with CoreBluetooth, it is no longer returned during a scan. It should be re-acquired through the known devices list that does not give any information about the vicinity of the devices returned.

Is there a simpler way than trying to connect to each known device to know if it is in range ?

EDIT: Known devices are indeed returned during a scan, I was working on BLE devices that did not have the same advertising name and GAPP name, which confused me when rescanning after the first connection to a device :)

Louis
  • 1,913
  • 2
  • 28
  • 41
  • if you've been connected but lost the connection because of the range, you can get a notification. can't you use that? – ladislas Dec 16 '16 at 13:19

3 Answers3

0

You know a device is in range if you can see it advertising.

There are multiple forms of advertising:

  • Connectable
  • Non-connectable
  • Connectable Directed (Whitelist of devices the peripheral is willing to connect to)
  • Non Connectable directed (Whitelist of devices that can see when this peripheral is advertising, even though they can't connect to it)

Some devices only advertise a subset of the services they offer once you connect. The reason for this is there are only 31 bytes of advertising data that you can send, and if you use a Bluetooth standard Service, it'll be two bytes for each service. if you use a custom service, it's 16 bytes just to advertise that service!

It can also advertise its list of services, so you know what you can use to connect to it to do.

That means that you should parse the advertising data returned; doing this is in the scope of this Stack Overflow question.

Community
  • 1
  • 1
George Stocker
  • 57,289
  • 29
  • 176
  • 237
0

I use this in Swift 3

when starting the app

 func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    let perip = peripheral.identifier.uuidString

    if !perip.isEmpty {

        if peripheral == myPeriperal {


            print("myPeriperal Ok")
        }


    }else{
        print("no ble")

    }

}

and after

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {


    centralManager.connect(myPeriperal, options: nil)

}
Rob
  • 2,649
  • 3
  • 27
  • 34
0

You can find the range of your near by peripheral devices using RSSI(Received Signal Strength Indicator)

RSSI Value Range from 0 To 100(dbm) with newer chipsets. in older chipsets RSSI used to range from 0 to 256(dbm)

Here is the code to determine RSSI to find how

var RSSIs = [NSNumber]() // Define property in viewcontroller to store near by Peripherals RSSI value in array

 func centralManager(_ central: CBCentralManager, didDiscover peripheral: 
CBPeripheral,advertisementData: [String : Any], rssi RSSI: NSNumber) {

    self.RSSIs.append(RSSI)
    }

If RSSI Value is ranges around 50 to 60 than Peripheral is in good range for communication. If RSSI value is around 80 or more you have poor range of communication.

Mehul D
  • 140
  • 2
  • 10