2 *******************************************************************
4 * Copyright 2015 Intel Corporation.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.ca;
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;
31 import java.util.EnumSet;
33 public class CaInterface {
35 System.loadLibrary("connectivity_abstraction");
36 System.loadLibrary("ca-interface");
38 private static volatile boolean isConnectionManagerInitialized = false;
39 private static volatile boolean isBtPairingInitialized = false;
41 public static native void initialize(Activity activity, Context context);
44 * Method start connection manager service.
45 * this method has to be called before other API call.
46 * @param context application context
47 * @param onConnectionManagerStateListener connection state callback listener
49 public synchronized static void startManagerService(Context context,
50 OnConnectionManagerStateListener onConnectionManagerStateListener) {
51 if (!isConnectionManagerInitialized) {
52 CaInterface.caManagerInitialize(context, onConnectionManagerStateListener);
53 isConnectionManagerInitialized = true;
58 * Method stop connection manager service.
59 * this method must be called, when Application is destroied.
61 public synchronized static void stopManagerService() {
62 if (isConnectionManagerInitialized) {
63 CaInterface.caManagerTerminate();
64 isConnectionManagerInitialized = false;
69 * Method set device information for Auto-Connection.
70 * this method has to be called before FindResource is called.
71 * @param address LE address of scanned bluetooth device.
73 public synchronized static void setAutoConnectionDevice(String address)
75 CaInterface.initCheckForConnectionManager();
76 CaInterface.caManagerSetAutoConnectionDeviceInfo(address);
80 * Method unset device information for Auto-Connection.
81 * @param address LE address of scanned bluetooth device.
83 public synchronized static void unsetAutoConnectionDevice(String address)
85 CaInterface.initCheckForConnectionManager();
86 CaInterface.caManagerUnsetAutoConnectionDeviceInfo(address);
90 * Interface for connection manager state listener.
91 * Event listeners are notified asynchronously.
93 public interface OnConnectionManagerStateListener {
94 public void onAdapterStateChanged(OcConnectivityType type, boolean enabled);
95 public void onConnectionStateChanged(OcConnectivityType type, String address,
99 private static void initCheckForConnectionManager() {
100 if (!isConnectionManagerInitialized) {
101 throw new IllegalStateException("ConnectionManager must be started by making "
102 + "a call to CaInterface.startManagerService before any other API "
103 + "calls are permitted");
107 private static native void caManagerInitialize(Context context,
108 OnConnectionManagerStateListener onConnectionManagerStateListener);
109 private static native void caManagerTerminate();
110 private static native void caManagerSetAutoConnectionDeviceInfo(String address);
111 private static native void caManagerUnsetAutoConnectionDeviceInfo(String address);
114 * start bluetooth pairing service.
115 * @param context application context
117 public synchronized static void startBtPairingService(Context context,
118 OnBtDeviceFoundListener listener) {
119 if (!isBtPairingInitialized) {
120 CaInterface.caBtPairingInitialize(context, listener);
122 isBtPairingInitialized = true;
127 * stop bluetooth pairing service.
129 public synchronized static void stopBtPairingService() {
130 if (isBtPairingInitialized) {
131 CaInterface.caBtPairingTerminate();
133 isBtPairingInitialized = false;
138 * start bluetooth device scan.
140 public synchronized static void startScan()
142 CaInterface.initCheckForBtPairingUtil();
143 CaInterface.caBtPairingStartScan();
147 * stop bluetooth device scan.
149 public synchronized static void stopScan()
151 CaInterface.initCheckForBtPairingUtil();
152 CaInterface.caBtPairingStopScan();
158 public synchronized static void createBond(BluetoothDevice device)
160 CaInterface.initCheckForBtPairingUtil();
161 CaInterface.caBtPairingCreateBond(device);
164 public interface OnBtDeviceFoundListener {
165 public void onBtDeviceFound(BluetoothDevice device) throws OcException;
168 private static void initCheckForBtPairingUtil() {
169 if (!isBtPairingInitialized) {
170 throw new IllegalStateException("BT pairing Util must be started by making "
171 + "a call to CaInterface.startBtPairingService before any other API "
172 + "calls are permitted");
176 private static native void caBtPairingInitialize(Context context,
177 OnBtDeviceFoundListener listener);
178 private static native void caBtPairingTerminate();
179 private static native void caBtPairingStartScan();
180 private static native void caBtPairingStopScan();
181 private static native void caBtPairingCreateBond(BluetoothDevice device);
184 * set BLE scan interval time and working count.
185 * scanning logic (start scan -> stop scan) will be worked repeatly for workingCount.
186 * and if you choose '0' value for workingCount parameter,
187 * scanning will be worked continually as interval time.
188 * @param intervalTime interval time(Seconds).
189 * @param workingCount working count with interval time.
191 public synchronized static void setLeScanIntervalTime(int intervalTime, int workingCount){
192 CaInterface.setLeScanIntervalTimeImpl(intervalTime, workingCount);
194 private static native void setLeScanIntervalTimeImpl(int intervalTime, int workingCount);
198 * if you want to start scan, it can be triggered by setLeScanIntervalTime or
199 * other request API like findResource.
201 public synchronized static void stopLeScan(){
202 CaInterface.stopLeScanImpl();
204 private static native void stopLeScanImpl();
207 * start BLE Advertising.
209 public synchronized static void startLeAdvertising(){
210 CaInterface.startLeAdvertisingImpl();
212 private static native void startLeAdvertisingImpl();
215 * stop BLE Advertising.
217 public synchronized static void stopLeAdvertising(){
218 CaInterface.stopLeAdvertisingImpl();
220 private static native void stopLeAdvertisingImpl();
225 public synchronized static void setBTConfigure(int flag){
226 CaInterface.setBTConfigureImpl(flag);
228 private static native void setBTConfigureImpl(int flag);
230 public synchronized static int setCipherSuite(OicCipher cipher, OcConnectivityType connType){
231 return CaInterface.setCipherSuiteImpl(cipher.getValue(), connType.getValue());
233 private static native int setCipherSuiteImpl(int cipher, int adapter);
236 * Disconnect TCP session.
237 * It will disconnect the current TCP session.
239 * @param address host address [IP address].
240 * @param port Port number.
241 * @param transportFlags Set of Transport flags.
243 * @return Result of the API call.
245 * @see CaTransportFlags
247 public synchronized static int disconnectTCPSession(String address, int port,
248 EnumSet<CaTransportFlags> transportFlags) {
249 int transPortFlagsInt = 0;
250 for (CaTransportFlags flag : CaTransportFlags.values()) {
251 if (transportFlags.contains(flag)) {
252 transPortFlagsInt |= flag.getValue();
256 return CaInterface.disconnectTCPSessionImpl(address, port, transPortFlagsInt);
258 private static native int disconnectTCPSessionImpl(String address, int port, int flags);