|
|
@ -1,5 +1,5 @@ |
|
|
|
import React, { Component } from 'react'; |
|
|
|
import { View, TouchableOpacity, Text, FlatList, InteractionManager, RefreshControl, ScrollView } from 'react-native'; |
|
|
|
import { View, TouchableOpacity, NativeEventEmitter, NativeModules, Text, FlatList, InteractionManager, RefreshControl, ScrollView } from 'react-native'; |
|
|
|
import { BlueLoading, SafeBlueArea, WalletsCarousel, BlueList, BlueHeaderDefaultMain, BlueTransactionListItem } from '../../BlueComponents'; |
|
|
|
import { Icon } from 'react-native-elements'; |
|
|
|
import { NavigationEvents } from 'react-navigation'; |
|
|
@ -7,6 +7,9 @@ import ReactNativeHapticFeedback from 'react-native-haptic-feedback'; |
|
|
|
import PropTypes from 'prop-types'; |
|
|
|
import WalletGradient from '../../class/walletGradient'; |
|
|
|
import OnAppLaunch from '../../class/onAppLaunch'; |
|
|
|
import BleManager from 'react-native-ble-manager'; |
|
|
|
const BleManagerModule = NativeModules.BleManager; |
|
|
|
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule); |
|
|
|
let EV = require('../../events'); |
|
|
|
let A = require('../../analytics'); |
|
|
|
/** @type {AppStorage} */ |
|
|
@ -38,15 +41,32 @@ export default class WalletsList extends Component { |
|
|
|
isFlatListRefreshControlHidden: true, |
|
|
|
wallets: BlueApp.getWallets().concat(false), |
|
|
|
lastSnappedTo: 0, |
|
|
|
scanning:false, |
|
|
|
peripherals: new Map(), |
|
|
|
}; |
|
|
|
EV(EV.enum.WALLETS_COUNT_CHANGED, this.redrawScreen.bind(this)); |
|
|
|
|
|
|
|
// here, when we receive TRANSACTIONS_COUNT_CHANGED we do not query
|
|
|
|
// remote server, we just redraw the screen
|
|
|
|
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED, this.redrawScreen.bind(this)); |
|
|
|
|
|
|
|
this.handleDiscoverPeripheral = this.handleDiscoverPeripheral.bind(this); |
|
|
|
this.handleStopScan = this.handleStopScan.bind(this); |
|
|
|
this.handleUpdateValueForCharacteristic = this.handleUpdateValueForCharacteristic.bind(this); |
|
|
|
this.handleDisconnectedPeripheral = this.handleDisconnectedPeripheral.bind(this); |
|
|
|
this.handleAppStateChange = this.handleAppStateChange.bind(this); |
|
|
|
} |
|
|
|
|
|
|
|
componentDidMount() { |
|
|
|
|
|
|
|
BleManager.start({showAlert: true}).then((result) => this.startScan()); |
|
|
|
|
|
|
|
this.handlerDiscover = bleManagerEmitter.addListener('BleManagerDiscoverPeripheral', this.handleDiscoverPeripheral ); |
|
|
|
this.handlerStop = bleManagerEmitter.addListener('BleManagerStopScan', this.handleStopScan ); |
|
|
|
this.handlerDisconnect = bleManagerEmitter.addListener('BleManagerDisconnectPeripheral', this.handleDisconnectedPeripheral ); |
|
|
|
this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleUpdateValueForCharacteristic ); |
|
|
|
|
|
|
|
|
|
|
|
this.redrawScreen(); |
|
|
|
// the idea is that upon wallet launch we will refresh
|
|
|
|
// all balances and all transactions here:
|
|
|
@ -76,6 +96,152 @@ export default class WalletsList extends Component { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
handleDisconnectedPeripheral(data) { |
|
|
|
let peripherals = this.state.peripherals; |
|
|
|
let peripheral = peripherals.get(data.peripheral); |
|
|
|
if (peripheral) { |
|
|
|
peripheral.connected = false; |
|
|
|
peripherals.set(peripheral.id, peripheral); |
|
|
|
this.setState({peripherals}); |
|
|
|
} |
|
|
|
console.log('Disconnected from ' + data.peripheral); |
|
|
|
} |
|
|
|
|
|
|
|
handleUpdateValueForCharacteristic(data) { |
|
|
|
console.log('Received data from ' + data.peripheral + ' characteristic ' + data.characteristic, data.value); |
|
|
|
} |
|
|
|
|
|
|
|
handleStopScan() { |
|
|
|
console.log('Scan is stopped'); |
|
|
|
this.setState({ scanning: false }); |
|
|
|
} |
|
|
|
|
|
|
|
startScan() { |
|
|
|
if (!this.state.scanning) { |
|
|
|
console.warn('HERE') |
|
|
|
//this.setState({peripherals: new Map()});
|
|
|
|
BleManager.scan([], 3, true).then((results) => { |
|
|
|
console.log('Scanning...'); |
|
|
|
console.warn(results) |
|
|
|
BleManager.connect('3EE7D2BD-0D0E-BBEC-7FC4-F53BBB23F6BB') |
|
|
|
.then(() => { |
|
|
|
// Success code
|
|
|
|
console.log('Connected'); |
|
|
|
}) |
|
|
|
.catch((error) => { |
|
|
|
// Failure code
|
|
|
|
console.log(error); |
|
|
|
}); |
|
|
|
this.setState({scanning:true}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
retrieveConnected(){ |
|
|
|
BleManager.getConnectedPeripherals([]).then((results) => { |
|
|
|
if (results.length == 0) { |
|
|
|
console.log('No connected peripherals') |
|
|
|
} |
|
|
|
console.log(results); |
|
|
|
var peripherals = this.state.peripherals; |
|
|
|
for (var i = 0; i < results.length; i++) { |
|
|
|
var peripheral = results[i]; |
|
|
|
peripheral.connected = true; |
|
|
|
peripherals.set(peripheral.id, peripheral); |
|
|
|
this.setState({ peripherals }); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
handleAppStateChange(nextAppState) { |
|
|
|
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') { |
|
|
|
console.log('App has come to the foreground!') |
|
|
|
BleManager.getConnectedPeripherals([]).then((peripheralsArray) => { |
|
|
|
console.log('Connected peripherals: ' + peripheralsArray.length); |
|
|
|
}); |
|
|
|
} |
|
|
|
this.setState({appState: nextAppState}); |
|
|
|
} |
|
|
|
|
|
|
|
handleDiscoverPeripheral(peripheral){ |
|
|
|
var peripherals = this.state.peripherals; |
|
|
|
console.log('Got ble peripheral', peripheral); |
|
|
|
if (!peripheral.name) { |
|
|
|
peripheral.name = 'NO NAME'; |
|
|
|
} |
|
|
|
peripherals.set(peripheral.id, peripheral); |
|
|
|
this.setState({ peripherals }); |
|
|
|
} |
|
|
|
|
|
|
|
test(peripheral) { |
|
|
|
if (peripheral){ |
|
|
|
if (peripheral.connected){ |
|
|
|
BleManager.disconnect(peripheral.id); |
|
|
|
}else{ |
|
|
|
BleManager.connect(peripheral.id).then(() => { |
|
|
|
let peripherals = this.state.peripherals; |
|
|
|
let p = peripherals.get(peripheral.id); |
|
|
|
if (p) { |
|
|
|
p.connected = true; |
|
|
|
peripherals.set(peripheral.id, p); |
|
|
|
this.setState({peripherals}); |
|
|
|
} |
|
|
|
console.log('Connected to ' + peripheral.id); |
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
|
|
/* Test read current RSSI value |
|
|
|
BleManager.retrieveServices(peripheral.id).then((peripheralData) => { |
|
|
|
console.log('Retrieved peripheral services', peripheralData); |
|
|
|
BleManager.readRSSI(peripheral.id).then((rssi) => { |
|
|
|
console.log('Retrieved actual RSSI value', rssi); |
|
|
|
}); |
|
|
|
});*/ |
|
|
|
|
|
|
|
// Test using bleno's pizza example
|
|
|
|
// https://github.com/sandeepmistry/bleno/tree/master/examples/pizza
|
|
|
|
BleManager.retrieveServices(peripheral.id).then((peripheralInfo) => { |
|
|
|
console.log(peripheralInfo); |
|
|
|
var service = '13333333-3333-3333-3333-333333333337'; |
|
|
|
var bakeCharacteristic = '13333333-3333-3333-3333-333333330003'; |
|
|
|
var crustCharacteristic = '13333333-3333-3333-3333-333333330001'; |
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
BleManager.startNotification(peripheral.id, service, bakeCharacteristic).then(() => { |
|
|
|
console.log('Started notification on ' + peripheral.id); |
|
|
|
setTimeout(() => { |
|
|
|
BleManager.write(peripheral.id, service, crustCharacteristic, [0]).then(() => { |
|
|
|
console.log('Writed NORMAL crust'); |
|
|
|
BleManager.write(peripheral.id, service, bakeCharacteristic, [1,95]).then(() => { |
|
|
|
console.log('Writed 351 temperature, the pizza should be BAKED'); |
|
|
|
/* |
|
|
|
var PizzaBakeResult = { |
|
|
|
HALF_BAKED: 0, |
|
|
|
BAKED: 1, |
|
|
|
CRISPY: 2, |
|
|
|
BURNT: 3, |
|
|
|
ON_FIRE: 4 |
|
|
|
};*/ |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
}, 500); |
|
|
|
}).catch((error) => { |
|
|
|
console.log('Notification error', error); |
|
|
|
}); |
|
|
|
}, 200); |
|
|
|
}); |
|
|
|
|
|
|
|
}, 900); |
|
|
|
}).catch((error) => { |
|
|
|
console.log('Connection error', error); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Forcefully fetches TXs and balance for lastSnappedTo (i.e. current) wallet. |
|
|
|
* Triggered manually by user on pull-to-refresh. |
|
|
|