Home | History | Annotate | Download | only in bluetooth
      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 
     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 mListener;
     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 
     36   public BluetoothBroadcastHelper(Context context, BroadcastReceiver listener) {
     37     mContext = context;
     38     mListener = listener;
     39     mReceiver = new BluetoothReceiver();
     40   }
     41 
     42   public void startReceiver() {
     43     IntentFilter mIntentFilter = new IntentFilter();
     44     for(String action : mActions) {
     45       mIntentFilter.addAction(action);
     46     }
     47     mContext.registerReceiver(mReceiver, mIntentFilter);
     48   }
     49 
     50   public static class BluetoothReceiver extends BroadcastReceiver {
     51     @Override
     52     public void onReceive(Context context, Intent intent) {
     53       mListener.onReceive(context, intent);
     54     }
     55   }
     56 
     57   public void stopReceiver() {
     58     mContext.unregisterReceiver(mReceiver);
     59   }
     60 }
     61