1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_bluetooth_bluetoothadapter_h__
8 #define mozilla_dom_bluetooth_bluetoothadapter_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/DOMEventTargetHelper.h"
12 #include "mozilla/dom/BluetoothAdapter2Binding.h"
13 #include "mozilla/dom/BluetoothDeviceEvent.h"
14 #include "mozilla/dom/Promise.h"
15 #include "BluetoothCommon.h"
22 struct MediaPlayStatus
;
26 BEGIN_BLUETOOTH_NAMESPACE
28 class BluetoothDevice
;
29 class BluetoothDiscoveryHandle
;
30 class BluetoothSignal
;
31 class BluetoothNamedValue
;
32 class BluetoothPairingListener
;
35 class BluetoothAdapter
: public DOMEventTargetHelper
36 , public BluetoothSignalObserver
39 NS_DECL_ISUPPORTS_INHERITED
41 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BluetoothAdapter
,
44 static already_AddRefed
<BluetoothAdapter
>
45 Create(nsPIDOMWindow
* aOwner
, const BluetoothValue
& aValue
);
47 void Notify(const BluetoothSignal
& aParam
);
49 void SetPropertyByValue(const BluetoothNamedValue
& aValue
);
51 virtual void DisconnectFromOwner() MOZ_OVERRIDE
;
53 BluetoothAdapterState
State() const
58 void GetAddress(nsString
& aAddress
) const
64 GetName(nsString
& aName
) const
81 BluetoothPairingListener
* PairingReqs() const
87 * Update this adapter's discovery handle in use (mDiscoveryHandleInUse).
89 * |mDiscoveryHandleInUse| is set to the latest discovery handle when adapter
90 * just starts discovery, and is reset to nullptr when discovery is stopped
93 * @param aDiscoveryHandle [in] the discovery handle to set.
95 void SetDiscoveryHandleInUse(BluetoothDiscoveryHandle
* aDiscoveryHandle
);
97 already_AddRefed
<Promise
> SetName(const nsAString
& aName
, ErrorResult
& aRv
);
98 already_AddRefed
<Promise
>
99 SetDiscoverable(bool aDiscoverable
, ErrorResult
& aRv
);
100 already_AddRefed
<Promise
> StartDiscovery(ErrorResult
& aRv
);
101 already_AddRefed
<Promise
> StopDiscovery(ErrorResult
& aRv
);
103 already_AddRefed
<Promise
>
104 Pair(const nsAString
& aDeviceAddress
, ErrorResult
& aRv
);
105 already_AddRefed
<Promise
>
106 Unpair(const nsAString
& aDeviceAddress
, ErrorResult
& aRv
);
109 * Get a list of paired bluetooth devices.
111 * @param aDevices [out] Devices array to return
113 void GetPairedDevices(nsTArray
<nsRefPtr
<BluetoothDevice
> >& aDevices
);
115 already_AddRefed
<Promise
> EnableDisable(bool aEnable
, ErrorResult
& aRv
);
116 already_AddRefed
<Promise
> Enable(ErrorResult
& aRv
);
117 already_AddRefed
<Promise
> Disable(ErrorResult
& aRv
);
119 already_AddRefed
<DOMRequest
>
120 Connect(BluetoothDevice
& aDevice
,
121 const Optional
<short unsigned int>& aServiceUuid
, ErrorResult
& aRv
);
122 already_AddRefed
<DOMRequest
>
123 Disconnect(BluetoothDevice
& aDevice
,
124 const Optional
<short unsigned int>& aServiceUuid
,
126 already_AddRefed
<DOMRequest
>
127 GetConnectedDevices(uint16_t aServiceUuid
, ErrorResult
& aRv
);
129 already_AddRefed
<DOMRequest
>
130 SendFile(const nsAString
& aDeviceAddress
, nsIDOMBlob
* aBlob
,
132 already_AddRefed
<DOMRequest
>
133 StopSendingFile(const nsAString
& aDeviceAddress
, ErrorResult
& aRv
);
134 already_AddRefed
<DOMRequest
>
135 ConfirmReceivingFile(const nsAString
& aDeviceAddress
, bool aConfirmation
,
138 already_AddRefed
<DOMRequest
> ConnectSco(ErrorResult
& aRv
);
139 already_AddRefed
<DOMRequest
> DisconnectSco(ErrorResult
& aRv
);
140 already_AddRefed
<DOMRequest
> IsScoConnected(ErrorResult
& aRv
);
142 already_AddRefed
<DOMRequest
> AnswerWaitingCall(ErrorResult
& aRv
);
143 already_AddRefed
<DOMRequest
> IgnoreWaitingCall(ErrorResult
& aRv
);
144 already_AddRefed
<DOMRequest
> ToggleCalls(ErrorResult
& aRv
);
146 already_AddRefed
<DOMRequest
>
147 SendMediaMetaData(const MediaMetaData
& aMediaMetaData
, ErrorResult
& aRv
);
148 already_AddRefed
<DOMRequest
>
149 SendMediaPlayStatus(const MediaPlayStatus
& aMediaPlayStatus
, ErrorResult
& aRv
);
151 IMPL_EVENT_HANDLER(a2dpstatuschanged
);
152 IMPL_EVENT_HANDLER(hfpstatuschanged
);
153 IMPL_EVENT_HANDLER(requestmediaplaystatus
);
154 IMPL_EVENT_HANDLER(scostatuschanged
);
155 IMPL_EVENT_HANDLER(attributechanged
);
156 IMPL_EVENT_HANDLER(devicepaired
);
157 IMPL_EVENT_HANDLER(deviceunpaired
);
159 nsPIDOMWindow
* GetParentObject() const
165 WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
168 BluetoothAdapter(nsPIDOMWindow
* aOwner
, const BluetoothValue
& aValue
);
171 already_AddRefed
<Promise
>
172 PairUnpair(bool aPair
, const nsAString
& aDeviceAddress
, ErrorResult
& aRv
);
174 bool IsAdapterAttributeChanged(BluetoothAdapterAttribute aType
,
175 const BluetoothValue
& aValue
);
176 void HandleAdapterStateChanged();
177 void HandlePropertyChanged(const BluetoothValue
& aValue
);
178 void DispatchAttributeEvent(const nsTArray
<nsString
>& aTypes
);
179 BluetoothAdapterAttribute
180 ConvertStringToAdapterAttribute(const nsAString
& aString
);
182 void GetPairedDeviceProperties(const nsTArray
<nsString
>& aDeviceAddresses
);
184 void HandleDeviceFound(const BluetoothValue
& aValue
);
185 void HandlePairingRequest(const BluetoothValue
& aValue
);
188 * Handle DEVICE_PAIRED_ID bluetooth signal.
190 * @param aValue [in] Properties array of the paired device.
191 * The array should contain two properties:
192 * - nsString 'Address'
195 void HandleDevicePaired(const BluetoothValue
& aValue
);
198 * Handle DEVICE_UNPAIRED_ID bluetooth signal.
200 * @param aValue [in] Properties array of the unpaired device.
201 * The array should contain two properties:
202 * - nsString 'Address'
205 void HandleDeviceUnpaired(const BluetoothValue
& aValue
);
208 * Fire BluetoothDeviceEvent to trigger
209 * ondeviceparied/ondeviceunpaired event handler.
211 * @param aType [in] Event type to fire
212 * @param aInit [in] Event initialization value
214 void DispatchDeviceEvent(const nsAString
& aType
,
215 const BluetoothDeviceEventInit
& aInit
);
218 * mDevices holds references of all created device objects.
219 * It is an empty array when the adapter state is disabled.
221 * Devices will be appended when
222 * 1) Enabling BT: Paired devices reported by stack.
223 * 2) Discovering: Discovered devices during discovery operation.
224 * A device won't be appended if a device object with the same
225 * address already exists.
227 * Devices will be removed when
228 * 1) Starting discovery: All unpaired devices will be removed before this
229 * adapter starts a new discovery.
230 * 2) Disabling BT: All devices will be removed.
232 nsTArray
<nsRefPtr
<BluetoothDevice
> > mDevices
;
233 nsRefPtr
<BluetoothDiscoveryHandle
> mDiscoveryHandleInUse
;
234 nsRefPtr
<BluetoothPairingListener
> mPairingReqs
;
235 BluetoothAdapterState mState
;
242 END_BLUETOOTH_NAMESPACE