zephyr/samples/bluetooth/observer/src/main.c
Yeshvanth M a1947006d6 samples: Bluetooth: Added Observer sample
This sample scans for nearby BLE devices and prints the address of the
device found and the RSSI value to the UART terminal.

Uses passive scanning and demonstrates the role of the observer.

Signed-off-by: Yeshvanth M <yeshvanthmuniraj@gmail.com>
2021-11-25 17:09:32 +02:00

54 lines
1.1 KiB
C

/* main.c - Application main entry point */
/*
* Copyright (c) 2015-2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/types.h>
#include <stddef.h>
#include <sys/printk.h>
#include <sys/util.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
struct net_buf_simple *ad)
{
char addr_str[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
}
void main(void)
{
struct bt_le_scan_param scan_param = {
.type = BT_LE_SCAN_TYPE_PASSIVE,
.options = BT_LE_SCAN_OPT_FILTER_DUPLICATE,
.interval = BT_GAP_SCAN_FAST_INTERVAL,
.window = BT_GAP_SCAN_FAST_WINDOW,
};
int err;
printk("Starting Observer\n");
/* Initialize the Bluetooth Subsystem */
err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}
printk("Bluetooth initialized\n");
err = bt_le_scan_start(&scan_param, device_found);
printk("\n");
if (err) {
printk("Starting scanning failed (err %d)\n", err);
return;
}
}