Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
return new Promise( (resolve, reject) =>{
BleManager.connect(id)
.then( (peripheralInfo) => {
console.log('Connected peripheralInfo: ', peripheralInfo);
this.peripheralId = peripheralInfo.id;
this.getUUID(peripheralInfo);
if(this.nofityServiceUUID.length == 4 || this.nofityServiceUUID.length != 36 ||
this.nofityServiceUUID == '' || this.nofityCharacteristicUUID == '' ||
this.writeServiceUUID == '' ||this.writeCharacteristicUUID == ''){
return 'invalid';
}
return BleManager.startNotification(this.peripheralId, this.nofityServiceUUID, this.nofityCharacteristicUUID)
}).then((status) => {
this.isConnecting = false; //当前蓝牙连接结束
if(status == 'invalid'){
console.log('Notification error'); //连接成功但不能打开通知监听
// this.disconnect(); // 断开当前连接
reject(status);
function handleConnectPeripheral() {
console.log("handle connect to", peripheral);
BleManager.connect(peripheral.id).then(() => {
setBleState("connected");
changeCode({"type": "connect", "peripheral_id": peripheral.id});
setTimeout(() => {
BleManager.retrieveServices(peripheral.id).then((peripheralInfo) => {
console.log(peripheralInfo);
setTimeout(() => {
BleManager.startNotification(peripheral.id, service, contentsCharacteristic).then(() => {
console.log('Started notification on ' + peripheral.id);
setTimeout(() => {
BleManager.write(peripheral.id, service, filenameCharacteristic, stringToBytes("/code.py")).then(() => {
console.log('Wrote filename');
setFileState("nameSet");
});
connect = (peripheral_id) => {
BleManager.connect(peripheral_id)
.then(() => {
this.setState({
connected_peripheral: peripheral_id
});
prompt(
'Enter full name',
'Enter your full name to attend',
[
{
text: 'Cancel',
onPress: () => {
console.log('Cancel Pressed')
}
},
{
return new Promise( (resolve, reject) =>{
BleManager.connect(id)
.then(() => {
console.log('Connected success.');
return BleManager.retrieveServices(id);
})
.then((peripheralInfo)=>{
console.log('Connected peripheralInfo: ', peripheralInfo);
this.peripheralId = peripheralInfo.id;
this.getUUID(peripheralInfo);
this.isConnecting = false; //当前蓝牙连接结束
resolve(peripheralInfo);
})
.catch(error=>{
console.log('Connected error:',error);
this.isConnecting = false; //当前蓝牙连接结束
reject(error);
});
componentDidMount() {
this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleUpdateValueForCharacteristic );
BleManager.connect(this.state.device_id).then(() => {
let peripherals = this.state.peripherals;
let p = peripherals.get(this.state.device_id);
let id = this.state.device_id;
if (p) {
p.connected = true;
peripherals.set(id, p);
this.setState({peripherals});
}
console.log('Connected to ' + id);
setTimeout(() => {
BleManager.retrieveServices(id).then((peripheralInfo) => {
console.log(peripheralInfo);
var service = '4fafc201-1fb5-459e-8fcc-c5c9c331914b';
var tareCharacteristic = 'beb5483e-36e1-4688-b7f5-ea07361b26a8';
var loadCellCharacteristic = '4a78b8dd-a43d-46cf-9270-f6b750a717c8'; //calibrate as well
return new Promise( (resolve, reject) =>{
BleManager.connect(id)
.then( () => {
console.log('Connected');
return BleManager.retrieveServices(id)
}).then( (peripheralInfo)=>{
console.log('Connected peripheralInfo: ', peripheralInfo);
this.peripheralId = peripheralInfo.id;
this.getUUID(peripheralInfo);
if(this.nofityServiceUUID.length == 4 || this.nofityServiceUUID.length != 36 ||
this.nofityServiceUUID == '' || this.nofityCharacteristicUUID == '' ||
this.writeServiceUUID == '' ||this.writeCharacteristicUUID == ''){
return 'invalid';
}
return BleManager.startNotification(this.peripheralId, this.nofityServiceUUID, this.nofityCharacteristicUUID)
}).then((status) => {
this.isConnecting = false; //当前蓝牙连接结束