UNPKG

autobots-lib

Version:

汽车人基础库

96 lines (73 loc) 1.57 kB
'use strict'; import React,{Component} from 'react'; import ReactNative from 'react-native'; import Native from '../native'; import Toast from './toast'; import PropTypes from 'prop-types'; const { StyleSheet, requireNativeComponent, NativeModules, DeviceEventEmitter, Platform, View, Text, } = ReactNative; let openNative = function(callback) { let url = 'autobots://rn/scan'; Native.callNative(url,function(result,data){ if(callback) { callback(result,data); } }); } class Me extends Component { start(callback){ openNative(callback); } componentWillMount() { var that = this; function cb_android(e:Event) { let result; try{ result = JSON.parse(e.result); }catch(ex){ result = e.result; } if(that.props.onCallBack) { that.props.onCallBack(result); } } function cb_ios(p) { if(that.props.onCallBack) { that.props.onCallBack(p); } } if(Platform.OS==='android') { this.sinListener = DeviceEventEmitter.addListener("react_event_scan_result",cb_android); }else { this.sinListener = DeviceEventEmitter.addListener("react_event_scan_result",cb_ios); } } componentWillUnmount() { if(this.sinListener!=null) { this.sinListener.remove(); this.sinListener = null; } } render() { return(<View/>); } } Me.propTypes= { onCallBack: PropTypes.func.isRequired, } module.exports = Me;