Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / android / casample / cAInterface / 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
30 public class CaEdrInterface {
31
32     private CaEdrInterface(Context context) {
33
34         registerIntentFilter(context);
35     }
36
37     private static IntentFilter registerIntentFilter(Context context) {
38         IntentFilter filter = new IntentFilter();
39         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
40         filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
41         context.registerReceiver(mReceiver, filter);
42         return filter;
43     }
44
45     // Network Monitor
46     private native static void caEdrStateChangedCallback(int state);
47
48     private native static void caEdrBondStateChangedCallback(String addr);
49
50     private static final BroadcastReceiver mReceiver = new BroadcastReceiver() {
51
52         @Override
53         public void onReceive(Context context, Intent intent) {
54
55             String action = intent.getAction();
56
57             if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
58
59                 int state =
60                         intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
61
62                 // STATE_ON:12, STATE_OFF:10
63                 if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
64                 {
65                     caEdrStateChangedCallback(state);
66                 }
67             }
68
69             if (action != null && action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
70
71                 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
72                                                    BluetoothDevice.ERROR);
73
74                 if (bondState == BluetoothDevice.BOND_NONE) {
75                     if ((intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
76                                             BluetoothDevice.ERROR)
77                                                 == BluetoothDevice.BOND_BONDED)) {
78                         BluetoothDevice device
79                             = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
80
81                         caEdrBondStateChangedCallback(device.getAddress());
82                     }
83                 }
84             }
85         }
86     };
87 }