0.1.16 • Published 5 years ago

web-bluetooth-vuex v0.1.16

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

web-bluetooth-vuex

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.

Installation

$ yarn add web-bluetooth-vuex
# OR
$ npm install web-bluetooth-vuex

Configuration

//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}})

Use inside a .vue

Available Actions

addDevice

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)

removeDevice

Disconnects from device and then removes device and all its services & characteristics from the store.

let options = {device: deviceReferenceFromStore}
store.dispatch('webBluetooth/removeDevice',options)

connectDevice

Connects to a device that has been added to the store.

let options = {device: deviceReferenceFromStore}
store.dispatch('webBluetooth/connectDevice',options)

disconnectDevice

Connects to a device that has been added to the store.

let options = {device: deviceReferenceFromStore}
store.dispatch('webBluetooth/disconnectDevice',options)

discoverServices

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)

writeCharacteristic

Write an arraybuffer to the characteristic

let valueToWrite = Uint8Array.of(1)
let option2 = { characteristic: characteristicReferenceFromStore, value: valueToWrite }
store.dispatch('webBluetooth/writeCharacteristic',option1)

Available Getters

device(deviceId)

const device = store.getters['webBluetooth/device'](deviceId)

servicesForDevice(deviceId)

const services = store.getters['webBluetooth/servicesForDevice'](deviceId)

serviceForDevice(deviceId,uuid)

const service = store.getters['webBluetooth/serviceForDevice'](deviceId,BluetoothUUID.canonicalUUID(0x180F))

characteristicsForService(service)

const characteristics = store.getters['webBluetooth/characteristicsForService'](serviceFromStore)

characteristicForService(service,uuid)

const characteristic = store.getters['webBluetooth/characteristicForService'](serviceFromStore, BluetoothUUID.canonicalUUID(0x2A19))

Available state

devices

const devices = store.state.webBluetooth.devices

services

const services = store.state.webBluetooth.services

characteristics

const characteristics = store.state.webBluetooth.characteristics
0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago