1/*
2 * Copyright (C) 2016 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.googlecode.android_scripting.facade.bluetooth;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothDevice;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25
26import java.util.Set;
27
28import com.googlecode.android_scripting.Log;
29
30public class BluetoothDiscoveryHelper {
31
32  public static interface BluetoothDiscoveryListener {
33    public void addBondedDevice(String name, String address);
34
35    public void addDevice(String name, String address);
36
37    public void scanDone();
38  }
39
40  private final Context mContext;
41  private final BluetoothDiscoveryListener mListener;
42  private final BroadcastReceiver mReceiver;
43
44  public BluetoothDiscoveryHelper(Context context, BluetoothDiscoveryListener listener) {
45    mContext = context;
46    mListener = listener;
47    mReceiver = new BluetoothReceiver();
48  }
49
50  private class BluetoothReceiver extends BroadcastReceiver {
51    @Override
52    public void onReceive(Context context, Intent intent) {
53      final String action = intent.getAction();
54
55      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
56        // Get the BluetoothDevice object from the Intent.
57        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
58        Log.d("Found device " + device.getAliasName());
59        // If it's already paired, skip it, because it's been listed already.
60        if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
61          mListener.addDevice(device.getName(), device.getAddress());
62        }
63      } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
64        mListener.scanDone();
65      }
66    }
67  }
68
69  public void startDiscovery() {
70    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
71
72    if (bluetoothAdapter.isDiscovering()) {
73      bluetoothAdapter.cancelDiscovery();
74    }
75
76    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
77    for (BluetoothDevice device : pairedDevices) {
78      mListener.addBondedDevice(device.getName(), device.getAddress());
79    }
80
81    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
82    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
83    mContext.registerReceiver(mReceiver, filter);
84
85    if (!bluetoothAdapter.isEnabled()) {
86      bluetoothAdapter.enable();
87    }
88
89    bluetoothAdapter.startDiscovery();
90  }
91
92  public void cancel() {
93    mContext.unregisterReceiver(mReceiver);
94    mListener.scanDone();
95  }
96}
97