Я пытаюсь создать приложение с функцией, которая будет изменять имя устройства определенной парной периферии.Протокол, который был отправлен мне, не говорит мне, какой тип данных отправляется на периферию, чтобы иметь возможность изменить это.В микропрограмме этого периферийного устройства у вас есть файл с именем 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);
});
}