如何使用CoreBluetooth发送BLE命令?

问题描述 投票:0回答:1

我想使用奥豪斯蓝牙接口连接奥豪斯体重秤:

https://dmx.ohaus.com/WorkArea/showcontent.aspx?id=4294974224

我成功连接并接收了 uuid 2456e1b9-26e2-8f83-e744-f34f01e9d703 的 CBCharacteristic。

我想知道体重秤的重量。这就是我所做的:

//send command to the scale
let data = "IP"
let valueString = (data as NSString).data(using: String.Encoding.utf8.rawValue)
self.scalePeripheral.writeValue(valueString!, for: scaleCharchteristic, type: CBCharacteristicWriteType.withResponse)

我应该期望通过委托功能收到秤重量:

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic,error: Error?){
    guard 
        let characteristicValue = characteristic.value,
        let characteristicASCIIValue = NSString(data: characteristicValue, encoding: String.Encoding.utf8.rawValue)
    else {
        return
    }
    print("\((characteristicASCIIValue as String))")
}

但我收到空值。

而且我没有收到任何数据到

peripheralDidUpdateValueForCharacteristic

可能是什么问题?

蓝牙指令参考如下:

enter image description here

swift bluetooth-lowenergy core-bluetooth
1个回答
0
投票

您不能只访问

characteristic.value
,您需要执行特征的
readValue(for:)
并等待对
peripheral(_:didUpdateValueFor:error)
的回调,或者(如果支持)通过
setNotifyValue(for:)
启用特征通知

© www.soinside.com 2019 - 2024. All rights reserved.