npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-scy-nbc

v1.1.2

Published

[![npm version](https://img.shields.io/npm/v/react-native-scy-nbc.svg?style=flat)](https://www.npmjs.com/package/react-native-scy-nbc)

Downloads

4

Readme

NfcBleManager

A Bluetooth, NFC package library, Bluetooth module can be scanned, linked, read and other operations. NFC module can get mobile phone NFC state, and can read IOS14443A type Tag card.

react-native-scy-nbc

npm version

RN版本

RN 0.40+

Android版本

  • Android (API 18+)

安装

npm i react-native-scy-nbc --save 

链接原生代码库

react-native link rreact-native-scy-nbc

android/app/build.gradle:

// file: android/app/build.gradle
...

android {
    ...

    defaultConfig {
        ...
        minSdkVersion 18 // <--- make sure this is 18 or greater
        ...
    }
    ...
}

连接原生库成功后,检查如下文件是否正确

// file: android/settings.gradle
...

include ':react-native-scy-nbc'
project(':react-native-scy-nbc').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-scy-nbc/android')
// file: android/app/build.gradle
...

android {
    ...

    defaultConfig {
        ...
        minSdkVersion 18 // <--- 最低版本为18
        ...
    }
    ...
}

dependencies {
    ...
    compile project(':react-native-ble-manager')
}
...
import com.nfcblemanager.bluetooth.NfcBleManagerPackage; // <--- 导入包,注意查看路径是否正确

public class MainApplication extends Application implements ReactApplication {

    ...

    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new NfcBleManagerPackage() // <------ 添加包
        );
    }

    ...
}
修改 Android Manifest
// file: android/app/src/main/AndroidManifest.xml
...
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.NFC" />
...
...
   <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop" // <--- 1.设置启动方式
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize"> 
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        // 2.添加如下filter
        <intent-filter>  
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />  
        </intent-filter>  
        <intent-filter>  
            <action android:name="android.nfc.action.TAG_DISCOVERED" >  
            </action>  
            <category android:name="android.intent.category.DEFAULT" >  
            </category>  
        </intent-filter>  
        <intent-filter>  
            <action android:name="android.nfc.action.TECH_DISCOVERED" />  
        </intent-filter>  
        // 3.配置扫描过滤
        <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/nfc_tech_filter" />
      </activity>
...
...
   
        在app/src/res/下创建xml文件夹,并在该文件夹下创建fc_tech_filter.xml文件:
        <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
            <tech-list>
                <tech>android.nfc.tech.IsoDep</tech>  
                <tech>android.nfc.tech.NfcA</tech>    
                <tech>android.nfc.tech.NfcB</tech>    
                <tech>android.nfc.tech.NfcF</tech>    
                <tech>android.nfc.tech.NfcV</tech>    
                <tech>android.nfc.tech.Ndef</tech>    
                <tech>android.nfc.tech.NdefFormatable</tech>    
                <tech>android.nfc.tech.MifareUltralight</tech>   
                <tech>android.nfc.tech.MifareClassic</tech>  
            </tech-list>
        </resources>
...

注意

  • 记住在任何事情之前使用start方法。
  • 避免在扫描期间连接/读取/写入外设。
  • 读写之前,需要调用retrieveServices方法。

代码示例

示例 .

NFC

注册数据接受回调

用于接收手机NFC状态、读取NFC数据。

示例

    componentDidMount() {
        // NFC状态
        DeviceEventEmitter.addListener('onNfcStatus', function(status) {
            ToastAndroid.show(status, ToastAndroid.SHORT);
        })
        // NFC扫描读取的Tag数据
        DeviceEventEmitter.addListener('onNfcScanResult', function(result) {
            ToastAndroid.show(result, ToastAndroid.SHORT);
        })
    }

startNfc()

检测手机NFC功能状态

示例

  _checkNfc() {
    Manager.startNfc();
  }

readNfc()

启动NFC读取功能,启动后,将Tag放入扫描区即可读取。

示例

  _startRead() {
    Manager.readNfc();
  }

蓝牙

start

在使用蓝牙前,需要调用start启动。

示例

    componentDidMount() {
        Manager.start({showAlert: false, allowDuplicates: false});
    }

enableBluetooth

检测手机蓝牙状态,例如蓝牙是否可用,是否开启,未开启状态下会申请蓝牙权限,并自动开启蓝牙功能。

示例

     Manager.enableBluetooth()
          .then(() => {
            alert('蓝牙已开启,可以使用');
          })
          .catch((error) => {
            alert('用户拒绝授权蓝牙权限');
    });

scan(serviceUUID,seconds,true)

扫描外部蓝牙设备

参数

  • serviceUUID 传- [] - 即可
  • seconds 扫描时长,以秒计算.

示例

 Manager.scan([], 6, true).then((results) => {
        ToastAndroid.show("扫描中..." + results,ToastAndroid.SHORT);
      });

stopScan

停止扫描外部蓝牙设备

示例

Manager.stopScan()
  .then(() => {
     ToastAndroid.show("停止扫描..." + results,ToastAndroid.SHORT);
  });

connect(peripheralId)

链接外部蓝牙设备

参数

  • peripheralId 蓝牙设备Id

示例

      Manager.connect(peripheral.id)
        .then(() => {
          ToastAndroid.show('连接外部设备'+peripheral.id, ToastAndroid.SHORT);
        })
        .catch((error) => {
          ToastAndroid.show('连接错误:'+error, ToastAndroid.SHORT);
        });

disconnect(peripheralId)

断开外部蓝牙设备链接

参数

  • peripheralId 蓝牙设备Id

示例

     Manager.disconnect(peripheral.id);

retrieveServices(peripheralId)

注册蓝牙设备服务,检索外设的service和characteristic。

参数

  • peripheralId 蓝牙设备Id

示例

    Manager.retrieveServices(peripheral.id).then((peripheralData) => {
          ToastAndroid.show('检索的外部服务' + JSON.stringify(peripheralData), ToastAndroid.SHORT);
    });

readRSSI(peripheralId)

读取当前链接蓝牙设备的RSSI值

参数

  • peripheralId 蓝牙设备Id

示例

    Manager.readRSSI(peripheral.id).then((rssi) => {
        ToastAndroid.show('检索实际RSSI值'+rssi, ToastAndroid.SHORT);
    });

read(peripheralId,serviceUUID,characteristicUUID)

读取当前链接蓝牙设备的数据

参数

  • peripheralId 蓝牙设备Id
  • serviceUUID 服务的UUID。
  • characteristicUUID 特性的UUID。

示例

   Manager.read('peripheralId', 'serviceUUID', 'characteristicUUID')
      .then((readData) => {
        ToastAndroid.show('读取到数据:'+readData, ToastAndroid.SHORT);
      })
      .catch((error) => {
        ToastAndroid.show('读取数据失败:'+error, ToastAndroid.SHORT);
      });

write(peripheralId, serviceUUID, characteristicUUID, data, maxByteSize)

Write with response to the specified characteristic. Returns a Promise object.

参数

  • peripheralId 外设的id / mac地址。
  • serviceUUID 服务的UUID。
  • characteristicUUID characteristic的UUID。
  • data 要写入的数据,数组。
  • maxByteSize 在分割消息之前指定最大字节大小

示例

Manager.write('peripheralId', 'serviceUUID', 'characteristicUUID', data)
  .then(() => {
    ToastAndroid.show('成功写入的数据: '+data, ToastAndroid.SHORT);
  })
  .catch((error) => {
     ToastAndroid.show('写入数据失败:'+error, ToastAndroid.SHORT);
  });

getConnectedPeripherals(serviceUUIDs)

获取已连接的蓝牙设备。

参数

  • serviceUUIDs -寻找的服务的UUID。

示例

Manager.getConnectedPeripherals([])
  .then((peripheralsArray) => {
    ToastAndroid.show('所有已连接的蓝牙设备 : ' + peripheralsArray.length);
  });

回调事件

BleManagerStopScan

停止扫描回调监听。

示例

bleManagerEmitter.addListener(
    'BleManagerStopScan',
    () => {
        // Scanning is stopped
    }
);

BleManagerDidUpdateState

蓝色状态发生改变。

示例

bleManagerEmitter.addListener(
    'BleManagerDidUpdateState',
    (status) => {
        ToastAndroid.show('当前蓝牙状态为:'+status, ToastAndroid.SHORT);
    }
);

BleManagerDiscoverPeripheral

扫描到蓝牙设备

示例

bleManagerEmitter.addListener(
    'BleManagerDiscoverPeripheral',
    (peripheral) => {
        ToastAndroid.show('蓝牙Id为:' + peripheral.id + '蓝牙名称为:' + peripheral.name, ToastAndroid.SHORT);
    }
);

BleManagerDidUpdateValueForCharacteristic

Characteristic通知新值。

示例

bleManagerEmitter.addListener(
    'BleManagerDidUpdateValueForCharacteristic',
    (data) => {
        ToastAndroid.show('收到数据:' + data.value + '蓝牙设备:' + data.peripheral + '蓝牙characteristicUUID:' + data.characteristic, ToastAndroid.SHORT);
    }
);

BleManagerConnectPeripheral

外设已连接。

示例

bleManagerEmitter.addListener(
    'BleManagerConnectPeripheral',
    (peripheralId) => {
    
    }
);

BleManagerDisconnectPeripheral

外设已断开。

示例

bleManagerEmitter.addListener(
    'BleManagerDisconnectPeripheral',
    (peripheralId) => {
    
    }
);