To start scanning for nearby BLE devices, simply call scan on the adapter's central. The provided block will be called for each scan result:

import ble

  adapter := ble.Adapter
  central := adapter.central

  central.scan: | device/ble.RemoteScannedDevice |
    print "Found $device"

Here the scan will run indefinitely.

Collecting results

A scan can be used to create a list of remote devices that match a criteria, e.g. implement a specific service. A service is identified by a UUID with a select few services being assigned by Bluetooth SIG. As an example, the 16-bit UUID 0x180F represents a battery service.

The following example shows how to scan for 3 seconds for the addresses of remote devices that implement a battery service.

import ble

BATTERY-SERVICE ::= ble.BleUuid "180F"
SCAN-DURATION   ::= Duration --s=3

  adapter := ble.Adapter
  central := adapter.central

  addresses := []

  central.scan --duration=SCAN-DURATION: | device/ble.RemoteScannedDevice |
      addresses.add device.address

  print addresses

Example: Mobile phone as a BLE device

If you want to discover and find your mobile phone as a BLE device using the above Toit program, you can download a mobile app, like nRF Connect.

Download the app on your mobile phone for iOS or for Android.

The nRF Connect app allows your iOS or Android device to advertise as a BLE peripheral, as well as discovering nearby peripherals, like your Toit device.

When running the above Toit program for scanning, you will be able to see your mobile phone on the list of discovered BLE devices.