从Bluez 5.48开始如何写特征

时间:2018-06-24 14:59:32

标签: linux bluetooth-lowenergy bluez

我正在使用Bluez 5.50; 5.48是正式(非实验)支持的acquire-write版本。我是否正确使用了获取写入/写入功能?

在我的Raspberry Pi Zero W上:

pi@raspberrypi:~ $ bluetoothctl
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
[bluetooth]# connect B8:27:E8:72:33:7A
Attempting to connect to B8:27:E8:72:33:7A
Connection successful
[DSD TECH]# menu gatt
[DSD TECH]# select-attribute 0000ffe1-0000-1000-8000-00805f9b34fb
[DSD TECH:/service0010/char0011]# acquire-write
[CHG] Attribute /org/bluez/hci0/dev_B8_27_E8_72_33_7A/service0010/char0011 WriteAcquired: yes
AcquireWrite success: fd 7 MTU 23
[DSD TECH:/service0010/char0011]# write("h")
[DSD TECH:/service0010/char0011]# write('h')
[DSD TECH:/service0010/char0011]# write(h)
[DSD TECH:/service0010/char0011]# write(0x68)

在连接到HM-10的Arduino Nano上:

void setup() 
{
  Serial.begin(9600);
}

void loop() {
  delay(10);
  if (Serial.available())
  {
    char c = Serial.read();
    Serial.println("Read value");
  }
}

我使用名为“ Serial”的iOS应用程序,该应用程序连接到HM-10。我将字母h发送过来,并在arduino串行监视器上读取“读取值”。我在同一个arduino串行监视器上使用树莓派之前和之后直接执行此操作。树莓派通知永远不会通过。是的,我确定它是HM-10。这是我在BLE范围内开机的唯一HM-10,并且在连接手机时无法连接到UUID。 (实际上,我将connect B8:27:E8:72:33:7A提交给树莓派,然后继续尝试,然后断开与手机的连接,rpi会自动连接)。

谢谢!

2 个答案:

答案 0 :(得分:0)

将gatt服务与bluetoothctl一起使用时,请省略括号并仅使用:
write 0x68

答案 1 :(得分:0)

acquire-write返回一个文件描述符,该文件描述符可用于写入GATT特性,写入的使用将被锁定。因此,要么使用acquire-write(需要一个write-without-response标志),要么使用write。

            void WriteValue(array{byte} value, dict options)

                    Issues a request to write the value of the
                    characteristic.

                    Possible options: "offset": Start offset
                                      "device": Device path (Server only)
                                      "link": Link type (Server only)
                                      "prepare-authorize": boolean Is prepare
                                                           authorization
                                                           request

                    Possible Errors: org.bluez.Error.Failed
                                     org.bluez.Error.InProgress
                                     org.bluez.Error.NotPermitted
                                     org.bluez.Error.InvalidValueLength
                                     org.bluez.Error.NotAuthorized
                                     org.bluez.Error.NotSupported

            fd, uint16 AcquireWrite(dict options) [optional]

                    Acquire file descriptor and MTU for writing. Usage of
                    WriteValue will be locked causing it to return
                    NotPermitted error.

                    For server the MTU returned shall be equal or smaller
                    than the negotiated MTU.

                    For client it only works with characteristic that has
                    WriteAcquired property which relies on
                    write-without-response Flag.

                    To release the lock the client shall close the file
                    descriptor, a HUP is generated in case the device
                    is disconnected.

                    Note: the MTU can only be negotiated once and is
                    symmetric therefore this method may be delayed in
                    order to have the exchange MTU completed, because of
                    that the file descriptor is closed during
                    reconnections as the MTU has to be renegotiated.

                    Possible options: "device": Object Device (Server only)
                                      "MTU": Exchanged MTU (Server only)
                                      "link": Link type (Server only)

                    Possible Errors: org.bluez.Error.Failed
                                     org.bluez.Error.NotSupported