replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaBtPairingInterface.java
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 package org.iotivity.ca;
22
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;
29
30 public class CaBtPairingInterface {
31     private static Context mContext;
32     private static volatile boolean isBtInitialized = false;
33
34     private CaBtPairingInterface(Context context) {
35         synchronized(CaBtPairingInterface.class) {
36             mContext = context;
37         }
38         if (!isBtInitialized) {
39             registerIntentFilter();
40             isBtInitialized = true;
41         }
42     }
43
44     private static IntentFilter registerIntentFilter() {
45         IntentFilter filter = new IntentFilter();
46         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
47         filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
48         filter.addAction(BluetoothDevice.ACTION_FOUND);
49         mContext.registerReceiver(mReceiver, filter);
50         return filter;
51     }
52
53     public static void destroyEdrInterface() {
54         if (isBtInitialized) {
55             mContext.unregisterReceiver(mReceiver);
56             isBtInitialized = false;
57         }
58     }
59
60     private native static void oicEdrStateChangedCallback(int state);
61
62     private native static void oicEdrBondStateChangedCallback(String addr);
63
64     private native static void oicEdrFoundDeviceCallback(BluetoothDevice device);
65
66     private static final BroadcastReceiver mReceiver = new BroadcastReceiver() {
67
68         @Override
69         public void onReceive(Context context, Intent intent) {
70
71             String action = intent.getAction();
72
73             if (action != null && action.equals(BluetoothDevice.ACTION_FOUND)) {
74                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
75
76                 // if found device is not paired with this device
77                 if (device.getBondState() != BluetoothDevice.BOND_BONDED)
78                 {
79                     oicEdrFoundDeviceCallback(device);
80                 }
81             }
82
83             if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
84
85                 int state =
86                         intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
87
88                 // STATE_ON:12, STATE_OFF:10
89                 if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
90                 {
91                     oicEdrStateChangedCallback(state);
92                 }
93             }
94
95             if (action != null && action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
96
97                 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
98                                                    BluetoothDevice.ERROR);
99
100                 // unpairing event
101                 if (bondState == BluetoothDevice.BOND_NONE) {
102                     if ((intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
103                                             BluetoothDevice.ERROR)
104                                                 == BluetoothDevice.BOND_BONDED)) {
105                         BluetoothDevice device
106                             = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
107
108                         oicEdrBondStateChangedCallback(device.getAddress());
109                     }
110                 }
111             }
112         }
113     };
114 }