1 /******************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 package org.iotivity.ca;
23 import android.bluetooth.BluetoothAdapter;
24 import android.bluetooth.BluetoothDevice;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
30 public class CaBtPairingInterface {
31 private static Context mContext;
33 private CaBtPairingInterface(Context context) {
34 synchronized(CaBtPairingInterface.class) {
37 registerIntentFilter();
40 private static IntentFilter registerIntentFilter() {
41 IntentFilter filter = new IntentFilter();
42 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
43 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
44 filter.addAction(BluetoothDevice.ACTION_FOUND);
45 mContext.registerReceiver(mReceiver, filter);
49 public static void destroyEdrInterface() {
50 mContext.unregisterReceiver(mReceiver);
53 private native static void oicEdrStateChangedCallback(int state);
55 private native static void oicEdrBondStateChangedCallback(String addr);
57 private native static void oicEdrFoundDeviceCallback(BluetoothDevice device);
59 private static final BroadcastReceiver mReceiver = new BroadcastReceiver() {
62 public void onReceive(Context context, Intent intent) {
64 String action = intent.getAction();
66 if (action != null && action.equals(BluetoothDevice.ACTION_FOUND)) {
67 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
69 // if found device is not paired with this device
70 if (device.getBondState() != BluetoothDevice.BOND_BONDED)
72 oicEdrFoundDeviceCallback(device);
76 if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
79 intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
81 // STATE_ON:12, STATE_OFF:10
82 if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
84 oicEdrStateChangedCallback(state);
88 if (action != null && action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
90 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
91 BluetoothDevice.ERROR);
94 if (bondState == BluetoothDevice.BOND_NONE) {
95 if ((intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
96 BluetoothDevice.ERROR)
97 == BluetoothDevice.BOND_BONDED)) {
98 BluetoothDevice device
99 = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
101 oicEdrBondStateChangedCallback(device.getAddress());