React-native-ble特性写入错误:'无效参数不满足:data!= nil'

时间:2017-04-08 12:11:31

标签: react-native bluetooth-lowenergy

我尝试使用react-native-ble而我遇到了第一个问题:缓冲区未定义,因此我在调用react-native-之前添加了shim.js虽然我有这个错误:

[fatal][tid:com.facebook.react.RNBLEQueue] Exception 'Invalid parameter not satisfying: data != nil' was thrown while invoking write on target RNBLE with params (
    "AA542816-1CD6-8FF8-2766-BCBF89A4A634",
    "4B48E81D-BFBD-4334-8CB5-1F941E00AA50",
    "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFF4",
    Samoud,
    1
)

这是我的代码:

return new Promise(function(resolve, reject) {
      self._connectDevice(peripheral, id)
      .then((characteristic) => {
        // data = 'Samoud'
        characteristic.write(data, true, function(error) {
          if (error) reject(error);
          resolve(true);
        });
      })
      .catch((error) => reject(error));
    });

请知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

要写入的数据必须是缓冲区。你已经通过了字符串。另外我发现如果第二个参数为真,则写入不成功。

试试这段代码:---

return new Promise(function(resolve, reject) {
  self._connectDevice(peripheral, id)
  .then((characteristic) => {
    // data = 'Samoud'
    characteristic.write(new Buffer('Samoud'), false, function(error) {
      if (error) reject(error);
      resolve(true);
    });
  })
  .catch((error) => reject(error));
});