app_main.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "common\timer.h"
  9. static uint8_t mfg_data[] = {0xff, 0xff, 0x00};
  10. static const struct bt_data ad[] = {
  11. BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, 3),
  12. };
  13. struct k_timer adv_work_timer;
  14. void start_adv_process(void)
  15. {
  16. int err;
  17. printk("Sending advertising data: 0x%02X\n", mfg_data[2]);
  18. /* Start advertising */
  19. err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad), NULL, 0);
  20. if (err)
  21. {
  22. printk("Advertising failed to start (err %d)\n", err);
  23. return;
  24. }
  25. }
  26. void stop_adv_process(void)
  27. {
  28. int err;
  29. err = bt_le_adv_stop();
  30. if (err)
  31. {
  32. printk("Advertising failed to stop (err %d)\n", err);
  33. return;
  34. }
  35. mfg_data[2]++;
  36. }
  37. uint8_t current_work_state;
  38. void adv_work_timer_expiry(struct k_timer *timer)
  39. {
  40. current_work_state++;
  41. printk("adv_work_timer_expiry, current_work_state: %d\n", current_work_state);
  42. if (current_work_state % 0x02 == 0)
  43. {
  44. start_adv_process();
  45. }
  46. else
  47. {
  48. stop_adv_process();
  49. }
  50. }
  51. void bt_ready(int err)
  52. {
  53. if (err)
  54. {
  55. printk("Bluetooth init failed (err %d)\n", err);
  56. return;
  57. }
  58. printk("Bluetooth initialized\n");
  59. k_timer_init(&adv_work_timer, adv_work_timer_expiry, NULL);
  60. k_timer_start(&adv_work_timer, K_SECONDS(1), K_SECONDS(1));
  61. }
  62. void app_polling_work(void)
  63. {
  64. return;
  65. }