{"size": 5648, "ext": "js", "max_stars_repo_path": "example/App.js", "max_stars_repo_name": "N3TC4T/react-native-discovery", "max_stars_repo_stars_event_min_datetime": "2019-03-19T09:04:13.000Z", "max_stars_repo_stars_event_max_datetime": "2020-04-04T23:05:01.000Z", "max_issues_repo_path": "example/App.js", "max_issues_repo_name": "N3TC4T/react-native-discovery", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "example/App.js", "max_forks_repo_name": "N3TC4T/react-native-discovery", "max_forks_repo_forks_event_min_datetime": "2020-04-04T09:41:01.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-14T12:56:34.000Z"}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": 3, "avg_line_length": 25.2142857143, "max_line_length": 80, "alphanum_fraction": 0.5366501416}
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 * @lint-ignore-every XPLATJSCOPYRIGHT1
 */

import React, { Component } from "react";
import {
  Platform,
  StyleSheet,
  Text,
  View,
  FlatList,
  DeviceEventEmitter,
  PermissionsAndroid,
  AppState
} from "react-native";

import Discovery from "react-native-discovery";

const MY_UUID = "9bfdc12c-9d5b-40bd-9a9e-2f5e57f73d5a";
const SERVICE_NAME = "XRPTIP";

type Props = {};
export default class App extends Component<Props> {
  constructor() {
    super();

    this.state = {
      discovered: [],
      bluetoothStatus: "Unknonw",
      locationStatus: "Unknonw",
      paused: false,
      appState: "inactive"
    };
  }

  componentDidMount() {
    this.checkPermission();
    this.checkBluetooth();
    this.checkLocation();

    AppState.addEventListener("change", this.handleAppStateChange);

    Discovery.initialize(MY_UUID, SERVICE_NAME).then(uuid => {
      console.log("Discovery initialized!");
      Discovery.setShouldAdvertise(true);
      Discovery.setShouldDiscover(true);
    });

    // Listen for discovery changes
    Discovery.on("discoveredUsers", this.handleDiscover);
    // Listen for bluetooth state changes
    Discovery.on("bleStateChanged", this.handleBluethootState);
  }

  handleBluethootState = event => {
    const { paused } = this.state;
    this.setState({
      bluetoothStatus: event.isOn ? "true" : "false"
    });
  };

  handleDiscover = data => {
    const { discovered } = this.state;
    console.log(data);
    if (discovered !== data.users) {
      this.setState({
        discovered: data.users
      });
    }
    //slight callback discrepancy between the iOS and Android libraries
  };

  pause = value => {
    const { paused } = this.state;
    if (value) {
      if (!paused) {
        console.log("Pause the discvoery module");
        Discovery.setPaused(true);
        this.setState({
          paused: true
        });
      }
    } else {
      if (paused) {
        console.log("Continue the discovery module");
        Discovery.setPaused(false);
        this.setState({
          paused: false
        });
      }
    }
  };

  handleAppStateChange = nextAppState => {
    const { appState, paused } = this.state;
    if (appState.match(/inactive|background/) && nextAppState === "active") {
      this.pause(false);
    } else {
      this.pause(true);
    }
    this.setState({ appState: nextAppState });
  };

  checkBluetooth = () => {
    // if bluetooth is not enable enable it
    Discovery.isBluetoothEnabled().then(status => {
      if (status !== true) {
        if (Platform.OS === "android") {
          Discovery.setBluetoothOn(() => {});
        }
      }
    });
  };

  checkLocation = () => {
    if (Platform.OS === "android") {
      Discovery.isLocationEnabled().then(status => {
        this.setState({
          locationStatus: status ? "true" : "false"
        });
      });
    }
  };

  checkPermission = () => {
    if (Platform.OS === "android" && Platform.Version >= 23) {
      PermissionsAndroid.check(
        PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION
      ).then(result => {
        if (result) {
          console.log("Permission is OK: true");
          return true;
        } else {
          PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION
          ).then(result => {
            if (result) {
              console.log("User accept permission!");
              return true;
            } else {
              console.log("User refuse permission!");
              return false;
            }
          });
        }
      });
    }
  };

  render() {
    const { discovered, bluetoothStatus, locationStatus } = this.state;

    return (
      <View style={styles.container}>
        <View style={{ flex: 1, backgroundColor: "#c2c3c4", ...styles.center }}>
          <Text style={{ textAlign: "center", fontSize: 18 }}>
            React Native Discovery
          </Text>
        </View>
        <View style={{ flex: 1, backgroundColor: "#E9EBEE", ...styles.center }}>
          <Text style={{ textAlign: "center" }}>
            Bluetooth is On:{" "}
            <Text
              style={{ color: bluetoothStatus === "true" ? "green" : "red" }}
            >
              {bluetoothStatus}
            </Text>
          </Text>
          <Text style={{ textAlign: "center" }}>
            Location is On:{" "}
            <Text
              style={{ color: locationStatus === "true" ? "green" : "red" }}
            >
              {locationStatus}
            </Text>
          </Text>
          <Text style={{ textAlign: "center" }}>
            My UUID : <Text style={{ color: "orange" }}>{MY_UUID}</Text>
          </Text>
        </View>
        <View style={{ flex: 6 }}>
          <FlatList
            data={discovered}
            renderItem={({ item }) => (
              <Text style={{ color: "green", ...styles.item }}>
                {item.uuid}
              </Text>
            )}
            keyExtractor={(item, index) => item.uuid}
            ListEmptyComponent={
              <Text style={{ color: "red", ...styles.item }}>
                No user discovered!
              </Text>
            }
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: "column",
    backgroundColor: "#F5FCFF"
  },
  center: {
    justifyContent: "center",
    alignItems: "center"
  },
  item: {
    marginTop: 20,
    textAlign: "center",
    fontSize: 15,
    fontWeight: "900"
  }
});
