app_main.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include "base/types.h"
  4. #include <bluetooth/bluetooth.h>
  5. #include <bluetooth/hci.h>
  6. #include <drivers/hci_driver.h>
  7. #include <logging/bt_log_impl.h>
  8. #define DEVICE_NAME CONFIG_BT_DEVICE_NAME
  9. #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
  10. /*
  11. * Set Advertisement data. Based on the Eddystone specification:
  12. * https://github.com/google/eddystone/blob/master/protocol-specification.md
  13. * https://github.com/google/eddystone/tree/master/eddystone-url
  14. */
  15. static const struct bt_data ad[] = {
  16. BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
  17. BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe),
  18. BT_DATA_BYTES(BT_DATA_SVC_DATA16, 0xaa, 0xfe, /* Eddystone UUID */
  19. 0x10, /* Eddystone-URL frame type */
  20. 0x00, /* Calibrated Tx power at 0m */
  21. 0x00, /* URL Scheme Prefix http://www. */
  22. 'z', 'e', 'p', 'h', 'y', 'r', 'p', 'r', 'o', 'j', 'e', 'c', 't',
  23. 0x08) /* .org */
  24. };
  25. /* Set Scan Response data */
  26. static const struct bt_data sd[] = {
  27. BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
  28. };
  29. void bt_ready(int err)
  30. {
  31. char addr_s[BT_ADDR_LE_STR_LEN];
  32. bt_addr_le_t addr = {0};
  33. size_t count = 1;
  34. if (err)
  35. {
  36. printk("Bluetooth init failed (err %d)\n", err);
  37. return;
  38. }
  39. printk("Bluetooth initialized\n");
  40. /* Start advertising */
  41. err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
  42. if (err)
  43. {
  44. printk("Advertising failed to start (err %d)\n", err);
  45. return;
  46. }
  47. /* For connectable advertising you would use
  48. * bt_le_oob_get_local(). For non-connectable non-identity
  49. * advertising an non-resolvable private address is used;
  50. * there is no API to retrieve that.
  51. */
  52. bt_id_get(&addr, &count);
  53. bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s));
  54. printk("Beacon started, advertising as %s\n", addr_s);
  55. }
  56. void app_polling_work(void)
  57. {
  58. return;
  59. }