A vuex module to interface with BLE devices through the Web Bluetooth API available in Chrome. For Linux and Macos Chrome support was available in 54, for windows 10 in version 70.
$ yarn add web-bluetooth-vuex
# OR
$ npm install web-bluetooth-vuex
//Set up Vue & Vuex
import Vue from 'vue'
import Vuex from 'vuex'
import WebBluetoothModule from 'web-bluetooth-vuex'
Vue.use(Vuex)
//Create your vuex store
let store = new Vuex.Store({state: {}, modules: {webBluetooth: WebBluetoothModule}})
Add devices in the proxmity to the store. Accepts a couple of different combinations. The snipplet below illustrates the combinations. The services array can take the form of names of established services, 16bit uuid and 128bit uuids.
// Scan for any devices around you, services determines which possible services
// you can acccess when connected
const option1 = { anyDevices: true, services:['device_information'] }
// Scan for a device with specific name around you, services determines which
// possible services you can acccess when connected
const option2 = { name: 'Demo Device', services['device_information'] }
// Scan for a devices with a name prefix round you, services determines which
// possible services you can acccess when connected
const option3 = { namePrefix: 'Demo Device', services['device_information'] }
// Scan for a devices with a specific set of services advertised
const option4 = { services['device_information'] }
store.dispatch('webBluetooth/addDevice',options)
Disconnects from device and then removes device and all its services & characteristics from the store.
let options = {device: deviceReferenceFromStore}
store.dispatch('webBluetooth/removeDevice',options)
Connects to a device that has been added to the store.
let options = {device: deviceReferenceFromStore}
store.dispatch('webBluetooth/connectDevice',options)
Connects to a device that has been added to the store.
let options = {device: deviceReferenceFromStore}
store.dispatch('webBluetooth/disconnectDevice',options)
For a connected devices performs a service and characteristics discovery. characteristics will also be configuerd according to their permissions. Read, Write, Notify, Indicate permissions will be setup with apppripate callbacks and the store will update with value changes.
// Discover all services specifed in addDevice
let option1 = { device: deviceReferenceFromStore }
// Discover a specific service specifed in addDevice
// in this incase the uuid of the Battery Service
let option2 = { device: deviceReferenceFromStore, uuid: BluetoothUUID.canonicalUUID(0x180F) }
store.dispatch('webBluetooth/discoverServices',option1)
Write an arraybuffer to the characteristic
let valueToWrite = Uint8Array.of(1)
let option2 = { characteristic: characteristicReferenceFromStore, value: valueToWrite }
store.dispatch('webBluetooth/writeCharacteristic',option1)
const device = store.getters['webBluetooth/device'](deviceId)
const services = store.getters['webBluetooth/servicesForDevice'](deviceId)
const service = store.getters['webBluetooth/serviceForDevice'](deviceId,BluetoothUUID.canonicalUUID(0x180F))
const characteristics = store.getters['webBluetooth/characteristicsForService'](serviceFromStore)
const characteristic = store.getters['webBluetooth/characteristicForService'](serviceFromStore, BluetoothUUID.canonicalUUID(0x2A19))
const devices = store.state.webBluetooth.devices
const services = store.state.webBluetooth.services
const characteristics = store.state.webBluetooth.characteristics