简体   繁体   English

使用iOS SDK中的BLE从外部设备发送和接收数据

[英]Send and Receive data from external device using BLE in iOS SDK

I have to fetch the data from the external device (Datalogger Thermometer) using BLE. 我必须使用BLE从外部设备(数据记录仪温度计)获取数据。 Please see the below image for this device. 请参阅此设备的下图。

This device is only sending data when we are sending one command. 仅当我们发送一个命令时,此设备才发送数据。 I mean we have to send static byte data to device first, then and then it will be sending back the data of T1, T2, T3 and T4 values which is showing into the image. 我的意思是我们必须先将静态字节数据发送到设备,然后再将发送回T1,T2,T3和T4值的数据发送到设备中。

I can able to sending data using BLE to one iOS device to another using BTLE Central Peripheral Transfer Demo and this answer . 我可以使用BLE将数据发送到一个iOS设备,再使用BTLE Central Peripheral Transfer Demo和此答案将数据发送到另一个。 But, I can not able to send and receive data to this external device using this way. 但是,我无法使用这种方式向该外部设备发送和接收数据。

As mentioned into this demo, I am using CBPeripheralManager and CBCentralManager for sending and receiving data using BLE. 如本演示中所述,我正在使用CBPeripheralManagerCBCentralManager通过BLE发送和接收数据。 Also I can able to show the list of all near by BLE connections using EAAccessoryManager . 我也可以使用EAAccessoryManager通过BLE连接显示所有附近的列表。 So when user click on the any device from the list, then I fetching UUID of that device and trying to sending and receiving data using that UUID. 因此,当用户单击列表中的任何设备时,我将获取该设备的UUID并尝试使用该UUID发送和接收数据。

Is there any other way to sending and receiving data using Bluetooth to iOS device to any external device.? 还有其他方法可以使用蓝牙将数据发送到iOS设备和任何外部设备来收发数据吗?

I would like to share that, Android Developer has implement this into Android using SSP, not BLE. 我想分享一下,Android Developer已使用SSP(而不是BLE)将其实现到Android中。 Is this possible to do this using BLE in iOS.? 在iOS中使用BLE可以做到这一点吗?

Any help would be appreciated. 任何帮助,将不胜感激。

I always use the CoreBluetooth implementing this protocols (CBCentralManagerDelegate,CBPeripheralDelegate) and overriding this methods: 我总是使用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?)

I hope it helps 希望对您有所帮助

Before you write or read values, you need to know which characteristic is readable and which is writeable, than you can read or write it. 在写入或读取值之前,您需要知道哪些特征是可读的,哪些特征是可写的,而不是可以读取或写入的。 Simply the process of BTLE is: BTLE的过程很简单:

scan peripheral -> conenect -> find services -> discover characteristics -> ...operations you need 扫描外围设备->连接->查找服务->发现特征-> ...您需要的操作

if you want to write value to writeable characteristics, you need confirm that peripheral will response this write request, otherwise you will receive a error in callback: peripheral(_:didWriteValueFor:error) maybe "Unknow error". 如果要将值写入可写特征,则需要确认外设将响应此写请求,否则您将在回调中收到错误: peripheral(_:didWriteValueFor:error)可能是“未知错误”。

other when you try to read value of characteristics you need call: readValue(for:) or setNotifyValue(_:for:) , the results of peripheral's update will response in peripheral(_:didUpdateValueFor:error:) 其他当您尝试读取特征值时需要调用: readValue(for:)setNotifyValue(_:for:) ,外设的更新结果将在peripheral(_:didUpdateValueFor:error:)响应peripheral(_:didUpdateValueFor:error:)

Above, is my understanding of BTLE data transfer. 上面是我对BTLE数据传输的理解。 hope to help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM