2016-12-28 76 views
0

我必須使用BLE從外部設備(數據記錄器溫度計)獲取數據。請參閱下面的圖片瞭解該設備。在iOS SDK中使用BLE從外部設備發送和接收數據

該設備僅在發送一條命令時才發送數據。我的意思是我們必須首先發送靜態字節數據到設備,然後它將發送顯示在圖像中的T1,T2,T3和T4值的數據。

我可以使用BLE將數據發送到一個iOS設備,使用BTLE Central Peripheral Transfer Demoanswer。但是,我無法使用這種方式向外部設備發送和接收數據。

正如本演示中提到的,我使用CBPeripheralManagerCBCentralManager來使用BLE發送和接收數據。此外,我可以使用EAAccessoryManager來顯示所有BLE連接附近的列表。因此,當用戶從列表中單擊任何設備時,我會獲取該設備的UUID並嘗試使用該UUID發送和接收數據。

是否有任何其他方式使用藍牙將iOS設備發送和接收數據到任何外部設備?

我想分享一下,Android Developer使用SSP而不是BLE來實現這個功能。這是否可以在iOS中使用BLE來完成此操作?

任何幫助,將不勝感激。

+1

您確定該設備實際支持BLE嗎?經典的藍牙和BLE是兩種不同的協議/堆棧。通過'EAAccessoryManager'可見的設備是Classic Bluetooth設備,而不是BLE設備。要掃描BLE設備,您應該使用'CBCentralManager scanForPeripheralsWithServices:options:'。 – jcaron

回答

1

我總是用CoreBluetooth執行這一協議(CBCentralManagerDelegate,CBPeripheralDelegate)和重寫此方法:

func centralManagerDidUpdateState(_ central: CBCentralManager) 

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) 

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) 

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) 

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) 

// Check if the service discovered is a valid Service 
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) 

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) 

// And for getting the value changes in the BLE Device... 
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) 

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) 

我希望它能幫助

1

您寫入或讀取數值之前,你需要知道哪些特點是可讀的並且是可寫的,比你可以讀或寫。只需BTLE的過程爲:

掃描周邊 - > conenect - >查找服務 - >發現的特點 - > ...你需要

操作,如果你想寫值可寫特性,需要確認外設將響應此寫入請求,否則您將在回調中收到錯誤:peripheral(_:didWriteValueFor:error)可能是「未知錯誤」。

其他

當您嘗試讀取你需要呼叫特徵的價值:readValue(for:)setNotifyValue(_:for:),外圍設備的更新的結果將在響應peripheral(_:didUpdateValueFor:error:)

以上,是我BTLE數據傳輸的理解。希望能幫助你。