• 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 public class BluetoothBroadcastHelper {
27 
28     private static BroadcastReceiver sListener;
29     private final Context mContext;
30     private final BroadcastReceiver mReceiver;
31     private final String[] mActions = {BluetoothDevice.ACTION_FOUND,
32             BluetoothDevice.ACTION_UUID,
33             BluetoothAdapter.ACTION_DISCOVERY_STARTED,
34             BluetoothAdapter.ACTION_DISCOVERY_FINISHED};
35 
BluetoothBroadcastHelper(Context context, BroadcastReceiver listener)36     public BluetoothBroadcastHelper(Context context, BroadcastReceiver listener) {
37         mContext = context;
38         sListener = listener;
39         mReceiver = new BluetoothReceiver();
40     }
41 
42     /**
43      * Start the Receiver.
44      */
startReceiver()45     public void startReceiver() {
46         IntentFilter mIntentFilter = new IntentFilter();
47         for (String action : mActions) {
48             mIntentFilter.addAction(action);
49         }
50         mContext.registerReceiver(mReceiver, mIntentFilter);
51     }
52 
53     /**
54      * Bluetooth Receiver Class.
55      */
56     public static class BluetoothReceiver extends BroadcastReceiver {
57         @Override
onReceive(Context context, Intent intent)58         public void onReceive(Context context, Intent intent) {
59             sListener.onReceive(context, intent);
60         }
61     }
62 
63     /**
64      * Unregister the receiver.
65      */
stopReceiver()66     public void stopReceiver() {
67         mContext.unregisterReceiver(mReceiver);
68     }
69 }
70