接收BLE中的通知

时间:2018-12-28 17:07:16

标签: react-native bluetooth-lowenergy

我尝试使用react-native-ble-manager将BLE设备连接到我的应用程序。 BLE设备应在发送消息后应答。我加载了另一个应用程序进行测试:我向另一个应用程序发送消息和设备答案。我尝试用自己的应用程序重现它,但没有答案。 这是我的代码:您知道我该如何解决吗?

import BleManager from 'react-native-ble-manager'; 
...

const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);

export default class App extends Component {
  constructor(){
    super()
...

async connectAndPrepare(peripheral) {

  var service = 'XXXXXXXX';
  var characNotif = 'XXXXXXXX';
  var characWrite = 'XXXXXXXX';
  var textToWrite = stringToBytes('XXXXXXXX');

  bleManagerEmitter.addListener(
   'BleManagerDidUpdateValueForCharacteristic',
  ({ value, peripheral, characNotif, service }) => {
      console.log("message received")
      const data = bytesToString(value);
      console.log(`Received ${data} for characteristic ${characNotif}`);
   }
  );

  await BleManager.connect(peripheral.id);
  await BleManager.retrieveServices(peripheral.id);
  await BleManager.startNotification(peripheral.id, service, characNotif);
  await BleManager.write(peripheral.id, service, characWrite, textToWrite);
}

0 个答案:

没有答案