Skip to content

Commit

Permalink
v1.2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mastashake08 committed Oct 23, 2023
1 parent 7022c07 commit 3930e3a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
25 changes: 15 additions & 10 deletions classes/BluetoothManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class BluetoothManager extends WebIOT {
#services = {}
#characteristic = {}
#currentValue = null
constructor (callback = ()=>({}), debug = false) {
constructor (callback = (event)=>{console.log(event)}, debug = false) {
super(debug)
navigator.bluetooth.getAvailability().then((available) => {
if (available) {
Expand All @@ -21,13 +21,10 @@ export class BluetoothManager extends WebIOT {

}

async requestDevice (options = {acceptAllDevices: true}) {
return await this.requestDevice(options)
}
async getDevices (options = {}) {
return await navigator.bluetooth.getDevices(options)
}
async requestDevice (options) {
async requestDevice (options = {acceptAllDevices: true}) {
try {
this.device = await navigator.bluetooth.requestDevice(options)
return this.device
Expand All @@ -36,8 +33,15 @@ export class BluetoothManager extends WebIOT {
}
}

async connectToServer () {
this.server = await this.device.gatt.connect()
async connectToServer (disconnect = (event)=>{console.log(event)}, serviceadded = (event)=>{console.log(event)}) {
try {
this.server = await this.device.gatt.connect()
this.device.gattserverdisconnected = disconnect
this.device.gatt.serviceadded = serviceadded
} catch {
console.log('Could not connect to server')
}

}

async getService (service) {
Expand All @@ -50,8 +54,9 @@ export class BluetoothManager extends WebIOT {
return this.services
}

async getCharacteristic (char) {
async getCharacteristic (char, valueChanged = (event)=>{console.log(event)}) {
this.characteristic = await this.selectedService.getCharacteristic(char)
this.characteristic.characteristicvaluechanged = valueChanged
return this.characteristic
}

Expand All @@ -68,7 +73,7 @@ export class BluetoothManager extends WebIOT {
await this.characteristic.writeValue(data)
}

startLEScan(options) {
this.bluetooth.requestLEScan(options)
async startLEScan(options = {acceptAllDevices: true}) {
return await this.bluetooth.requestLEScan(options)
}
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-iot",
"version": "1.2.5",
"name": "@mastashake08/web-iot",
"version": "1.2.7",
"description": "Connect to your IoT devices via usb, serial, NFC or Bluetooth",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 3930e3a

Please sign in to comment.