我正在尝试创建一个具有更改特定配对外围设备的设备名称的功能的应用程序。发送给我的协议没有告诉我发送到该外围设备的数据类型,以便能够对其进行更改。在该外围设备的固件上,您有一个名为gatt.xml的文件,该文件包含以下信息:
<characteristic id="device_name" name="Device Name" sourceId="org.bluetooth.characteristic.gap.device_name" uuid="2A00">
<informativeText/>
<value length="32" type="utf-8" variable_length="true">smartFan</value>
<properties read="true" read_requirement="mandatory" write="true" write_no_response="true" write_no_response_requirement="optional" authenticated_write="true"/>
</characteristic>
所以可以写,类型是utf8,所以它应该采用utf-8编码的字符串,对吗? 我试图确保我的字符串是utf-8,但出现无效值错误。 这是我尝试做的事情:
function checkUTF8(text) {
var utf8Text = text;
try {
// Try to convert to utf-8
utf8Text = decodeURIComponent(escape(text));
// If the conversion succeeds, text is not utf-8
}catch(e) {
console.log(e.message); // URI malformed
// This exception means text is utf-8
}
return utf8Text; // returned text is always utf-8
}
function writeName() {
var nameToFanDecoded = Buffer.from(deviceName.get("deviceName"),"utf8");
var nameToFan = checkUTF8(nameToFanDecoded);
ble.write({
peripheralUUID: peripheral.UUID,
serviceUUID: '1800',
characteristicUUID: '2A00',
value: nameToFan
}).then(function (result) {
console.log(typeof(nameToFan) + " " + nameToFan + "SUCCESS");
}, function (err) {
console.log("write error: " + err);
});
}