如何解决(BLE) 无法将数据写入 iOS 上的特性
我一直在开发一个移动应用程序,该应用程序应该向充当 GATT 服务器的 BLE 设备发送一些数据,并在接收到数据后,GATT 服务器发送回通知。
问题是在iOS上,数据没有成功发送,因为GATT服务器没有发送通知。在 Android 上,我在发送数据和接收通知方面没有遇到任何问题。
在 iOS 上,我认为我已经完成了 BLE 连接所需的所有步骤(即服务发现、特征发现、订阅通知、监听回调以确保事件成功发生等),并且我认为问题发生在我将数据写入特征时。
首先,我在Android上做了一个BT snoop日志,分析了一个使用Wireshark的成功案例。
3113 6682.498460 Google_8c:f4:d1 (Pixel 3a) microchi_16:a6:19 (n08S7E) ATT 29 ✓ Sent Write Request,Handle: 0x001b (UnkNown: UnkNown)
Frame 3113: 29 bytes on wire (232 bits),29 bytes captured (232 bits)
Encapsulation type: Bluetooth H4 with linux header (99)
Arrival Time: May 30,2021 18:01:43.998492000 GTB Daylight Time
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1622386903.998492000 seconds
[Time delta from prevIoUs captured frame: 0.063883000 seconds]
[Time delta from prevIoUs displayed frame: 0.063883000 seconds]
[Time since reference or first frame: 6682.498460000 seconds]
Frame Number: 3113
Frame Length: 29 bytes (232 bits)
Capture Length: 29 bytes (232 bits)
[Frame is marked: False]
[Frame is ignored: False]
point-to-point Direction: Sent (0)
[Protocols in frame: bluetooth:hci_h4:bthci_acl:btl2cap:btatt]
Bluetooth
[Source: Google_8c:f4:d1 (58:cb:52:8c:f4:d1)]
[Destination: microchi_16:a6:19 (68:27:19:16:a6:19)]
Bluetooth HCI H4
[Direction: Sent (0x00)]
HCI Packet Type: ACL Data (0x02)
Bluetooth HCI ACL Packet
.... 0000 0000 0111 = Connection Handle: 0x007
..00 .... .... .... = PB Flag: First Non-automatically Flushable Packet (0)
00.. .... .... .... = BC Flag: point-to-point (0)
Data Total Length: 24
Data
[Connect in frame: 3035]
[disconnect in frame: 3132]
[Source BD_ADDR: Google_8c:f4:d1 (58:cb:52:8c:f4:d1)]
[Source Device Name: Pixel 3a]
[Source Role: UnkNown (0)]
[Destination BD_ADDR: microchi_16:a6:19 (68:27:19:16:a6:19)]
[Destination Device Name: n08S7E]
[Destination Role: UnkNown (0)]
[Current Mode: UnkNown (-1)]
Bluetooth L2CAP Protocol
Length: 20
CID: Attribute Protocol (0x0004)
Bluetooth Attribute Protocol
Opcode: Write Request (0x12)
Handle: 0x001b (UnkNown: UnkNown)
Value: 102120d2451b384fccb055b3b336ac5c36
可以看出,值 (102120d2451b384fccb055b3b336ac5c36) 已成功从 Android (char packet_bytes[] = { 0x10,0x21,0x20,0xd2,0x45,0x1b,0x38,0x4f,0xcc,0xb0,0x55,0xb3,0x36,0xac,0x5c,0x36 }).
尝试从 iOS 发送相同的值,我得到以下结果:
let value: [UInt8] = [0x10,0x36]
self.pheripheral?.writeValue(Data(value),for: characteristic,type: .withResponse)
此外,我收到确认值已写入且 didWriteValueFor 特性中没有错误。
我已经使用 PacketLogger 在我的设备上记录了 BT 数据,然后我将其导出并在 Wireshark 中进行了分析。
122 -790273982.464000 localhost () microchi_16:a6:19 (n08S7E) ATT 28 Sent Write Request,Handle: 0x001b (Generic Attribute Profile: Model Number String: Client Characteristic Configuration)
Frame 122: 28 bytes on wire (224 bits),28 bytes captured (224 bits)
Bluetooth
Bluetooth HCI H1 Sent ACL Data
Bluetooth HCI ACL Packet
.... 0000 0100 0001 = Connection Handle: 0x041
..00 .... .... .... = PB Flag: First Non-automatically Flushable Packet (0)
00.. .... .... .... = BC Flag: point-to-point (0)
Data Total Length: 24
Data
[Connect in frame: 22]
[disconnect in frame: 138]
[Source BD_ADDR: 00:00:00_00:00:00 (00:00:00:00:00:00)]
[Source Device Name: ]
[Source Role: UnkNown (0)]
[Destination BD_ADDR: microchi_16:a6:19 (68:27:19:16:a6:19)]
[Destination Device Name: n08S7E]
[Destination Role: UnkNown (0)]
[Current Mode: UnkNown (-1)]
Bluetooth L2CAP Protocol
Bluetooth Attribute Protocol
Opcode: Write Request (0x12)
0... .... = Authentication Signature: False
.0.. .... = Command: False
..01 0010 = Method: Write Request (0x12)
Handle: 0x001b (Generic Attribute Profile: Model Number String: Client Characteristic Configuration)
Characteristic Configuration Client: 0x2110
0010 0001 0001 00.. = Reseved: 0x0844
.... .... .... ..0. = Indication: False
.... .... .... ...0 = Notification: False
[Expert Info (Warning/Protocol): Bad Data]
在 iOS 上,Wireshark 显示“错误数据”(与 Android 显示字段“值”不同),当我将鼠标悬停在我发送的数据上时,它会突出显示标题中的 3 个额外字节,而在 Android 上它只突出显示发送的数据。
我尝试了很多方法来发送数据,但是每次从 iOS 发送数据时,Wireshark 都显示“错误数据”。
有没有人遇到过类似的情况?我也不确定为什么 iOS 和 Android 之间的标头不同(iOS 缺少 HCI 数据包类型:ACL 数据标头)。
我要么认为我将数据错误地写入特征(因此“错误数据”显示在 Wireshark 中),要么 GATT 服务器存在问题(这很奇怪,因为它适用于Android 和 Wireshark 只是错误地显示“错误数据”)。
如果我应该包含更多信息,请告诉我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。