mediaplayer: qml: fix issue with bluetooth icon
[apps/mediaplayer.git] / app / api / BluetoothManager.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2017 Konsulko Group
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 import QtQuick 2.6
19 import QtWebSockets 1.0
20
21 WebSocket {
22     id: root
23     active: true
24     url: bindingAddress
25
26     property string statusString: "waiting..."
27     property string apiString: "Bluetooth-Manager"
28     property var verbs: []
29     property string payloadLength: "9999"
30
31     property string deviceAddress: ""
32     property bool connected: false
33     property bool av_connected: false
34
35     property int position: 0
36     property int duration: 0
37
38     property string artist: ""
39     property string title: ""
40     property string state: "stopped"
41
42     // AVRCP Target UUID
43     property string avrcp_uuid: "0000110e-0000-1000-8000-00805f9b34fb"
44
45     // A2DP Source
46     property string a2dp_uuid:  "0000110a-0000-1000-8000-00805f9b34fb"
47
48     readonly property var msgid: {
49         "call": 2,
50         "retok": 3,
51         "reterr": 4,
52         "event": 5
53     }
54
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))
61         switch (json[0]) {
62             case msgid.call:
63                 break
64             case msgid.retok:
65                 root.statusString = request.status
66                 var address = ""
67
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)
74
75                             root.connected = true
76                             player.pause()
77
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
82                             }
83                         }
84                     }
85                     root.deviceAddress = address
86                     if (!address) {
87                         root.connected = false
88                     }
89                 }
90                 break
91             case msgid.reterr:
92                 root.statusString = "Bad return value, binding probably not installed"
93                 break
94             case msgid.event:
95                 var payload = JSON.parse(JSON.stringify(json[2]))
96                 var event = payload.event
97
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
103
104                     if (root.deviceAddress == "")
105                         root.deviceAddress = data.Address
106
107                     if (root.deviceAddress != data.Address)
108                         break
109
110                     if (data.Connected == "False") {
111                         console.debug("Device Disconnected")
112                         sendSocketMessage("discovery_result", 'None')
113                         break
114                     }
115                     root.connected = data.Connected == "True"
116                     root.av_connected = data.AVPConnected == "True"
117
118                     if ('Position' in metadata) {
119                         console.debug("Position " + metadata.Position)
120                         root.position = metadata.Position
121                     }
122
123                     if ('Duration' in metadata) {
124                         console.debug("Duration " + metadata.Duration)
125                         root.duration = metadata.Duration
126                     }
127
128                     if ('Status' in metadata) {
129                         console.debug("Status " + metadata.Status)
130                         root.state = metadata.Status
131                     }
132
133                     if ('Artist' in metadata) {
134                         console.debug("Artist " + metadata.Artist)
135                         root.artist = metadata.Artist
136                     }
137
138                     if ('Title' in metadata) {
139                         console.debug("Title " + metadata.Title)
140                         root.title = metadata.Title
141                     }
142                 }
143                 break
144         }
145     }
146
147     onStatusChanged: {
148         switch (status) {
149             case WebSocket.Open:
150             console.debug("onStatusChanged: Open")
151             sendSocketMessage("subscribe", { value : "device_updated" })
152             sendSocketMessage("subscribe", { value : "connection" })
153             sendSocketMessage("discovery_result", 'None')
154             break
155             case WebSocket.Error:
156             root.statusString = "WebSocket error: " + root.errorString
157             break
158         }
159     }
160
161     function sendSocketMessage(verb, parameter) {
162         var requestJson = [ msgid.call, payloadLength, apiString + '/'
163         + verb, parameter ]
164         console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
165         verbs.push(verb)
166         root.sendTextMessage(JSON.stringify(requestJson))
167     }
168
169     function sendMediaCommand(state) {
170         var parameters = { "Address": deviceAddress, "value": state }
171         sendSocketMessage("set_avrcp_controls", parameters)
172     }
173
174     function connect_profiles() {
175         sendSocketMessage("connect", { "value": root.deviceAddress, "uuid": a2dp_uuid })
176         sendSocketMessage("connect", { "value": root.deviceAddress, "uuid": avrcp_uuid })
177     }
178
179     function disconnect_profiles() {
180         sendSocketMessage("disconnect", { "value": root.deviceAddress, "uuid": a2dp_uuid })
181         sendSocketMessage("disconnect", { "value": root.deviceAddress, "uuid": avrcp_uuid })
182     }
183 }