replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaEdrInterface.java
1 /******************************************************************
2  *
3  * Copyright 2014 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 import android.util.Log;
30
31 public class CaEdrInterface {
32     private static Context mContext;
33     private static volatile boolean isEdrInitialized = false;
34
35     private CaEdrInterface(Context context) {
36         synchronized(CaEdrInterface.class) {
37             mContext = context;
38         }
39         if (!isEdrInitialized) {
40             registerIntentFilter();
41             isEdrInitialized = true;
42         }
43     }
44
45     private static IntentFilter registerIntentFilter() {
46         IntentFilter filter = new IntentFilter();
47         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
48         filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
49         filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
50         mContext.registerReceiver(mReceiver, filter);
51         return filter;
52     }
53
54     public static void destroyEdrInterface() {
55         if (isEdrInitialized) {
56             mContext.unregisterReceiver(mReceiver);
57             isEdrInitialized = false;
58         }
59     }
60
61     // Network Monitor
62     private native static void caEdrStateChangedCallback(int state);
63
64     private native static void caEdrBondStateChangedCallback(String addr);
65
66     private native static void caEdrConnectionStateChangedCallback(String addr, int isConnected);
67
68     private static final BroadcastReceiver mReceiver = new BroadcastReceiver() {
69
70         @Override
71         public void onReceive(Context context, Intent intent) {
72
73             String action = intent.getAction();
74
75             if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
76
77                 int state =
78                         intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
79
80                 // STATE_ON:12, STATE_OFF:10
81                 if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
82                 {
83                     caEdrStateChangedCallback(state);
84                 }
85             }
86
87             if (action != null && action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
88
89                 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
90                                                    BluetoothDevice.ERROR);
91
92                 if (bondState == BluetoothDevice.BOND_NONE) {
93                     if ((intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
94                                             BluetoothDevice.ERROR)
95                                                 == BluetoothDevice.BOND_BONDED)) {
96                         BluetoothDevice device
97                             = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
98
99                         caEdrBondStateChangedCallback(device.getAddress());
100                     }
101                 }
102             }
103
104             if (action != null && action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
105                 BluetoothDevice device
106                     = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
107                 if (BluetoothDevice.DEVICE_TYPE_CLASSIC == device.getType())
108                 {
109                     caEdrConnectionStateChangedCallback(device.getAddress(), 0);
110                 }
111             }
112         }
113     };
114 }