Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement set and get bluetooth of devices. #790

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/units/device/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
53 changes: 53 additions & 0 deletions lib/units/device/plugins/bluetooth.js
Original file line number Diff line number Diff line change
@@ -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)
])
})
})
})
29 changes: 29 additions & 0 deletions lib/units/device/plugins/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,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
Expand Down
20 changes: 20 additions & 0 deletions lib/units/websocket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,26 @@ module.exports = function(options) {
)
])
})
.on('bluetooth.set', function(channel, responseChannel, data) {
joinChannel(responseChannel)
push.send([
channel
, wireutil.transaction(
responseChannel
, new wire.BluetoothSetEnabledMessage(data.enabled)
)
])
})
.on('bluetooth.get', function(channel, responseChannel) {
joinChannel(responseChannel)
push.send([
channel
, wireutil.transaction(
responseChannel
, new wire.BluetoothGetStatusMessage()
)
])
})
.on('group.invite', function(channel, responseChannel, data) {
joinChannel(responseChannel)
push.send([
Expand Down
9 changes: 9 additions & 0 deletions lib/wire/wire.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ enum MessageType {
RingerGetMessage = 64;
WifiSetEnabledMessage = 57;
WifiGetStatusMessage = 58;
BluetoothGetStatusMessage = 95;
AccountAddMenuMessage = 59;
AccountAddMessage = 60;
AccountCheckMessage = 63;
Expand All @@ -78,6 +79,7 @@ enum MessageType {
FileSystemGetMessage = 82;
ConnectStartedMessage = 92;
ConnectStoppedMessage = 93;
BluetoothSetEnabledMessage = 94;
}

message ConnectStartedMessage {
Expand Down Expand Up @@ -531,6 +533,13 @@ message WifiSetEnabledMessage {
message WifiGetStatusMessage {
}

message BluetoothSetEnabledMessage {
required bool enabled = 1;
}

message BluetoothGetStatusMessage {
}

// Events, these must be kept in sync with STFService/wire.proto

message AirplaneModeEvent {
Expand Down
10 changes: 10 additions & 0 deletions res/app/components/stf/control/control-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ module.exports = function ControlServiceFactory(
return sendTwoWay('wifi.get')
}

this.setBluetoothEnabled = function(enabled) {
return sendTwoWay('bluetooth.set', {
enabled: enabled
})
}

this.getBluetoothStatus = function() {
return sendTwoWay('bluetooth.get')
}

window.cc = this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ module.exports = function DeviceSettingsCtrl($scope, $timeout) {
}
}

function getBluetoothStatus() {
if ($scope.control) {
$scope.control.getBluetoothStatus().then(function(result) {
$scope.$apply(function() {
$scope.bluetoothEnabled = (result.lastData === 'bluetooth_enabled')
})
})
}
}
getBluetoothStatus()

$scope.toggleBluetooth = function(enable) {
if ($scope.control) {
$scope.control.setBluetoothEnabled(enable)
$timeout(getBluetoothStatus, 2500)
}
}

$scope.$watch('ringerMode', function(newValue, oldValue) {
if (oldValue) {
if ($scope.control) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
span(translate) Device Settings
.widget-content.padded
.row
.col-md-6
.col-md-4
h6(translate) Manner Mode
.btn-group
label.btn.btn-sm.btn-primary-outline(ng-model='ringerMode', uib-btn-radio='"SILENT"', uib-tooltip='{{"Silent Mode" | translate}}')
Expand All @@ -14,14 +14,22 @@
label.btn.btn-sm.btn-primary-outline(ng-model='ringerMode', uib-btn-radio='"NORMAL"', uib-tooltip='{{"Normal Mode" | translate}}')
i.fa.fa-volume-up.fa-fw

.col-md-6
.col-md-4
h6(translate) WiFi
.btn-group
label.btn.btn-sm.btn-primary-outline(ng-model='wifiEnabled', ng-click='toggleWifi(false)', uib-btn-radio='false', uib-tooltip='{{"Disable WiFi" | translate}}')
i.fa.fa-power-off.fa-fw
label.btn.btn-sm.btn-primary-outline(ng-model='wifiEnabled', ng-click='toggleWifi(true)', uib-btn-radio='true', uib-tooltip='{{"Enable WiFi" | translate}}')
i.fa.fa-wifi.fa-fw

.col-md-4
h6(translate) Bluetooth
.btn-group
label.btn.btn-sm.btn-primary-outline(ng-model='bluetoothEnabled', ng-click='toggleBluetooth(false)', uib-btn-radio='false', uib-tooltip='{{"Disable WiFi" | translate}}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tooltips still refer to WiFi

i.fa.fa-power-off.fa-fw
label.btn.btn-sm.btn-primary-outline(ng-model='bluetoothEnabled', ng-click='toggleBluetooth(true)', uib-btn-radio='true', uib-tooltip='{{"Enable WiFi" | translate}}')
i.fa.fa-bluetooth-b.fa-fw

//.row
.col-md-12
h6(translate) Lock Rotation
Expand Down
18 changes: 18 additions & 0 deletions vendor/STFService/wire.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ enum MessageType {
GET_SD_STATUS = 25;
GET_VERSION = 8;
GET_WIFI_STATUS = 23;
GET_BLUETOOTH_STATUS = 29;
SET_CLIPBOARD = 9;
SET_KEYGUARD_STATE = 10;
SET_RINGER_MODE = 21;
SET_ROTATION = 12;
SET_WAKE_LOCK = 11;
SET_WIFI_ENABLED = 22;
SET_BLUETOOTH_ENABLED = 30;
SET_MASTER_MUTE = 28;
EVENT_AIRPLANE_MODE = 13;
EVENT_BATTERY = 14;
Expand Down Expand Up @@ -246,6 +248,22 @@ message GetWifiStatusResponse {
required bool status = 2;
}

message SetBluetoothEnabledRequest {
required bool enabled = 1;
}

message SetBluetoothEnabledResponse {
required bool success = 1;
}

message GetBluetoothStatusRequest {
}

message GetBluetoothStatusResponse {
required bool success = 1;
required bool status = 2;
}

message GetSdStatusRequest {
}

Expand Down