• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.googlecode.android_scripting.facade.bluetooth;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 
26 import com.googlecode.android_scripting.Log;
27 
28 import java.util.Set;
29 
30 /**
31  * This class is used to put the device in discovery mode.
32  */
33 public class BluetoothDiscoveryHelper {
34 
35     /**
36      * BLuetooth discovery listener Interface.
37      */
38     public interface BluetoothDiscoveryListener {
39 
40         /**
41          * Add Bonded device.
42          */
addBondedDevice(String name, String address)43         void addBondedDevice(String name, String address);
44 
45         /**
46          * Add Devices.
47          */
addDevice(String name, String address)48         void addDevice(String name, String address);
49 
50         /**
51          * Complete scanning.
52          */
scanDone()53         void scanDone();
54     }
55 
56     private final Context mContext;
57     private final BluetoothDiscoveryListener mListener;
58     private final BroadcastReceiver mReceiver;
59 
BluetoothDiscoveryHelper( Context context, BluetoothDiscoveryListener listener)60     public BluetoothDiscoveryHelper(
61             Context context, BluetoothDiscoveryListener listener) {
62     mContext = context;
63     mListener = listener;
64     mReceiver = new BluetoothReceiver();
65     }
66 
67     private class BluetoothReceiver extends BroadcastReceiver {
68     @Override
onReceive(Context context, Intent intent)69         public void onReceive(Context context, Intent intent) {
70             final String action = intent.getAction();
71 
72             if (BluetoothDevice.ACTION_FOUND.equals(action)) {
73                 // Get the BluetoothDevice object from the Intent.
74                 BluetoothDevice device = intent.getParcelableExtra(
75                         BluetoothDevice.EXTRA_DEVICE);
76                 Log.d("Found device " + device.getAlias());
77                 // If it's already paired, skip it, because it's been listed already.
78                 if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
79                     mListener.addDevice(device.getName(), device.getAddress());
80                 }
81             } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
82                 mListener.scanDone();
83             }
84         }
85     }
86     /**
87      * Start Bluetooth Discovery.
88      */
89 
startDiscovery()90     public void startDiscovery() {
91         BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
92 
93         if (bluetoothAdapter.isDiscovering()) {
94             bluetoothAdapter.cancelDiscovery();
95         }
96 
97         Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
98         for (BluetoothDevice device : pairedDevices) {
99             mListener.addBondedDevice(device.getName(), device.getAddress());
100         }
101 
102         IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
103         filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
104         mContext.registerReceiver(mReceiver, filter);
105 
106         if (!bluetoothAdapter.isEnabled()) {
107             bluetoothAdapter.enable();
108         }
109 
110         bluetoothAdapter.startDiscovery();
111     }
112 
113     /**
114      * Cancel.
115      */
cancel()116     public void cancel() {
117         mContext.unregisterReceiver(mReceiver);
118         mListener.scanDone();
119     }
120 }
121