當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Adapter.on方法代碼示例

本文整理匯總了TypeScript中pc-ble-driver-js.Adapter.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Adapter.on方法的具體用法?TypeScript Adapter.on怎麽用?TypeScript Adapter.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pc-ble-driver-js.Adapter的用法示例。


在下文中一共展示了Adapter.on方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: startReading

  openAdapter$.subscribe((adapter: Adapter) => {

    adapter.on('error', (err) => {
      console.log('An error occurred ..');
      console.error(err);
    });

    startReading(adapter);
  });
開發者ID:ksachdeva,項目名稱:pc-ble-driver-js-examples,代碼行數:9,代碼來源:index.ts

示例2: onDeviceDiscovered

  return Observable.create((obs: Subscriber<Device>) => {

    function onDeviceDiscovered(device: Device) {
      obs.next(device);
    }

    adapter.on('deviceDiscovered', onDeviceDiscovered);

    adapter.startScan(scanOptions, (error) => {
      if (error) {
        obs.error(new ScanError(error));
        return;
      }
    });

    return () => {
      adapter.removeListener('deviceDiscovered', onDeviceDiscovered);
    };
  });
開發者ID:ksachdeva,項目名稱:rx-pc-ble-driver-js,代碼行數:19,代碼來源:index.ts

示例3: onDeviceConnected

  return Observable.create((obs: Subscriber<Device>) => {

    function onDeviceConnected(connectedDevice) {
      obs.next(connectedDevice);
      obs.complete();
    }

    adapter.on('deviceConnected', onDeviceConnected);

    adapter.connect(device.address, options, (err) => {
      if (err) {
        obs.error(new ConnectError(err, device));
        return;
      }
    });

    return () => {
      adapter.removeListener('deviceConnected', onDeviceConnected);
    };
  });
開發者ID:ksachdeva,項目名稱:rx-pc-ble-driver-js,代碼行數:20,代碼來源:index.ts

示例4: startApp

function startApp(adapater: Adapter) {
  console.log('Starting the app ...');

  adapterOne = adapater;

  let connect_in_progress = false;

  if (!listenersAdded) {
    adapterOne.on('logMessage', (severity, message) => {
      if (severity > 1)
        console.log(`#1 logMessage: ${message}`);
    });
    adapterOne.on('status', (status) => {
      console.log('#########################################');
      console.log(`#1 status: ${JSON.stringify(status)}`);
      console.log('#########################################');
    });
    adapterOne.on('error', error => {
      console.log('#########################################');
      console.log('#1 error: ' + JSON.stringify(error, null, 1));
      console.log('#########################################');
    });
    adapterOne.on('stateChanged', state => {
      console.log('#########################################');
      console.log('#1 stateChanged: ' + JSON.stringify(state));
      console.log('#########################################');
    });

    adapterOne.on('connParamUpdateRequest', (device: Device, requestedParameters) => {
      console.log('#1 connParamUpdateRequest: ' + JSON.stringify(device));
      console.log(requestedParameters);
    });

    adapterOne.on('deviceDisconnected', (device: Device) => {
      console.log('#1 deviceDisconnected: ' + JSON.stringify(device));
    });

    adapterOne.on('deviceConnected', (device: Device) => {
      console.log('#1 deviceConnected: ' + JSON.stringify(device));

      adapterOne.getServices(device.instanceId, (err, services) => {
        if (err) {
          console.error('failed to discover services');
        } else {
          // since we are connecitng to iPhone the discovery
          // will return many services
          console.log(services);

          // get the desired service and then discover characterisitc on it
          const desiredService = services.filter((s) => s.uuid === config.peripheralOptions.serviceUUID)[0];
          console.log(desiredService);

          adapterOne.getCharacteristics(desiredService.instanceId, (err, chars) => {
            console.log(chars);

            const desiredChar = chars.filter((c) => c.uuid === config.peripheralOptions.charUUID)[0];
            adapterOne.readCharacteristicValue(desiredChar.instanceId, (err, val) => {
              console.log(val);

              console.log('Press c to terminate the program ..');

            });

          });
        }
      });
    });

    adapterOne.on('deviceDiscovered', device => {

      console.log(`Discovered device: ${JSON.stringify(device)}`);

      if (device.name === config.peripheralOptions.deviceName) {
        // we issue stopScan
        adapterOne.stopScan(() => {
          console.log('issuing the connection ...');
          adapterOne.connect(device.address, config.connectionOptions, (err) => {
            if (err) {
              console.log('Failed during connection to the device', err);
              return;
            }
          });
        });
      }
    });

    listenersAdded = true;
  }

  adapterOne.open(config.adapaterOpenOptions, (error) => {
    if (error) {
      console.error('Error during adapater opening ..', error);
      return;
    }

    let advData = {
      completeLocalName: 'adapterOne',
      txPowerLevel: 20,
    };

//.........這裏部分代碼省略.........
開發者ID:ksachdeva,項目名稱:pc-ble-driver-js-examples,代碼行數:101,代碼來源:index.ts


注:本文中的pc-ble-driver-js.Adapter.on方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。