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 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 26public 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