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);
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) => {
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 peripheralReducer(state, action) {
if (action.action == "add") {
let peripheral = action.peripheral;
console.log(peripheral);
if (state.has(peripheral.id)) {
return state; // No state change
}
if (!peripheral.advertising.serviceUUIDs || peripheral.advertising.serviceUUIDs.length == 0 || peripheral.advertising.serviceUUIDs[0].toLowerCase() != 'adaf0100-4369-7263-7569-74507974686e') {
return state;
}
console.log(peripheral);
var newMap = new Map(state);
newMap.set(peripheral.id, peripheral);
return newMap;
} else if (action.action == "scan") {
BleManager.scan([], 3).then((results) => {
console.log('Scanning...');
});
} else if (action.action == "clear") {
state.clear();
}
return state;
}
disconnect() {
BleManager.disconnect(this.peripheralId)
.then( () => {
console.log('Disconnected');
})
.catch( (error) => {
console.log('Disconnected error:',error);
});
/* return new Promise( (resolve, reject) =>{
BleManager.disconnect(this.peripheralId)
.then( () => {
console.log('Disconnected');
resolve();
})
.catch( (error) => {
console.log('Disconnected error:',error);
reject(error);
});
useEffect(() => {
console.log("new blestate", bleState);
if (bleState == "permOk") {
} else if (bleState == "started") {
changePeripherals({"action": "clear"});
BleManager.getConnectedPeripherals([]).then((peripheralsArray) => {
for (p of peripheralsArray) {
console.log(p);
p.connected = true;
BleManager.connect(p.id).then(() => {
BleManager.retrieveServices(p.id).then((peripheralInfo) => {
console.log(peripheralInfo);
p.advertising.serviceUUIDs = [peripheralInfo.services[2].uuid];
changePeripherals({"action": "add", "peripheral": p});
});
});
}
console.log('Connected peripherals: ' + peripheralsArray.length);
});
changePeripherals({"action": "scan"});
} else if (bleState == "disconnected") {
// set a timeout and try to reconnect
let totalLength = 2 + 2 + 4 + 4 + 4 + encodedInsert.length
let patch = new ArrayBuffer(totalLength);
let view = new DataView(patch);
view.setUint16(0, totalLength, true);
view.setUint16(2, 2, true);
view.setUint32(4, action.offset, true);
view.setUint32(8, action.oldValue.length, true);
view.setUint32(12, encodedInsert.length, true);
let byteView = new Uint8Array(patch, 16, encodedInsert.length);
byteView.set(encodedInsert);
// React native bridging can't handle Uint8Array so copy into a normal array.
let finalPatch = Array.from(new Uint8Array(patch));
if (state.peripheral_id) {
console.log("writing patch", finalPatch, patch);
BleManager.write(state.peripheral_id, service, contentsCharacteristic, finalPatch).then(() => {
console.log('Wrote patch to device');
});
} else {
console.log("no peripheral", newState.queue);
newState.queue.push(patch);
}
console.log("merging together", action, state.code);
newState.code = state.code.substring(0, action.offset) + action.newValue + state.code.substring(action.offset + action.oldValue.length, state.code.length);
}
console.log("new code state", newState);
return newState;
}
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,
getBondedPeripherals(){
BleManager.getBondedPeripherals([])
.then((bondedPeripheralsArray) => {
// Each peripheral in returned array will have id and name properties
console.log('Bonded peripherals: ' + bondedPeripheralsArray);
});
}
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");
});