aaf8bc6f5cf3ea771323427d929dcfaf0f93365d
[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
34     private CaEdrInterface(Context context) {
35         synchronized(CaEdrInterface.class) {
36             mContext = context;
37         }
38         registerIntentFilter();
39     }
40
41     private static IntentFilter registerIntentFilter() {
42         IntentFilter filter = new IntentFilter();
43         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
44         filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
45         filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
46         mContext.registerReceiver(mReceiver, filter);
47         return filter;
48     }
49
50     public static void destroyEdrInterface() {
51         mContext.unregisterReceiver(mReceiver);
52     }
53
54     // Network Monitor
55     private native static void caEdrStateChangedCallback(int state);
56
57     private native static void caEdrBondStateChangedCallback(String addr);
58
59     private native static void caEdrConnectionStateChangedCallback(String addr, int isConnected);
60
61     private static final BroadcastReceiver mReceiver = new BroadcastReceiver() {
62
63         @Override
64         public void onReceive(Context context, Intent intent) {
65
66             String action = intent.getAction();
67
68             if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
69
70                 int state =
71                         intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
72
73                 // STATE_ON:12, STATE_OFF:10
74                 if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
75                 {
76                     caEdrStateChangedCallback(state);
77                 }
78             }
79
80             if (action != null && action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
81
82                 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
83                                                    BluetoothDevice.ERROR);
84
85                 if (bondState == BluetoothDevice.BOND_NONE) {
86                     if ((intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
87                                             BluetoothDevice.ERROR)
88                                                 == BluetoothDevice.BOND_BONDED)) {
89                         BluetoothDevice device
90                             = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
91
92                         caEdrBondStateChangedCallback(device.getAddress());
93                     }
94                 }
95             }
96
97             if (action != null && action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
98                 BluetoothDevice device
99                     = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
100                 if (BluetoothDevice.DEVICE_TYPE_CLASSIC == device.getType())
101                 {
102                     caEdrConnectionStateChangedCallback(device.getAddress(), 0);
103                 }
104             }
105         }
106     };
107 }