|
| 1 | +// Nefry Eddystone |
| 2 | +// Copyright(C) 2017 wami |
| 3 | +// |
| 4 | +// This program is free software : you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program.If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +#include "sdkconfig.h" |
| 18 | + |
| 19 | +#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) |
| 20 | + |
| 21 | +#include "NefryEddystone.h" |
| 22 | +#include "esp32-hal-log.h" |
| 23 | + |
| 24 | +#include "bt.h" |
| 25 | +#include "bta_api.h" |
| 26 | +#include "esp_gap_ble_api.h" |
| 27 | +#include "esp_gatts_api.h" |
| 28 | +#include "esp_bt_defs.h" |
| 29 | +#include "esp_bt_main.h" |
| 30 | +#include "Arduino.h" |
| 31 | + |
| 32 | +static esp_ble_adv_params_t _adv_params = { |
| 33 | + .adv_int_min = 0x20, |
| 34 | + .adv_int_max = 0x40, |
| 35 | + .adv_type = ADV_TYPE_NONCONN_IND, |
| 36 | + .own_addr_type = BLE_ADDR_TYPE_PUBLIC, |
| 37 | + .peer_addr = { 0x00, }, |
| 38 | + .peer_addr_type = BLE_ADDR_TYPE_PUBLIC, |
| 39 | + .channel_map = ADV_CHNL_ALL, |
| 40 | + .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, |
| 41 | +}; |
| 42 | + |
| 43 | +uint8_t eddystone_packet[31] = { |
| 44 | + 0x02, 0x01, 0x06, 0x03, 0x03, 0xAA, 0xFE, |
| 45 | + 0x00, //Length |
| 46 | + 0x16, 0xAA, 0xFE, 0x10, 0x05, |
| 47 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 48 | +}; |
| 49 | +/* |
| 50 | +* BLE Arduino |
| 51 | +* |
| 52 | +* */ |
| 53 | +bool NefryEddystone::begin(String url) { |
| 54 | + setUrl(url); |
| 55 | + for (int i = 0; i < 31; i++) { |
| 56 | + Serial.print(eddystone_packet[i]); |
| 57 | + Serial.print(":"); |
| 58 | + } |
| 59 | + //Serial.println("btStart"); |
| 60 | + if (!btStarted() && !btStart()) { |
| 61 | + //Serial.println("btStart failed"); |
| 62 | + return false; |
| 63 | + } |
| 64 | + //Serial.println("esp_bluedroid_init"); |
| 65 | + esp_bluedroid_status_t bt_state = esp_bluedroid_get_status(); |
| 66 | + if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED) { |
| 67 | + if (esp_bluedroid_init()) { |
| 68 | + //Serial.println("esp_bluedroid_init failed"); |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + //Serial.println("esp_bluedroid_enable"); |
| 73 | + if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) { |
| 74 | + if (esp_bluedroid_enable()) { |
| 75 | + //Serial.println("esp_bluedroid_enable failed"); |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (esp_ble_gap_config_adv_data_raw(eddystone_packet, eddystone_packet[7]+8)) { |
| 81 | + //Serial.println("gap_config_adv_data failed"); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + esp_ble_gap_start_advertising(&_adv_params); |
| 86 | + return true; |
| 87 | +} |
| 88 | + |
| 89 | +bool NefryEddystone::end() |
| 90 | +{ |
| 91 | + if (btStarted()) { |
| 92 | + esp_bluedroid_disable(); |
| 93 | + esp_bluedroid_deinit(); |
| 94 | + btStop(); |
| 95 | + return true; |
| 96 | + } |
| 97 | + return false; |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +bool NefryEddystone::setUrl(String url) { |
| 102 | + if (url.startsWith("http://www.")) { |
| 103 | + return setUrl(url.substring(11), 0); |
| 104 | + } |
| 105 | + else if (url.startsWith("https://www.")) { |
| 106 | + return setUrl(url.substring(12), 1); |
| 107 | + } |
| 108 | + else if (url.startsWith("http://")) { |
| 109 | + return setUrl(url.substring(7), 2); |
| 110 | + } |
| 111 | + else if (url.startsWith("https://")) { |
| 112 | + return setUrl(url.substring(8), 3); |
| 113 | + } |
| 114 | + else { |
| 115 | + return setUrl(url, 3); |
| 116 | + } |
| 117 | + return true; |
| 118 | +} |
| 119 | +bool NefryEddystone::setUrl(String url, int mode) { |
| 120 | + /** mode ˆø”î•ñ |
| 121 | + Decimal Hex Expansion |
| 122 | + 0 0x00 http://www. |
| 123 | + 1 0x01 https://www. |
| 124 | + 2 0x02 http:// |
| 125 | + 3 0x03 https:// |
| 126 | + */ |
| 127 | + eddystone_packet[13] = mode & 0xff; // URL Scheme |
| 128 | + uint8_t uri_len = (uint8_t)url.length(); |
| 129 | + if (uri_len >= 17)return false; |
| 130 | + for (int i = 0; i<uri_len; i++) { |
| 131 | + eddystone_packet[14 + i] = (uint8_t)url.charAt(i); |
| 132 | + //Serial.print(url.charAt(i)); |
| 133 | + } |
| 134 | + eddystone_packet[7] = uri_len + 6; |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +#endif |
0 commit comments