如何使用react-native-ble-manager库获取服务UUID和特征性UUID的列表?

时间:2018-07-24 10:33:17

标签: react-native bluetooth-lowenergy

我已经连接了设备,想开始有关特定服务和特性的通知,但是我无法获得确切的serviceuuid和featureuuid。

test(peripheral) {
if (peripheral){
  if (peripheral.connected){
    BleManager.disconnect(peripheral.id);
  }else{
    BleManager.connect(peripheral.id)
    .then(() => {
      let peripherals = this.state.peripherals;
      let p = peripherals.get(peripheral.id);
      if (p) {
        p.connected = true;
        peripherals.set(peripheral.id, p);
        this.setState({peripherals});
      }

      setTimeout(() => {
        BleManager.retrieveServices(peripheral.id)
          .then((peripheralInfo) => {
        //  console.log("peripheralInfo-->>>  ",peripheralInfo['characteristics']);
          var service = '0000fee0-0000-1000-8000-00805f9b34fb';
        //  var bakeCharacteristic = '13333333-3333-3333-3333-333333330003';
          var Characteristic = '00000006-0000-3512-2118-0009af100700';//peripheralInfo['characteristics'][0]['characteristic'];

          setTimeout(() => {
            BleManager.startNotification(peripheral.id, service, Characteristic).then(() => {
              console.log('Started notification on ' + peripheral.id);
              setTimeout(() => {

                  BleManager.read(peripheral.id, service, Characteristic).then((readData) => {
                    return(
                      <Text>readData</Text>
                    )
                    var data = Array.from(byteArray, function(byte) {
                          return ('0' + (byte & 0xFF).toString(16)).slice(-2);
                    }).join('')
                    console.log(data);
                });

                //  BleManager.write(peripheral.id, service, Characteristic, [100]).then(() => {
                //    console.log('Write: ' + data);
                //  });

              }, 500);
            }).catch((error) => {
              console.log('Notification error', error);
            });
          }, 200);
        });

      }, 900);
    }).catch((error) => {
      console.log('Connection error', error);
    });

  }
}
}

这是我连接设备然后启动通知的功能。

如何启动有关所有服务和特征的通知?

0 个答案:

没有答案