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