Implement enable/disable bluetooth of device (#754)

Signed-off-by: Jussi Vatjus-Anttila <jussiva@gmail.com>
This commit is contained in:
Jussi Vatjus-Anttila
2024-01-29 13:06:57 +02:00
committed by GitHub
parent 63ad4f138e
commit e958a599cb
8 changed files with 163 additions and 2 deletions

View File

@@ -38,6 +38,7 @@ module.exports = function(options) {
.dependency(require('./plugins/account'))
.dependency(require('./plugins/ringer'))
.dependency(require('./plugins/wifi'))
.dependency(require('./plugins/bluetooth'))
.dependency(require('./plugins/sd'))
.dependency(require('./plugins/filesystem'))
.define(function(options, heartbeat, solo) {

View File

@@ -0,0 +1,53 @@
var syrup = require('stf-syrup')
var logger = require('../../../util/logger')
var wire = require('../../../wire')
var wireutil = require('../../../wire/util')
module.exports = syrup.serial()
.dependency(require('./service'))
.dependency(require('../support/router'))
.dependency(require('../support/push'))
.define(function(options, service, router, push) {
var log = logger.createLogger('device:plugins:bluetooth')
router.on(wire.BluetoothSetEnabledMessage, function(channel, message) {
var reply = wireutil.reply(options.serial)
log.info('Setting Bluetooth "%s"', message.enabled)
service.setBluetoothEnabled(message.enabled)
.timeout(30000)
.then(function() {
push.send([
channel
, reply.okay()
])
})
.catch(function(err) {
log.error('Setting Bluetooth enabled failed', err.stack)
push.send([
channel
, reply.fail(err.message)
])
})
})
router.on(wire.BluetoothGetStatusMessage, function(channel) {
var reply = wireutil.reply(options.serial)
log.info('Getting Bluetooth status')
service.getBluetoothStatus()
.timeout(30000)
.then(function(enabled) {
push.send([
channel
, reply.okay(enabled ? 'bluetooth_enabled' : 'bluetooth_disabled')
])
})
.catch(function(err) {
log.error('Getting Bluetooth status failed', err.stack)
push.send([
channel
, reply.fail(err.message)
])
})
})
})

View File

@@ -675,6 +675,35 @@ module.exports = syrup.serial()
})
}
plugin.setBluetoothEnabled = function(enabled) {
return runServiceCommand(
apk.wire.MessageType.SET_BLUETOOTH_ENABLED
, new apk.wire.SetBluetoothEnabledRequest(enabled)
)
.timeout(10000)
.then(function(data) {
var response = apk.wire.SetBluetoothEnabledResponse.decode(data)
if (!response.success) {
throw new Error('Unable to set Bluetooth')
}
})
}
plugin.getBluetoothStatus = function() {
return runServiceCommand(
apk.wire.MessageType.GET_BLUETOOTH_STATUS
, new apk.wire.GetBluetoothStatusRequest()
)
.timeout(10000)
.then(function(data) {
var response = apk.wire.GetBluetoothStatusResponse.decode(data)
if (response.success) {
return response.status
}
throw new Error('Unable to get Bluetooth status')
})
}
plugin.getSdStatus = function() {
return runServiceCommand(
apk.wire.MessageType.GET_SD_STATUS