蓝牙调试工具如何使用_使用此有价值的工具改进您的蓝牙项目:第2部分!
藍牙調試工具如何使用
This post is originally from www.jaredwolff.com.
這篇文章最初來自www.jaredwolff.com。
This is Part 2 of configuring your own Bluetooth Low Energy Service using a Nordic NRF52 series processor. If you haven’t seen Part 1 go back and check it out. I’ll be waiting right here..
這是使用Nordic NRF52系列處理器配置自己的低功耗藍牙服務的第2部分。 如果您還沒有看到第1部分,請返回并進行檢查。 我將在這里等
If you’re with me thus far, high five. ?
如果到目前為止你和我在一起,高五。 ?
Let’s jump in!
讓我們跳進去!
So far we’ve created an efficient cross platform data structure using Protocol Buffers. This Protocol Buffer in particular can be used to send these defined data structures a Bluetooth Low Energy Service. In this part, I’ll show you the inner workings of creating the service from scratch.
到目前為止,我們已經使用協議緩沖區創建了有效的跨平臺數據結構。 該協議緩沖區尤其可以用于向這些定義的數據結構發送藍牙低功耗服務。 在這一部分中,我將向您展示從頭開始創建服務的內部工作原理。
P.S. this post is lengthy. If you want something to download, click here for a a beautifully formatted PDF. (Added bonus, the PDF has all three parts of this series!)
PS這個帖子很長。 如果要下載某些內容, 請單擊此處以獲取格式精美的PDF。 (此外,PDF具有本系列的所有三個部分!)
創建服務 (Creating the Service)
Dealing with Bluetooth Low Energy in general can seem overwhelming. As I discussed here, there’s a few moving parts that you need to keep in mind.
通常,處理低功耗藍牙似乎不堪重負。 正如我在這里討論的那樣,您需要牢記一些活動部分。
The best way to create a new service is to copy an already existing one! I’ve done this by:
創建新服務的最好方法是復制一個已經存在的服務! 我這樣做是:
Copy ble_bas.h to include/ble
復制ble_bas.h以include/ble
Copy ble_bas.c to src/ble
將ble_bas.c復制到src/ble
I’ve then renamed them from ble_bas to ble_protobuf to be consistent. I’ve also done the same inside the files. (BAS is the battery level service used to report battery voltage or relative charge using a percentage)
然后,為了保持一致,我將它們從ble_bas重命名為ble_protobuf 。 我也在文件內部做了同樣的事情。 (BAS是電池電量服務,用于以百分比形式報告電池電壓或相對電量)
I’ve also gone ahead and removed all the battery measurement functions as they will be replaced. This part of the process is fairly tedious and prone to error. If you’re new to the Nordic SDK, I highly recommend you download the example code for this post.
我也繼續進行并刪除了所有電池測量功能,因為它們將被替換。 該過程的這一部分相當繁瑣并且容易出錯。 如果您是Nordic SDK的新手, 我強烈建議您下載本文的示例代碼。
添加UUID (Adding a UUID)
Normally, for a vendor defined service you’ll have to use your own UUID. There are certain ranges of UUIDs that are reserved for the Bluetooth SIG. Supposedly you can also reserve your own UUID if you’re a member. Here’s a handy post on Stack Overflow on the subject.
通常,對于供應商定義的服務,您將必須使用自己的UUID。 有某些范圍的UUID保留用于Bluetooth SIG。 如果您是會員,還可以保留自己的UUID。 這是有關該主題的Stack Overflow的便捷文章。
In our case, I’ve defined a UUID that I’ve used for other projects. If you go to ble_protobuf.h you can see the UUIDs for both the service and the characteristic:
在我們的案例中,我已經定義了用于其他項目的UUID。 如果轉到ble_protobuf.h ,則可以看到該服務和特征的UUID:
// UUID for the Service & Char #define PROTOBUF_UUID_BASE {0x72, 0x09, 0x1a, 0xb3, 0x5f, 0xff, 0x4d, 0xf6, \0x80, 0x62, 0x45, 0x8d, 0x00, 0x00, 0x00, 0x00} #define PROTOBUF_UUID_SERVICE 0xf510 #define PROTOBUF_UUID_CONFIG_CHAR (PROTOBUF_UUID_SERVICE + 1)Both PROTOBUF_UUID_BASE PROTOBUF_UUID_SERVICE are used in ble_protobuf_init The last one is used in command_char_add (I’ll describe that a bit more below).
這兩個PROTOBUF_UUID_BASE PROTOBUF_UUID_SERVICE在使用ble_protobuf_init最后一個是在使用command_char_add (我將介紹更多一點下文)。
You can go without defining a BASE ID but I highly recommend you go the extra mile. That way your application will be impervious to future Bluetooth protocol updates.
您可以在不定義BASE ID的情況下進行操作,但是我強烈建議您多加努力。 這樣,您的應用程序將不受以后的藍牙協議更新的影響。
創建特征 (Creating the Characteristic)
Nordic has a fairly straight forward way of initializing separate characteristics in each service. For each characteristic, there is a char_add function which then configures and add the characteristic to the service.
Nordic在每種服務中初始化一個單獨特征的方法相當簡單。 對于每個特征,都有一個char_add函數,該函數然后配置該特征并將其添加到服務中。
使用ble_add_char_params_t配置特征 (Configure your Characteristic withble_add_char_params_t)
Nordic has put the configurations parameters for a BLE Characteristic into a logical (and helpful) struct. If you’re developing on a different platform you may find the same settings though all not in one place! (Or handled logically… ?)
Nordic已將BLE特性的配置參數放入邏輯(且有用)的結構中。 如果您在不同的平臺上進行開發,則可能會發現相同的設置,盡管它們都不在一個地方! (或按邏輯處理……?)
Here’s the breakdown of the struct:
這是該結構的細分:
typedef struct {uint16_t uuid;uint8_t uuid_type;uint16_t max_len;uint16_t init_len;uint8_t * p_init_value;bool is_var_len;ble_gatt_char_props_t char_props;ble_gatt_char_ext_props_t char_ext_props;bool is_defered_read;bool is_defered_write;security_req_t read_access;security_req_t write_access;security_req_t cccd_write_access;bool is_value_user;ble_add_char_user_desc_t *p_user_descr;ble_gatts_char_pf_t *p_presentation_format; } ble_add_char_params_t;It’s here that you tell the BLE Stack about what type of characteristic this is. In this example I use a handful of parameters to define the Protobuf Command Characteristic.
您在這里告訴BLE堆棧這是什么類型的特征。 在此示例中,我使用了一些參數來定義Protobuf命令特征。
uuid defines the address of how the characteristic will be accessed. If you’re defining your own service. max_len is the maximum length of data that you may send though the characteristic. That’s why it’s important to set max_size in your Protocol Buffer .options file for all variable length parameters. Once you compile your Protocol Buffers you’ll get a *_size variable much like the one defined in command.pb.h Here's a snippet of what it looks like below:
uuid定義將如何訪問特征的地址。 如果您要定義自己的服務。 max_len是您可以通過特征發送的最大數據長度。 這就是為什么在Protocol Buffer .options文件中為所有可變長度參數設置max_size的原因。 編譯協議緩沖區后,您將獲得一個*_size變量,就像在command.pb.h定義的變量一樣。這是下面的代碼片段:
/* Maximum encoded size of messages (where known) */ #define event_size 67Thus when defining ble_add_char_params_t I set max_len to event_size:
因此,在定義ble_add_char_params_t我將max_len設置為event_size :
add_char_params.uuid = PROTOBUF_UUID_CONFIG_CHAR; add_char_params.max_len = event_size; add_char_params.is_var_len = true;Along the same lines, because we’re using a string as one of the parameters in the Protocol Buffer, this data can be of variable size. is_var_len is handy to make sure that the right amount of bytes is sent and received. The decode function of the Protocol Buffers will fail if more data is fed in than necessary. (I learned this the hard way!)
同樣,由于我們使用字符串作為協議緩沖區中的參數之一,因此此數據的大小可以可變。 is_var_len可以方便地確保發送和接收正確數量的字節。 如果輸入的數據超出了必要,協議緩沖區的解碼功能將失敗。 (我經過慘痛的教訓才學到這個!)
char_props define the permissions for the characteristic. If you’re familiar with file system permissions on a computer, this will be second nature to you. In this example, read and write is what we’re looking for.
char_props定義特征的權限。 如果您熟悉計算機上的文件系統權限,這將是您的第二天性。 在這個例子中, 讀取和寫入就是我們要尋找的。
Finally, parameters ending in _access determine the security type used. In most cases SEC_OPEN or SEC_JUST_WORKSis more than sufficient. If you’re handling critical data (passwords, etc) you may have to implement a second layer of encryption or enable a higher level security mode.
最后,以_access結尾的參數確定所使用的安全性類型。 在大多數情況下, SEC_OPEN或SEC_JUST_WORKS 。 如果要處理關鍵數據(密碼等),則可能必須實施第二層加密或啟用更高級別的安全模式。
If you’re looking on more info about Bluetooth Low Energy security, here’s a useful post on the subject.
如果您正在尋找有關藍牙低功耗安全性的更多信息, 這是有關此主題的有用文章。
加。 天 字符 (Add. dat. char.)
Once you’ve defined your params, it’s as easy as calling the characteristic_add function. This will associate this new characteristic with the related service. The first argument is the service handle, the second the configuration parameters and the third is a pointer to the handles for the characteristic. (See below)
定義了參數之后,就像調用characteristic_add函數一樣簡單。 這會將這個新特性與相關服務相關聯。 第一個參數是服務句柄,第二個參數是配置參數,第三個參數是指向特征句柄的指針。 (見下文)
uint32_t characteristic_add(uint16_t service_handle,ble_add_char_params_t * p_char_props,ble_gatts_char_handles_t * p_char_handle)使其運行 (Getting it Running)
Setting everything up inside ble_protobuf.c is 90% of the battle. The final mile requires some bits being added to services_init in main.c
在ble_protobuf.c設置所有內容的工作是90%。 最后一英里需要將一些位添加到main.c services_init中
ble_protobuf_init_t protobuf_init = {0};protobuf_init.evt_handler = ble_protobuf_evt_hanlder;protobuf_init.bl_rd_sec = SEC_JUST_WORKS;protobuf_init.bl_cccd_wr_sec = SEC_JUST_WORKS;protobuf_init.bl_wr_sec = SEC_JUST_WORKS;err_code = ble_protobuf_init(&m_protobuf,&protobuf_init);APP_ERROR_CHECK(err_code);The above allows events to be funneled back to the main context. That way your app becomes much more interactive with the core logic of your firmware code. In Nordic’s examples they’ve also brought the security parameters out so they can be defined in the main context as well.
上面的內容使事件可以漏回到主上下文中。 這樣,您的應用程序就可以與固件代碼的核心邏輯更加互動。 在Nordic的示例中,他們還提出了安全性參數,因此也可以在主要上下文中進行定義。
Side note: m_protobuf is defined using a macro from ble_protobuf.h It not only creates a static instance of the service but it also defines the callback that is used for handling events.
附注: m_protobuf使用宏定義從ble_protobuf.h它不僅創造了該服務的靜態實例,但它也定義了用于處理事件的回調。
/**@brief Macro for defining a ble_protobuf instance.** @param _name Name of the instance.* @hideinitializer*/ #define BLE_PROTOBUF_DEF(_name) \static ble_protobuf_t _name; \NRF_SDH_BLE_OBSERVER(_name ## _obs, \BLE_PROTOBUF_BLE_OBSERVER_PRIO, \ble_protobuf_on_ble_evt, \&_name)If you're making your own service, you'll have to update the function name for the event handler. If you need to tweak priorities you can define/update that as well.
如果您要提供自己的服務,則必須更新事件處理程序的函數名稱。 如果您需要調整優先級,則也可以定義/更新優先級。
寫入此特征后會發生什么? (What happens when this characteristic is written to?)
ble_protobuf_on_ble_evt is the main way that events are handled within Bluetooth Low Energy services. We’re most concerned with the BLE_GATTS_EVT_WRITE event but you can trigger on any GATT event that tickles your fancy.
ble_protobuf_on_ble_evt是在藍牙低功耗服務中處理事件的主要方式。 我們最關心BLE_GATTS_EVT_WRITE事件,但是您可以觸發任何讓您BLE_GATTS_EVT_WRITE GATT事件。
on_write is where the action happens. It takes the data that is written to the characteristic and decodes it according to event_fields It’s all put conveniently into a struct for additional processing, etc. If an error happens in decoding, pb_decode returns an error. Once modified, the data is encoded and made available for reading. Since reading Part 1, the calls to pb_decode and pb_encode should look very familiar!
on_write是執行操作的地方。 它接受寫入特征的數據,并根據event_fields對其進行解碼。所有這些都方便地放入結構中以進行其他處理,等等。如果解碼中發生錯誤,則pb_decode將返回錯誤。 修改后,數據將被編碼并可供讀取。 自閱讀第1部分以來,對pb_decode和pb_encode的調用應該看起來非常熟悉!
Of course, you can have your firmware do whatever you want to. The Bluetooth Energy World is your oyster.
當然,您可以讓固件做任何您想做的事情。 藍牙能源世界是您的牡蠣。
最后說明 (Final Notes)
When adding new services to a Bluetooth Low Energy example, you may have to make some changes to the underlying code.
在向低功耗藍牙示例添加新服務時,您可能必須對基礎代碼進行一些更改。
For example, sdk_config.h may need some changes. Particularly NRF_SDH_BLE_VS_UUID_COUNT needs to be increased depending how many service UUIDs are made available. For this project, I am also using the DFU service (as it should be a default for all connected projects!!)
例如, sdk_config.h可能需要進行一些更改。 特別是需要增加NRF_SDH_BLE_VS_UUID_COUNT ,具體取決于有多少個服務UUID可用。 對于這個項目,我還使用了DFU服務(因為它應該是所有連接項目的默認設置!)
Another important aspect is memory and flash management. The default .ld file that comes with the BLE DFU service may not be sufficient for another BLE Service. The only way you’ll know there’s not enough is when you compile and flash it to a NRF52 device. If the device boots up stating there’s not enough memory, you’ll have to make the suggested changes. The error will show up on the debug console where this message normally shows up:
另一個重要方面是內存和閃存管理。 BLE DFU服務隨附的默認.ld文件可能不足以用于另一個BLE服務。 唯一了解不足的方法是編譯并刷新到NRF52設備。 如果設備啟動后表明沒有足夠的內存,則必須進行建議的更改。 該錯誤將顯示在通常顯示此消息的調試控制臺上:
<info> app: Setting vector table to bootloader: 0x00078000 <info> app: Setting vector table to main app: 0x00026000Learn more about how to get the Debug Console set up in the example code here.
在此處的示例代碼中了解有關如何設置調試控制臺的更多信息。
結論 (Conclusion)
In this part I’ve shown you the inner workings of a custom Bluetooth Low Energy service using Protocol Buffers. In the last part, I’ll show you how to load the firmware, run the example javascript app and test our freshly developed Protocol Buffer!
在這一部分中,我向您展示了使用協議緩沖區的自定義低功耗藍牙服務的內部工作原理。 在最后一部分中,我將向您展示如何加載固件,運行示例javascript應用程序以及測試我們新開發的Protocol Buffer!
翻譯自: https://www.freecodecamp.org/news/improve-your-bluetooth-project-with-this-valuable-tool-part-2/
藍牙調試工具如何使用
總結
以上是生活随笔為你收集整理的蓝牙调试工具如何使用_使用此有价值的工具改进您的蓝牙项目:第2部分!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到自己出轨了是什么意思
- 下一篇: spark 架构_深入研究Spark内部