2016年11月20日 星期日

swift 3.0& BLE 初學觀念

以下blog是很好的初學概念架構

http://thinkingiot.blogspot.tw/2016/02/android-ble-scanner-5.html

http://cheng-min-i-taiwan.blogspot.tw/2015/06/android-ble.html


http://jasonchiucc.github.io/2016/09/07/arduino-ble/

http://lywdev.space/2016/09/22/ios-corebluetooth/

實作說明
http://shenyun23-4.blogspot.tw/2015/10/ios-corebluetooth-swift-2-ble-2.html

資料來源:藍芽工程師: GATT protocol



完整範例檔

https://github.com/HarryGoodwin/BLE_Scanner_iOS

以下資料來源
http://jasonchiucc.github.io/2016/09/07/arduino-ble/
一開始可以先看 Apple 官方 BLE 翻譯,對基本概念有個理解
IOS 主要使用 Core Bluetooth 框架來和藍芽裝置通訊,以下簡稱 CB
一般就是一個手機和一個藍牙來進行通訊,這兩個設備有另一個專有名詞
外接裝置(Peripheral = Server 端)和中心 (central = Client 端)
1
2
Server = 藍芽裝置(HM-10)
Client = IOS 裝置(手機)
所謂 Server 端就是提供資料方,Client 端是接收資料方
當然如果你是兩台 iphone 使用 BLE 互傳資料
那手機就可同時為 Server 端或 Client 端,並非手機只能當 Client
Client 要讀取外接裝置的流程如下
1
2
設定 CBCentralManager -> 發現外接裝置 -> 發現服務(CServer) 
       -> 發現特徵(CBCharateristic) -> 訂閱特徵 -> 讀取資料(Rx)
外接裝置的設定如下
1
設定 CBPeripheralManager -> 設定服務與特徵 -> 發布服務與特徵 -> 廣播服務 -> 發送資料
手機和 BLE 溝通時會用倒的方法
1
2
3
4
5
6
7
// Rx (IOS裝置讀取外接裝置)
func peripheral(peripheral: CBPeripheral?, didUpdateValueForCharacteristic 
    characteristic: CBCharacteristic?, error: NSError!)

// Tx (IOS裝置傳送外接裝置)
func peripheralDevice.writeValue(data, forCharacteristic: deviceCharacteristics, 
               type: CBCharacteristicWriteType.WithoutResponse)