Put bluetooth scripts in the right place
This commit is contained in:
@@ -6,7 +6,6 @@ import {
|
|||||||
} from '@ionic/vue';
|
} from '@ionic/vue';
|
||||||
import { chevronForward } from 'ionicons/icons';
|
import { chevronForward } from 'ionicons/icons';
|
||||||
import { onBeforeMount, ref, watch } from 'vue';
|
import { onBeforeMount, ref, watch } from 'vue';
|
||||||
import updater from '@/utils/updater';
|
|
||||||
|
|
||||||
const emit = defineEmits<{ (e: "connect"): void }>();
|
const emit = defineEmits<{ (e: "connect"): void }>();
|
||||||
const bleStore = useBluetoothStore();
|
const bleStore = useBluetoothStore();
|
||||||
@@ -14,17 +13,8 @@ const bleStore = useBluetoothStore();
|
|||||||
const devices = ref<BLECentralPlugin.PeripheralData[]>([]);
|
const devices = ref<BLECentralPlugin.PeripheralData[]>([]);
|
||||||
|
|
||||||
const connect = (device: BLECentralPlugin.PeripheralData) => {
|
const connect = (device: BLECentralPlugin.PeripheralData) => {
|
||||||
ble.connect(device.id,
|
bleStore.connect(device);
|
||||||
(data: BLECentralPlugin.PeripheralDataExtended) => {
|
emit("connect");
|
||||||
console.log(`Connected: ${data}`);
|
|
||||||
updater.obj.setDeviceId(device.id);
|
|
||||||
updater.device = device.id;
|
|
||||||
emit("connect");
|
|
||||||
},
|
|
||||||
(error: string | BLECentralPlugin.BLEError) => {
|
|
||||||
console.error(`Connection error: ${error}`);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const scan = () => {
|
const scan = () => {
|
||||||
@@ -32,22 +22,7 @@ const scan = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onBeforeMount(async () => {
|
onBeforeMount(async () => {
|
||||||
ble.enable(
|
bleStore.enableBLE();
|
||||||
() => {
|
|
||||||
console.log("BLE enalbed")
|
|
||||||
},
|
|
||||||
() => {
|
|
||||||
console.log("BLE not enalbed")
|
|
||||||
}
|
|
||||||
);
|
|
||||||
ble.isEnabled(
|
|
||||||
() => {
|
|
||||||
console.log("BLE enabled.");
|
|
||||||
},
|
|
||||||
() => {
|
|
||||||
console.log("BLE connect error.");
|
|
||||||
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(() => bleStore.devices, (newValue) => {devices.value = newValue}, {immediate: true});
|
watch(() => bleStore.devices, (newValue) => {devices.value = newValue}, {immediate: true});
|
||||||
|
|||||||
@@ -4,9 +4,27 @@ import updater from "@/utils/updater";
|
|||||||
|
|
||||||
const useBluetoothStore = defineStore('bluetooth', {
|
const useBluetoothStore = defineStore('bluetooth', {
|
||||||
state: () => (
|
state: () => (
|
||||||
{ devices: <BLECentralPlugin.PeripheralData[]>[], device: <BLECentralPlugin.PeripheralData>{}}
|
{ devices: <BLECentralPlugin.PeripheralData[]>[], device: <BLECentralPlugin.PeripheralData | undefined>{}}
|
||||||
),
|
),
|
||||||
actions: {
|
actions: {
|
||||||
|
async enableBLE(): Promise<boolean> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
ble.enable(
|
||||||
|
() => {
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
reject("Failed to enable");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
ble.isEnabled(
|
||||||
|
() => {
|
||||||
|
resolve(true);
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
reject("Failed to enable");
|
||||||
|
});
|
||||||
|
})
|
||||||
|
},
|
||||||
scan(): void {
|
scan(): void {
|
||||||
this.devices = [];
|
this.devices = [];
|
||||||
ble.scan([], 5, (device: BLECentralPlugin.PeripheralData) => {
|
ble.scan([], 5, (device: BLECentralPlugin.PeripheralData) => {
|
||||||
@@ -16,8 +34,31 @@ const useBluetoothStore = defineStore('bluetooth', {
|
|||||||
console.log(e)
|
console.log(e)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
connect(): void {
|
async connect(device: BLECentralPlugin.PeripheralData): Promise<boolean> {
|
||||||
|
return new Promise( (resolve, reject) => {
|
||||||
|
ble.connect(device.id,
|
||||||
|
(data: BLECentralPlugin.PeripheralDataExtended) => {
|
||||||
|
console.log(`Connected: ${data}`);
|
||||||
|
updater.obj.setDeviceId(device.id);
|
||||||
|
this.device = device;
|
||||||
|
resolve(true);
|
||||||
|
},
|
||||||
|
(error: string | BLECentralPlugin.BLEError) => {
|
||||||
|
console.error(`Connection error: ${error}`);
|
||||||
|
reject(false);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async disconnect(): Promise<boolean> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
ble.disconnect(this.device?.id as string, () => {
|
||||||
|
this.device = undefined;
|
||||||
|
resolve(true);
|
||||||
|
}, (err: string | BLECentralPlugin.BLEError) => {
|
||||||
|
reject(err);
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import Updater from "updaterweblibrary";
|
import Updater from "updaterweblibrary";
|
||||||
|
|
||||||
const updater = {obj: new Updater(), device: ""}
|
const updater = {obj: new Updater()}
|
||||||
|
|
||||||
export default updater;
|
export default updater;
|
||||||
@@ -32,11 +32,13 @@ import ArchiveCard from "@/components/ArchiveCard.vue";
|
|||||||
import FlashCard from "@/components/FlashCard.vue";
|
import FlashCard from "@/components/FlashCard.vue";
|
||||||
import {ref} from "vue";
|
import {ref} from "vue";
|
||||||
import SettingsCard from "@/components/SettingsCard.vue";
|
import SettingsCard from "@/components/SettingsCard.vue";
|
||||||
import updater from '@/utils/updater';
|
import useBluetoothStore from '@/stores/bluetooth';
|
||||||
|
|
||||||
let prevCard = 0;
|
let prevCard = 0;
|
||||||
const shownCard = ref<number>(0);
|
const shownCard = ref<number>(0);
|
||||||
|
|
||||||
|
const bleStore = useBluetoothStore();
|
||||||
|
|
||||||
const showArchive = () => {
|
const showArchive = () => {
|
||||||
prevCard = shownCard.value;
|
prevCard = shownCard.value;
|
||||||
shownCard.value = 3;
|
shownCard.value = 3;
|
||||||
@@ -72,18 +74,9 @@ const showSettings = () => {
|
|||||||
shownCard.value = 5;
|
shownCard.value = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
function disconnect() {
|
async function disconnect() {
|
||||||
ble.disconnect(
|
await bleStore.disconnect();
|
||||||
updater.device,
|
showScan();
|
||||||
() => {
|
|
||||||
console.log("Disconnected.");
|
|
||||||
showScan();
|
|
||||||
},
|
|
||||||
(failure) => {
|
|
||||||
console.error(failure);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user