1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

React Native BLE Major/Minor取得

Last updated at Posted at 2020-05-05

はじめに

前回の記事(https://qiita.com/yokobonbon/items/f17bac71e9ba02a713ac)
でReact NativeでBLEペリフェラルのスキャンを実現しました。今回はそこから、iBeaconのMajor/Minorデータ取得します。

Manufacture SpecificとiBeacon

以下のページが参考になります。
https://fabo.gitbooks.io/bledocs/content/nordic/beaconadvdata.html

iBeaconはUUID + Major(16ビット) + Minor(16ビット)で一意な端末を識別しますが、その情報は、BLE Advertised PacketのManufacture Specificデータの中に含まれます。Majro番号は25,26バイト目に格納され、Minor番号は26,27バイト目に格納されます。

Manufacture SpecificデータはBleManagerDiscoverPeripheralにおいて、advertisingの中のmanufactureDataに格納されています。
https://github.com/innoveit/react-native-ble-manager

  • id - String - the id of the peripheral
  • name - String - the name of the peripheral
  • rssi - Number - the RSSI value
  • advertising - JSON - the advertising payload, here are some examples:
    • isConnectable - Boolean
    • serviceUUIDs - Array of String
    • manufacturerData - JSON - contains the raw bytes and data (Base64 encoded string)
    • serviceData - JSON - contains the raw bytes and data (Base64 encoded string)
    • txPowerLevel - Int

manufacturerDataはbytesとdataが格納されているようですが、bytesの中の25、26バイト目がMajor番号、27、28バイト目がMinor番号が格納されています。

Major Minor番号の取得と表示

前回の記事に記述しているコードの差分になります。

      bleManagerEmitter.addListener(
        'BleManagerDiscoverPeripheral',
        (args) => {
            let major = 0;
            let minor = 0;
            major = args.advertising.manufacturerData.bytes[25] * 256 + args.advertising.manufacturerData.bytes[26];
            minor = args.advertising.manufacturerData.bytes[27] * 256 + args.advertising.manufacturerData.bytes[28];
            let peripheral = { 'id': args.id, 'name': args.name, 'major': major, 'minor': minor };
            this.discoveredPeripheralList.push(peripheral);
            this.setState({peripheralList: this.discoveredPeripheralList});
  renderPeripheral({ item }) {
    return (
      <View style={styles.peripheralList}>
        <Text style={styles.id_text}>ID: {item.id}</Text>
        <Text style={styles.id_text}>NAME: {item.name}</Text>
        <Text style={styles.id_text}>MAJOR: {item.major}</Text>
        <Text style={styles.id_text}>MINOR: {item.minor}</Text>
      </View>
    );
  }
スクリーンショット 2020-05-05 12.31.12.png
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?