mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:13:31 +02:00
Implement enable/disable bluetooth of device (#754)
Signed-off-by: Jussi Vatjus-Anttila <jussiva@gmail.com>
This commit is contained in:
committed by
GitHub
parent
63ad4f138e
commit
e958a599cb
53
lib/units/device/plugins/bluetooth.js
Normal file
53
lib/units/device/plugins/bluetooth.js
Normal 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)
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user