6924eb417b53d256a08acf6ea8b956dcdb8ce2cb
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaInterface.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2015 Intel Corporation.
5  *
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.ca;
24
25 import android.content.Context;
26 import android.app.Activity;
27 import android.bluetooth.BluetoothDevice;
28 import org.iotivity.base.OcException;
29 import org.iotivity.base.OcConnectivityType;
30
31 public class CaInterface {
32     static {
33         System.loadLibrary("connectivity_abstraction");
34         System.loadLibrary("ca-interface");
35     }
36     private static volatile boolean isConnectionManagerInitialized = false;
37     private static volatile boolean isBtPairingInitialized = false;
38
39     public static native void initialize(Activity activity, Context context);
40
41     /**
42      *  Method start connection manager service.
43      *  this method has to be called before other API call.
44      *  @param context                                application context
45      *  @param onConnectionManagerStateListener       connection state callback listener
46      */
47     public synchronized static void startManagerService(Context context,
48             OnConnectionManagerStateListener onConnectionManagerStateListener) {
49         if (!isConnectionManagerInitialized) {
50             CaInterface.caManagerInitialize(context, onConnectionManagerStateListener);
51             isConnectionManagerInitialized = true;
52         }
53     }
54
55     /**
56      *  Method stop connection manager service.
57      *  this method must be called, when Application is destroied.
58      */
59     public synchronized static void stopManagerService() {
60         if (isConnectionManagerInitialized) {
61             CaInterface.caManagerTerminate();
62             isConnectionManagerInitialized = false;
63         }
64     }
65
66     /**
67      *  Method set device information for Auto-Connection.
68      *  this method has to be called before FindResource is called.
69      *  @param address                      LE address of scanned bluetooth device.
70      */
71     public synchronized static void setAutoConnectionDevice(String address)
72             throws OcException {
73         CaInterface.initCheckForConnectionManager();
74         CaInterface.caManagerSetAutoConnectionDeviceInfo(address);
75     }
76
77     /**
78      *  Method unset device information for Auto-Connection.
79      *  @param address                      LE address of scanned bluetooth device.
80      */
81     public synchronized static void unsetAutoConnectionDevice(String address)
82             throws OcException {
83         CaInterface.initCheckForConnectionManager();
84         CaInterface.caManagerUnsetAutoConnectionDeviceInfo(address);
85     }
86
87     /**
88      *  Interface for connection manager state listener.
89      *  Event listeners are notified asynchronously.
90      */
91     public interface OnConnectionManagerStateListener {
92         public void onAdapterStateChanged(OcConnectivityType type, boolean enabled);
93         public void onConnectionStateChanged(OcConnectivityType type, String address,
94                 boolean connected);
95     }
96
97     private static void initCheckForConnectionManager() {
98         if (!isConnectionManagerInitialized) {
99             throw new IllegalStateException("ConnectionManager must be started by making "
100                     + "a call to CaInterface.startManagerService before any other API "
101                     + "calls are permitted");
102         }
103     }
104
105     private static native void caManagerInitialize(Context context,
106             OnConnectionManagerStateListener onConnectionManagerStateListener);
107     private static native void caManagerTerminate();
108     private static native void caManagerSetAutoConnectionDeviceInfo(String address);
109     private static native void caManagerUnsetAutoConnectionDeviceInfo(String address);
110
111     /**
112      *  start bluetooth pairing service.
113      *  @param context                      application context
114      */
115     public synchronized static void startBtPairingService(Context context,
116             OnBtDeviceFoundListener listener) {
117         if (!isBtPairingInitialized) {
118             CaInterface.caBtPairingInitialize(context, listener);
119
120             isBtPairingInitialized = true;
121         }
122     }
123
124     /**
125      *  stop bluetooth pairing service.
126      */
127     public synchronized static void stopBtPairingService() {
128         if (isBtPairingInitialized) {
129             CaInterface.caBtPairingTerminate();
130
131             isBtPairingInitialized = false;
132         }
133     }
134
135     /**
136      *  start bluetooth device scan.
137      */
138     public synchronized static void startScan()
139             throws OcException {
140         CaInterface.initCheckForBtPairingUtil();
141         CaInterface.caBtPairingStartScan();
142     }
143
144     /**
145      *  stop bluetooth device scan.
146      */
147     public synchronized static void stopScan()
148             throws OcException {
149         CaInterface.initCheckForBtPairingUtil();
150         CaInterface.caBtPairingStopScan();
151     }
152
153     /**
154      *  create bond
155      */
156     public synchronized static void createBond(BluetoothDevice device)
157             throws OcException {
158         CaInterface.initCheckForBtPairingUtil();
159         CaInterface.caBtPairingCreateBond(device);
160     }
161
162     public interface OnBtDeviceFoundListener {
163         public void onBtDeviceFound(BluetoothDevice device) throws OcException;
164     }
165
166     private static void initCheckForBtPairingUtil() {
167         if (!isBtPairingInitialized) {
168             throw new IllegalStateException("BT pairing Util must be started by making "
169                     + "a call to CaInterface.startBtPairingService before any other API "
170                     + "calls are permitted");
171         }
172     }
173
174     private static native void caBtPairingInitialize(Context context,
175             OnBtDeviceFoundListener listener);
176     private static native void caBtPairingTerminate();
177     private static native void caBtPairingStartScan();
178     private static native void caBtPairingStopScan();
179     private static native void caBtPairingCreateBond(BluetoothDevice device);
180
181     /**
182      *  set BLE scan interval time and working count.
183      *  scanning logic (start scan -> stop scan) will be worked repeatly for workingCount.
184      *  and if you choose '0' value for workingCount parameter,
185      *  scanning will be worked continually as interval time.
186      *  @param intervalTime                  interval time(Seconds).
187      *  @param workingCount                  working count with interval time.
188      */
189
190     public synchronized static void setLeScanIntervalTime(int intervalTime, int workingCount){
191         CaInterface.setLeScanIntervalTimeImpl(intervalTime, workingCount);
192     }
193
194     private static native void setLeScanIntervalTimeImpl(int intervalTime, int workingCount);
195
196
197     public synchronized static int setCipherSuite(OicCipher cipher, OcConnectivityType connType){
198         return CaInterface.setCipherSuiteImpl(cipher.getValue(), connType.getValue());
199     }
200     private static native int setCipherSuiteImpl(int cipher, int adapter);
201
202 }