2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2017 Konsulko Group
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 import QtWebSockets 1.0
26 property string statusString: "waiting..."
27 property string apiString: "Bluetooth-Manager"
28 property var verbs: []
29 property string payloadLength: "9999"
31 property string deviceAddress: ""
32 property bool connected: false
33 property bool av_connected: false
35 property int position: 0
36 property int duration: 0
38 property string artist: ""
39 property string title: ""
40 property string state: "stopped"
43 property string avrcp_uuid: "0000110e-0000-1000-8000-00805f9b34fb"
46 property string a2dp_uuid: "0000110a-0000-1000-8000-00805f9b34fb"
48 readonly property var msgid: {
55 onTextMessageReceived: {
56 var json = JSON.parse(message)
57 console.debug("Raw response: " + message)
58 var request = json[2].request
59 var response = json[2].response
60 console.debug("response: " + JSON.stringify(response))
65 root.statusString = request.status
68 if (request.info == "BT - Scan Result is Displayed") {
69 for (var i = 0; i < response.length; i++) {
70 var data = response[i]
71 if (data.Connected == "True" && data.UUIDs.indexOf(avrcp_uuid) >= 0) {
72 address = response[i].Address
73 console.debug("Connected Device: " + address)
78 //NOTE: This hack is here for when MediaPlayer is started
79 // with an existing connection.
80 if (data.AVPConnected == "True") {
81 root.av_connected = true
85 root.deviceAddress = address
87 root.connected = false
92 root.statusString = "Bad return value, binding probably not installed"
95 var payload = JSON.parse(JSON.stringify(json[2]))
96 var event = payload.event
98 if (event == "Bluetooth-Manager/connection") {
99 sendSocketMessage("discovery_result", 'None')
100 } else if (event == "Bluetooth-Manager/device_updated") {
101 var data = payload.data
102 var metadata = data.Metadata
104 if (root.deviceAddress != data.Address)
107 if (data.Connected == "False") {
108 console.debug("Device Disconnected")
109 sendSocketMessage("discovery_result", 'None')
112 root.av_connected = data.AVPConnected == "True"
114 if ('Position' in metadata) {
115 console.debug("Position " + metadata.Position)
116 root.position = metadata.Position
119 if ('Duration' in metadata) {
120 console.debug("Duration " + metadata.Duration)
121 root.duration = metadata.Duration
124 if ('Status' in metadata) {
125 console.debug("Status " + metadata.Status)
126 root.state = metadata.Status
129 if ('Artist' in metadata) {
130 console.debug("Artist " + metadata.Artist)
131 root.artist = metadata.Artist
134 if ('Title' in metadata) {
135 console.debug("Title " + metadata.Title)
136 root.title = metadata.Title
146 console.debug("onStatusChanged: Open")
147 sendSocketMessage("subscribe", { value : "device_updated" })
148 sendSocketMessage("subscribe", { value : "connection" })
149 sendSocketMessage("discovery_result", 'None')
151 case WebSocket.Error:
152 root.statusString = "WebSocket error: " + root.errorString
157 function sendSocketMessage(verb, parameter) {
158 var requestJson = [ msgid.call, payloadLength, apiString + '/'
160 console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
162 root.sendTextMessage(JSON.stringify(requestJson))
165 function sendMediaCommand(state) {
166 var parameters = { "Address": deviceAddress, "value": state }
167 sendSocketMessage("set_avrcp_controls", parameters)
170 function connect_profiles() {
171 sendSocketMessage("connect", { "value": root.deviceAddress, "uuid": a2dp_uuid })
172 sendSocketMessage("connect", { "value": root.deviceAddress, "uuid": avrcp_uuid })
175 function disconnect_profiles() {
176 sendSocketMessage("disconnect", { "value": root.deviceAddress, "uuid": a2dp_uuid })
177 sendSocketMessage("disconnect", { "value": root.deviceAddress, "uuid": avrcp_uuid })