aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/android/mobly/snippet/bundled/BluetoothLeAdvertiserSnippet.java
blob: 4b2c5b5cd82146b45333d127f02de82c120b1fe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (C) 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.mobly.snippet.bundled;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelUuid;
import com.google.android.mobly.snippet.Snippet;
import com.google.android.mobly.snippet.bundled.utils.JsonDeserializer;
import com.google.android.mobly.snippet.bundled.utils.JsonSerializer;
import com.google.android.mobly.snippet.bundled.utils.RpcEnum;
import com.google.android.mobly.snippet.event.EventCache;
import com.google.android.mobly.snippet.event.SnippetEvent;
import com.google.android.mobly.snippet.rpc.AsyncRpc;
import com.google.android.mobly.snippet.rpc.Rpc;
import com.google.android.mobly.snippet.rpc.RpcMinSdk;
import com.google.android.mobly.snippet.rpc.RpcOptional;
import com.google.android.mobly.snippet.util.Log;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

/** Snippet class exposing Android APIs in WifiManager. */
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
public class BluetoothLeAdvertiserSnippet implements Snippet {
    private static class BluetoothLeAdvertiserSnippetException extends Exception {
        private static final long serialVersionUID = 1;

        public BluetoothLeAdvertiserSnippetException(String msg) {
            super(msg);
        }
    }

    private final BluetoothLeAdvertiser mAdvertiser;
    private static final EventCache sEventCache = EventCache.getInstance();

    private final HashMap<String, AdvertiseCallback> mAdvertiseCallbacks = new HashMap<>();

    public BluetoothLeAdvertiserSnippet() {
        mAdvertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    }

    /**
     * Start Bluetooth LE advertising.
     *
     * <p>This can be called multiple times, and each call is associated with a {@link
     * AdvertiseCallback} object, which is used to stop the advertising.
     *
     * @param callbackId
     * @param advertiseSettings A JSONObject representing a {@link AdvertiseSettings object}. E.g.
     *     <pre>
     *          {
     *            "AdvertiseMode": "ADVERTISE_MODE_BALANCED",
     *            "Timeout": (int, milliseconds),
     *            "Connectable": (bool),
     *            "TxPowerLevel": "ADVERTISE_TX_POWER_LOW"
     *          }
     *     </pre>
     *
     * @param advertiseData A JSONObject representing a {@link AdvertiseData} object will be
     *     broadcast if the operation succeeds. E.g.
     *     <pre>
     *          {
     *            "IncludeDeviceName": (bool),
     *            # JSON list, each element representing a set of service data, which is composed of
     *            # a UUID, and an optional string.
     *            "ServiceData": [
     *                      {
     *                        "UUID": (A string representation of {@link ParcelUuid}),
     *                        "Data": (Optional, The string representation of what you want to
     *                                 advertise, base64 encoded)
     *                        # If you want to add a UUID without data, simply omit the "Data"
     *                        # field.
     *                      }
     *                ]
     *          }
     *     </pre>
     *
     * @param scanResponse A JSONObject representing a {@link AdvertiseData} object which will
     *     response the data to the scanning device. E.g.
     *     <pre>
     *          {
     *            "IncludeDeviceName": (bool),
     *            # JSON list, each element representing a set of service data, which is composed of
     *            # a UUID, and an optional string.
     *            "ServiceData": [
     *                      {
     *                        "UUID": (A string representation of {@link ParcelUuid}),
     *                        "Data": (Optional, The string representation of what you want to
     *                                 advertise, base64 encoded)
     *                        # If you want to add a UUID without data, simply omit the "Data"
     *                        # field.
     *                      }
     *                ]
     *          }
     *     </pre>
     *
     * @throws BluetoothLeAdvertiserSnippetException
     * @throws JSONException
     */
    @RpcMinSdk(Build.VERSION_CODES.LOLLIPOP_MR1)
    @AsyncRpc(description = "Start BLE advertising.")
    public void bleStartAdvertising(
            String callbackId,
            JSONObject advertiseSettings,
            JSONObject advertiseData,
            @RpcOptional JSONObject scanResponse)
            throws BluetoothLeAdvertiserSnippetException, JSONException {
        if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
            throw new BluetoothLeAdvertiserSnippetException(
                    "Bluetooth is disabled, cannot start BLE advertising.");
        }
        AdvertiseSettings settings = JsonDeserializer.jsonToBleAdvertiseSettings(advertiseSettings);
        AdvertiseData data = JsonDeserializer.jsonToBleAdvertiseData(advertiseData);
        AdvertiseCallback advertiseCallback = new DefaultAdvertiseCallback(callbackId);
        if (scanResponse == null) {
            mAdvertiser.startAdvertising(settings, data, advertiseCallback);
        } else {
            AdvertiseData response = JsonDeserializer.jsonToBleAdvertiseData(scanResponse);
            mAdvertiser.startAdvertising(settings, data, response, advertiseCallback);
        }
        mAdvertiseCallbacks.put(callbackId, advertiseCallback);
    }

    /**
     * Stop a BLE advertising.
     *
     * @param callbackId The callbackId corresponding to the {@link
     *     BluetoothLeAdvertiserSnippet#bleStartAdvertising} call that started the advertising.
     * @throws BluetoothLeScannerSnippet.BluetoothLeScanSnippetException
     */
    @RpcMinSdk(Build.VERSION_CODES.LOLLIPOP_MR1)
    @Rpc(description = "Stop BLE advertising.")
    public void bleStopAdvertising(String callbackId) throws BluetoothLeAdvertiserSnippetException {
        AdvertiseCallback callback = mAdvertiseCallbacks.remove(callbackId);
        if (callback == null) {
            throw new BluetoothLeAdvertiserSnippetException(
                    "No advertising session found for ID " + callbackId);
        }
        mAdvertiser.stopAdvertising(callback);
    }

    private static class DefaultAdvertiseCallback extends AdvertiseCallback {
        private final String mCallbackId;
        public static RpcEnum ADVERTISE_FAILURE_ERROR_CODE =
                new RpcEnum.Builder()
                        .add("ADVERTISE_FAILED_ALREADY_STARTED", ADVERTISE_FAILED_ALREADY_STARTED)
                        .add("ADVERTISE_FAILED_DATA_TOO_LARGE", ADVERTISE_FAILED_DATA_TOO_LARGE)
                        .add(
                                "ADVERTISE_FAILED_FEATURE_UNSUPPORTED",
                                ADVERTISE_FAILED_FEATURE_UNSUPPORTED)
                        .add("ADVERTISE_FAILED_INTERNAL_ERROR", ADVERTISE_FAILED_INTERNAL_ERROR)
                        .add(
                                "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS",
                                ADVERTISE_FAILED_TOO_MANY_ADVERTISERS)
                        .build();

        public DefaultAdvertiseCallback(String callbackId) {
            mCallbackId = callbackId;
        }

        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            Log.e("Bluetooth LE advertising started with settings: " + settingsInEffect.toString());
            SnippetEvent event = new SnippetEvent(mCallbackId, "onStartSuccess");
            Bundle advertiseSettings =
                    JsonSerializer.serializeBleAdvertisingSettings(settingsInEffect);
            event.getData().putBundle("SettingsInEffect", advertiseSettings);
            sEventCache.postEvent(event);
        }

        @Override
        public void onStartFailure(int errorCode) {
            Log.e("Bluetooth LE advertising failed to start with error code: " + errorCode);
            SnippetEvent event = new SnippetEvent(mCallbackId, "onStartFailure");
            final String errorCodeString = ADVERTISE_FAILURE_ERROR_CODE.getString(errorCode);
            event.getData().putString("ErrorCode", errorCodeString);
            sEventCache.postEvent(event);
        }
    }

    @Override
    public void shutdown() {
        for (AdvertiseCallback callback : mAdvertiseCallbacks.values()) {
            mAdvertiser.stopAdvertising(callback);
        }
        mAdvertiseCallbacks.clear();
    }
}