2017年2月27日 星期一

BLE ATT, GATT protocol 解析

markdown GATT,也就是 Generic Attribute Profile 的縮寫,是 Bluetooth Low Energy 裝置用來傳送資料的 protocol,它包含了所謂 Service 和 Characteristics 的概念在其中,並且建構在 Attribute Protocol(ATT) 的 protocol 之上 Attribute Protocol ---- ATT 定義了名為 Attribute 的結構,包含三個元素 * 16-bits handle * UUID(官方已有定義的為16-bits, 自定義的為 128-bits) 定義了 attribute type * Attribute value, 就是一 byte array, 其意義由 UUID 決定 ATT 單只是定義了這個結構,並未定義任何 UUID,這留給 GATT 來定義. 結構中 value 的長度取決於 UUID,傳送資料時並不會包含 value 的長度訊息 ATT server 維護 Attribute 資訊,ATT client 則否 Generic Attribute Profile ---- GATT 定義了許多的 Attribute,並且提出了 Service 和 Characteristics 的概念,由 Attribute 組成 Characteristics,再由多個 Characteristics 組成 Service,多個 Service 則組成 Profile ![Attribute structure](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGhB8leQjh7r9NZ0x-nHWAb4a-EcOlX5SPwxT8jYPWnywR2uBdagnJcPNa-QxEFbTAiq0gsLMIpVK-lJlmWfKUZdF1axGBWurcqUaOQGGd-yNW1bpMFY5tvrl8oe_K9NsJt5MmnuvguBV9/s1600/microcontrollers_GattStructure.png) 之前提到 ATT server 維護 Attribute 資訊,當 BLE 裝置連上後,可以跟 server 查詢其上面所有的 Attribute,client 可以得到一個 Attribute 列表 ![Attribute sample](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2iW68RFmDnZ0ANgHjcD8lGArmTYGcbb266QGxDm8EYNP9b8Njg7vTODpPB6LZnaN-dNV4JfJjsI2zm1sfknnUfhdL6pGnFTSnS4t0cINRLgc4Oo_NJ95iyVqlAoeKxrC-YiiBLIE0BxJW/s1600/%25E7%25B4%25A2%25E5%25BC%2595.png) GATT 定義了一個 Service 總是從 UUID 0x2800 開始的,從 0x2800 開始的所有 Attribute 都屬於該 service,直到下一個 0x2800 為止 0x2800 除了表示 Service 的起點,這個 Attribute 的 value 也定義了該 Service 是哪種 Service,例如 value = 0x180D 的話就表示這是一個 Heart Rate Service 一個 Service 總是包含了一個以上的 Characteristics,GATT 也定義了特定的 Attribute 來表示 Service 中有哪些 Characteristics ![GATT sample](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrZxUrwqFxwr0FhzmABicDyKVrN46acZ19_E-6e9dTutWC9qcKyTGbliekYIRsyvQeVnzEx8WSVaHexqE_Zy4dJdScXlr3QyvCehJVjK7Fg7w1lKxi13E-sb6DnAzFkT9WBdPNsocATY7H/s1600/table.png =800x*) UUID = 0x2803 的 Attribute 表示這個 Service 有一個 Characteristic,而是甚麼 Characteristics,它的值又在哪邊,則寫在此 Attribute 的 value 中,如上圖所示,第二列表示 Heart Rate Service 有一個 Characteristic,這個 Characteristic 的值在 Handle = 0x000E,UUID = 0x2A37 的 Attribute 中,也就是第三列,UUID 0x2A37 也是 BLE 標準預定義好的,表示 Heart Rate Measurement Characteristic,所以,實際上最少會用兩個 Attribute 來表示一個 Characteristic 這種設計方式非常的彈性,Client 可以知道有多少 Service 存在,Service 中又有多少 Characteristics,而不一定要確切知道這些 Service 和 Characteristic 是甚麼,BLE 裝置之間就以它們認識的 Service 互相溝通就好,而之後要新增新的 Characteristic和 Service,也不需要改變既有的 client 程式

2017年2月20日 星期一

Android SVG Support

Android 在 5.x 版之後加入了對 SVG的支持, SVG stands for scalable vector graphics, 中文叫可伸縮向量圖形, 實際上是 xml 格式的檔案, 內容為描述如何繪出圖形, 特點是放大或縮小不失真

Android主推的 material design 圖形在 Android Studio 中就以 SVG 的方式存在, 右鍵選擇 Vector Asset 就可以在專案中加入一個 SVG 圖形, 實際上已 xml 檔的形式存在


點開該 xml 檔會發現內容其實很簡單, 通常會修改的就是 width/height, viewportWidth/viewportHeight 這些參數


width/height 的意義就是這張圖片實際上在螢幕上占的大小, viewportWidth/viewportHeight則是表示將這張圖片分成幾等分, 在 xml 檔中的 path元素中使用的參數就是以 viewportWidth/viewportHeight 為準, 例如 viewportWidth為 48, path 中 width 24 的點就會在圖片的中央

在 Android 中使用 SVG 的好處有
  • 圖片放大縮小不失真
  • 不需要為了各種尺寸的螢幕準備不同大小的圖片
  • 減少 app 大小, 相對於 png 等圖片檔, xml 是很小的
但是當然也有壞處, 因為 SVG 等於是執行時才畫出圖形, 所以複雜的 SVG大量使用時會造成效能問題