summaryrefslogtreecommitdiff
path: root/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/BleClientTestBaseActivity.java
blob: bfcd85d6dc577c74f53b6626e3135afcaf67e55a (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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.android.cts.verifier.bluetooth;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;

import com.android.cts.verifier.PassFailButtons;
import com.android.cts.verifier.R;

import java.util.ArrayList;
import java.util.List;
import android.util.Log;

public class BleClientTestBaseActivity extends PassFailButtons.Activity {
    public static final String TAG = "BleClientTestBase";

    private static final boolean STEP_EXECUTION = false;

    private static final int PASS_FLAG_CONNECT = 0x1;
    private static final int PASS_FLAG_DISCOVER = 0x2;
    private static final int PASS_FLAG_READ_CHARACTERISTIC = 0x4;
    private static final int PASS_FLAG_WRITE_CHARACTERISTIC = 0x8;
    private static final int PASS_FLAG_RELIABLE_WRITE = 0x10;
    private static final int PASS_FLAG_NOTIFY_CHARACTERISTIC = 0x20;
    private static final int PASS_FLAG_READ_DESCRIPTOR = 0x40;
    private static final int PASS_FLAG_WRITE_DESCRIPTOR = 0x80;
    private static final int PASS_FLAG_READ_RSSI = 0x100;
    private static final int PASS_FLAG_DISCONNECT = 0x200;
    private static final int PASS_FLAG_READ_CHARACTERISTIC_NO_PERMISSION = 0x400;
    private static final int PASS_FLAG_WRITE_CHARACTERISTIC_NO_PERMISSION = 0x800;
    private static final int PASS_FLAG_READ_DESCRIPTOR_NO_PERMISSION = 0x1000;
    private static final int PASS_FLAG_WRITE_DESCRIPTOR_NO_PERMISSION = 0x2000;
    private static final int PASS_FLAG_MTU_CHANGE_23BYTES = 0x4000;
    private static final int PASS_FLAG_INDICATE_CHARACTERISTIC = 0x8000;
    private static final int PASS_FLAG_MTU_CHANGE_512BYTES = 0x10000;
    private static final int PASS_FLAG_RELIABLE_WRITE_BAD_RESP = 0x20000;
    private static final int PASS_FLAG_ON_SERVICE_CHANGED = 0x40000;
    private static final int PASS_FLAG_ALL = 0x7FFFF;

    private final int BLE_CLIENT_CONNECT = 0;
    private final int BLE_BLE_DISCOVER_SERVICE = 1;
    private final int BLE_READ_CHARACTERISTIC = 2;
    private final int BLE_WRITE_CHARACTERISTIC = 3;
    private final int BLE_REQUEST_MTU_23BYTES = 4;
    private final int BLE_REQUEST_MTU_512BYTES = 5;
    private final int BLE_READ_CHARACTERISTIC_NO_PERMISSION = 6;
    private final int BLE_WRITE_CHARACTERISTIC_NO_PERMISSION = 7;
    private final int BLE_RELIABLE_WRITE = 8;
    private final int BLE_RELIABLE_WRITE_BAD_RESP = 9;
    private final int BLE_NOTIFY_CHARACTERISTIC = 9;    //10;
    private final int BLE_INDICATE_CHARACTERISTIC = 10;  //11;
    private final int BLE_READ_DESCRIPTOR = 11; //12;
    private final int BLE_WRITE_DESCRIPTOR = 12;    //13;
    private final int BLE_READ_DESCRIPTOR_NO_PERMISSION = 13;   //14;
    private final int BLE_WRITE_DESCRIPTOR_NO_PERMISSION = 14;  //15;
    private final int BLE_READ_RSSI = 15;   //16;
    private final int BLE_ON_SERVICE_CHANGED = 15; //16; //17;
    private final int BLE_CLIENT_DISCONNECT = 16;  //17; //18;

    private TestAdapter mTestAdapter;
    private long mPassed;
    private Dialog mDialog;
    private Handler mHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ble_server_start);
        setPassFailButtonClickListeners();
        getPassButton().setEnabled(false);

        mTestAdapter = new TestAdapter(this, setupTestList());
        ListView listView = (ListView) findViewById(R.id.ble_server_tests);
        listView.setAdapter(mTestAdapter);

        mPassed = 0;
        mHandler = new Handler();
    }

    @Override
    public void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter();
        filter.addAction(BleClientService.BLE_BLUETOOTH_CONNECTED);
        filter.addAction(BleClientService.BLE_BLUETOOTH_DISCONNECTED);
        filter.addAction(BleClientService.BLE_SERVICES_DISCOVERED);
        filter.addAction(BleClientService.BLE_MTU_CHANGED_23BYTES);
        filter.addAction(BleClientService.BLE_MTU_CHANGED_512BYTES);
        filter.addAction(BleClientService.BLE_CHARACTERISTIC_READ);
        filter.addAction(BleClientService.BLE_CHARACTERISTIC_WRITE);
        filter.addAction(BleClientService.BLE_CHARACTERISTIC_CHANGED);
        filter.addAction(BleClientService.BLE_DESCRIPTOR_READ);
        filter.addAction(BleClientService.BLE_DESCRIPTOR_WRITE);
        filter.addAction(BleClientService.BLE_RELIABLE_WRITE_COMPLETED);
        filter.addAction(BleClientService.BLE_RELIABLE_WRITE_BAD_RESP_COMPLETED);
        filter.addAction(BleClientService.BLE_READ_REMOTE_RSSI);
        filter.addAction(BleClientService.BLE_ON_SERVICE_CHANGED);
        filter.addAction(BleClientService.BLE_CHARACTERISTIC_READ_NOPERMISSION);
        filter.addAction(BleClientService.BLE_CHARACTERISTIC_WRITE_NOPERMISSION);
        filter.addAction(BleClientService.BLE_DESCRIPTOR_READ_NOPERMISSION);
        filter.addAction(BleClientService.BLE_DESCRIPTOR_WRITE_NOPERMISSION);
        filter.addAction(BleClientService.BLE_CHARACTERISTIC_INDICATED);
        filter.addAction(BleClientService.BLE_BLUETOOTH_DISABLED);
        filter.addAction(BleClientService.BLE_BLUETOOTH_MISMATCH_SECURE);
        filter.addAction(BleClientService.BLE_BLUETOOTH_MISMATCH_INSECURE);
        filter.addAction(BleClientService.BLE_CLIENT_ERROR);

        registerReceiver(mBroadcast, filter);
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(mBroadcast);
        closeDialog();
    }

    private synchronized void closeDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    private synchronized void showProgressDialog() {
        closeDialog();

        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle(R.string.ble_test_running);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage(getString(R.string.ble_test_running_message));
        dialog.setCanceledOnTouchOutside(false);
        mDialog = dialog;
        mDialog.show();
    }

    private List<Integer> setupTestList() {
        ArrayList<Integer> testList = new ArrayList<Integer>();
        testList.add(R.string.ble_client_connect_name);
        testList.add(R.string.ble_discover_service_name);
        testList.add(R.string.ble_read_characteristic_name);
        testList.add(R.string.ble_write_characteristic_name);
        testList.add(R.string.ble_mtu_23_name);
        testList.add(R.string.ble_mtu_512_name);
        testList.add(R.string.ble_read_characteristic_nopermission_name);
        testList.add(R.string.ble_write_characteristic_nopermission_name);
        testList.add(R.string.ble_reliable_write_name);
//        testList.add(R.string.ble_reliable_write_bad_resp_name);
        testList.add(R.string.ble_notify_characteristic_name);
        testList.add(R.string.ble_indicate_characteristic_name);
        testList.add(R.string.ble_read_descriptor_name);
        testList.add(R.string.ble_write_descriptor_name);
        testList.add(R.string.ble_read_descriptor_nopermission_name);
        testList.add(R.string.ble_write_descriptor_nopermission_name);
// TODO: too flaky b/34951749
//        testList.add(R.string.ble_read_rssi_name);
        testList.add(R.string.ble_on_service_changed);
        testList.add(R.string.ble_client_disconnect_name);

        return testList;
    }

    private void showErrorDialog(int titleId, int messageId, boolean finish) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(titleId)
                .setMessage(messageId);
        if (finish) {
            builder.setOnCancelListener(new Dialog.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    finish();
                }
            });
        }
        builder.create().show();
    }

    public boolean shouldRebootBluetoothAfterTest() {
        return false;
    }

    private BroadcastReceiver mBroadcast = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean showProgressDialog = false;
            closeDialog();

            String action = intent.getAction();
            String newAction = null;
            String actionName = null;
            long previousPassed = mPassed;
            final Intent startIntent = new Intent(BleClientTestBaseActivity.this, BleClientService.class);
            if (action != null) {
                Log.d(TAG, "Processing " + action);
            }
            switch (action) {
            case BleClientService.BLE_BLUETOOTH_DISABLED:
                showErrorDialog(R.string.ble_bluetooth_disable_title, R.string.ble_bluetooth_disable_message, true);
                break;
            case BleClientService.BLE_BLUETOOTH_CONNECTED:
                actionName = getString(R.string.ble_client_connect_name);
                mTestAdapter.setTestPass(BLE_CLIENT_CONNECT);
                mPassed |= PASS_FLAG_CONNECT;
                // execute service discovery test
                newAction = BleClientService.BLE_CLIENT_ACTION_BLE_DISCOVER_SERVICE;
                break;
            case BleClientService.BLE_SERVICES_DISCOVERED:
                actionName = getString(R.string.ble_discover_service_name);
                mTestAdapter.setTestPass(BLE_BLE_DISCOVER_SERVICE);
                mPassed |= PASS_FLAG_DISCOVER;
                // execute MTU requesting test (23bytes)
                newAction = BleClientService.BLE_CLIENT_ACTION_READ_CHARACTERISTIC;
                break;
            case BleClientService.BLE_MTU_CHANGED_23BYTES:
                actionName = getString(R.string.ble_mtu_23_name);
                mTestAdapter.setTestPass(BLE_REQUEST_MTU_23BYTES);
                mPassed |= PASS_FLAG_MTU_CHANGE_23BYTES;
                // execute MTU requesting test (512bytes)
                newAction = BleClientService.BLE_CLIENT_ACTION_REQUEST_MTU_512;
                showProgressDialog = true;
                break;
            case BleClientService.BLE_MTU_CHANGED_512BYTES:
                actionName = getString(R.string.ble_mtu_512_name);
                mTestAdapter.setTestPass(BLE_REQUEST_MTU_512BYTES);
                mPassed |= PASS_FLAG_MTU_CHANGE_512BYTES;
                // execute characteristic reading test
                newAction = BleClientService.BLE_CLIENT_ACTION_READ_CHARACTERISTIC_NO_PERMISSION;
                break;
            case BleClientService.BLE_CHARACTERISTIC_READ:
                actionName = getString(R.string.ble_read_characteristic_name);
                mTestAdapter.setTestPass(BLE_READ_CHARACTERISTIC);
                mPassed |= PASS_FLAG_READ_CHARACTERISTIC;
                // execute characteristic writing test
                newAction = BleClientService.BLE_CLIENT_ACTION_WRITE_CHARACTERISTIC;
                break;
            case BleClientService.BLE_CHARACTERISTIC_WRITE:
                actionName = getString(R.string.ble_write_characteristic_name);
                mTestAdapter.setTestPass(BLE_WRITE_CHARACTERISTIC);
                mPassed |= PASS_FLAG_WRITE_CHARACTERISTIC;
                newAction = BleClientService.BLE_CLIENT_ACTION_REQUEST_MTU_23;
                showProgressDialog = true;
                break;
            case BleClientService.BLE_CHARACTERISTIC_READ_NOPERMISSION:
                actionName = getString(R.string.ble_read_characteristic_nopermission_name);
                mTestAdapter.setTestPass(BLE_READ_CHARACTERISTIC_NO_PERMISSION);
                mPassed |= PASS_FLAG_READ_CHARACTERISTIC_NO_PERMISSION;
                // execute unpermitted characteristic writing test
                newAction = BleClientService.BLE_CLIENT_ACTION_WRITE_CHARACTERISTIC_NO_PERMISSION;
                break;
            case BleClientService.BLE_CHARACTERISTIC_WRITE_NOPERMISSION:
                actionName = getString(R.string.ble_write_characteristic_nopermission_name);
                mTestAdapter.setTestPass(BLE_WRITE_CHARACTERISTIC_NO_PERMISSION);
                mPassed |= PASS_FLAG_WRITE_CHARACTERISTIC_NO_PERMISSION;
                // execute reliable write test
                newAction = BleClientService.BLE_CLIENT_ACTION_RELIABLE_WRITE;
                showProgressDialog = true;
                break;
            case BleClientService.BLE_RELIABLE_WRITE_COMPLETED:
                actionName = getString(R.string.ble_reliable_write_name);
                mTestAdapter.setTestPass(BLE_RELIABLE_WRITE);
                mPassed |= PASS_FLAG_RELIABLE_WRITE;
//                newAction = BleClientService.BLE_CLIENT_ACTION_RELIABLE_WRITE_BAD_RESP;

                // skip Reliable write (bad response) test
                mPassed |= PASS_FLAG_RELIABLE_WRITE_BAD_RESP;
                Log.d(TAG, "Skip PASS_FLAG_RELIABLE_WRITE_BAD_RESP.");
                newAction = BleClientService.BLE_CLIENT_ACTION_NOTIFY_CHARACTERISTIC;
                showProgressDialog = true;
                break;
            case BleClientService.BLE_RELIABLE_WRITE_BAD_RESP_COMPLETED: {
                actionName = getString(R.string.ble_reliable_write_bad_resp_name);
                if(!intent.hasExtra(BleClientService.EXTRA_ERROR_MESSAGE)) {
                    mPassed |= PASS_FLAG_RELIABLE_WRITE_BAD_RESP;
                    mTestAdapter.setTestPass(BLE_RELIABLE_WRITE_BAD_RESP);
                }
                // execute notification test
                newAction = BleClientService.BLE_CLIENT_ACTION_NOTIFY_CHARACTERISTIC;
                showProgressDialog = true;
            }
                break;
            case BleClientService.BLE_CHARACTERISTIC_CHANGED:
                actionName = getString(R.string.ble_notify_characteristic_name);
                mTestAdapter.setTestPass(BLE_NOTIFY_CHARACTERISTIC);
                mPassed |= PASS_FLAG_NOTIFY_CHARACTERISTIC;
                // execute indication test
                newAction = BleClientService.BLE_CLIENT_ACTION_INDICATE_CHARACTERISTIC;
                showProgressDialog = true;
                break;
            case BleClientService.BLE_CHARACTERISTIC_INDICATED:
                actionName = getString(R.string.ble_indicate_characteristic_name);
                mTestAdapter.setTestPass(BLE_INDICATE_CHARACTERISTIC);
                mPassed |= PASS_FLAG_INDICATE_CHARACTERISTIC;
                // execute descriptor reading test
                newAction = BleClientService.BLE_CLIENT_ACTION_READ_DESCRIPTOR;
                break;
            case BleClientService.BLE_DESCRIPTOR_READ:
                actionName = getString(R.string.ble_read_descriptor_name);
                mTestAdapter.setTestPass(BLE_READ_DESCRIPTOR);
                mPassed |= PASS_FLAG_READ_DESCRIPTOR;
                // execute descriptor writing test
                newAction = BleClientService.BLE_CLIENT_ACTION_WRITE_DESCRIPTOR;
                break;
            case BleClientService.BLE_DESCRIPTOR_WRITE:
                actionName = getString(R.string.ble_write_descriptor_name);
                mTestAdapter.setTestPass(BLE_WRITE_DESCRIPTOR);
                mPassed |= PASS_FLAG_WRITE_DESCRIPTOR;
                // execute unpermitted descriptor reading test
                newAction = BleClientService.BLE_CLIENT_ACTION_READ_DESCRIPTOR_NO_PERMISSION;
                break;
            case BleClientService.BLE_DESCRIPTOR_READ_NOPERMISSION:
                actionName = getString(R.string.ble_read_descriptor_nopermission_name);
                mTestAdapter.setTestPass(BLE_READ_DESCRIPTOR_NO_PERMISSION);
                mPassed |= PASS_FLAG_READ_DESCRIPTOR_NO_PERMISSION;
                // execute unpermitted descriptor writing test
                newAction = BleClientService.BLE_CLIENT_ACTION_WRITE_DESCRIPTOR_NO_PERMISSION;
                break;
            case BleClientService.BLE_DESCRIPTOR_WRITE_NOPERMISSION:
                actionName = getString(R.string.ble_write_descriptor_nopermission_name);
                mTestAdapter.setTestPass(BLE_WRITE_DESCRIPTOR_NO_PERMISSION);
                mPassed |= PASS_FLAG_WRITE_DESCRIPTOR_NO_PERMISSION;
// TODO: too flaky b/34951749
                // execute RSSI requesting test
                // newAction = BleClientService.BLE_CLIENT_ACTION_READ_RSSI;
                // execute disconnection test
                mPassed |= PASS_FLAG_READ_RSSI;
                Log.d(TAG, "Skip PASS_FLAG_READ_RSSI.");
                newAction = BleClientService.BLE_CLIENT_ACTION_TRIGGER_SERVICE_CHANGED;
                break;
            case BleClientService.BLE_READ_REMOTE_RSSI:
                actionName = getString(R.string.ble_read_rssi_name);
                mTestAdapter.setTestPass(BLE_READ_RSSI);
                mPassed |= PASS_FLAG_READ_RSSI;
                // execute disconnection test
                newAction = BleClientService.BLE_CLIENT_ACTION_TRIGGER_SERVICE_CHANGED;
                break;
            case BleClientService.BLE_ON_SERVICE_CHANGED:
                actionName = getString(R.string.ble_on_service_changed);
                mTestAdapter.setTestPass(BLE_ON_SERVICE_CHANGED);
                mPassed |= PASS_FLAG_ON_SERVICE_CHANGED;
                newAction = BleClientService.BLE_CLIENT_ACTION_CLIENT_DISCONNECT;
                break;
            case BleClientService.BLE_BLUETOOTH_DISCONNECTED:
                mTestAdapter.setTestPass(BLE_CLIENT_DISCONNECT);
                mPassed |= PASS_FLAG_DISCONNECT;
                // all test done
                newAction = null;
                break;
            case BleClientService.BLE_BLUETOOTH_MISMATCH_SECURE:
                showErrorDialog(R.string.ble_bluetooth_mismatch_title, R.string.ble_bluetooth_mismatch_secure_message, true);
                break;
            case BleClientService.BLE_BLUETOOTH_MISMATCH_INSECURE:
                showErrorDialog(R.string.ble_bluetooth_mismatch_title, R.string.ble_bluetooth_mismatch_insecure_message, true);
                break;
            }

            if (previousPassed != mPassed) {
                String logMessage = String.format("Passed Flags has changed from 0x%08X to 0x%08X. Delta=0x%08X",
                                                  previousPassed, mPassed, mPassed ^ previousPassed);
                Log.d(TAG, logMessage);
            }

            mTestAdapter.notifyDataSetChanged();

            if (newAction != null) {
                Log.d(TAG, "Starting " + newAction);
                startIntent.setAction(newAction);
                if (STEP_EXECUTION) {
                    closeDialog();
                    final boolean showProgressDialogValue = showProgressDialog;
                    mDialog = new AlertDialog.Builder(BleClientTestBaseActivity.this)
                            .setTitle(actionName)
                            .setMessage(R.string.ble_test_finished)
                            .setCancelable(false)
                            .setPositiveButton(R.string.ble_test_next,
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            closeDialog();
                                            if (showProgressDialogValue) {
                                                showProgressDialog();
                                            }
                                            startService(startIntent);
                                        }
                                    })
                            .show();
                } else {
                    if (showProgressDialog) {
                        showProgressDialog();
                    }
                    startService(startIntent);
                }
            } else {
                closeDialog();
            }

            if (mPassed == PASS_FLAG_ALL) {
                Log.d(TAG, "All Tests Passed.");
                if (shouldRebootBluetoothAfterTest()) {
                    mBtPowerSwitcher.executeSwitching();
                } else {
                    getPassButton().setEnabled(true);
                }
            }
        }
    };

    private static final long BT_ON_DELAY = 10000;
    private final BluetoothPowerSwitcher mBtPowerSwitcher = new BluetoothPowerSwitcher();
    private class BluetoothPowerSwitcher extends BroadcastReceiver {

        private boolean mIsSwitching = false;
        private BluetoothAdapter mAdapter;

        public void executeSwitching() {
            if (mAdapter == null) {
                BluetoothManager btMgr = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
                mAdapter = btMgr.getAdapter();
            }

            if (!mIsSwitching) {
                IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
                registerReceiver(this, filter);
                mIsSwitching = true;
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.disable();
                    }
                }, 1000);
                showProgressDialog();
            }
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                if (state == BluetoothAdapter.STATE_OFF) {
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.enable();
                        }
                    }, BT_ON_DELAY);
                } else if (state == BluetoothAdapter.STATE_ON) {
                    mIsSwitching = false;
                    unregisterReceiver(this);
                    getPassButton().setEnabled(true);
                    closeDialog();
                }
            }
        }
    }

}