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 public class CaInterface {
33 System.loadLibrary("connectivity_abstraction");
34 System.loadLibrary("ca-interface");
36 private static volatile boolean isConnectionManagerInitialized = false;
37 private static volatile boolean isBtPairingInitialized = false;
39 public static native void initialize(Activity activity, Context context);
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
47 public synchronized static void startManagerService(Context context,
48 OnConnectionManagerStateListener onConnectionManagerStateListener) {
49 if (!isConnectionManagerInitialized) {
50 CaInterface.caManagerInitialize(context, onConnectionManagerStateListener);
51 isConnectionManagerInitialized = true;
56 * Method stop connection manager service.
57 * this method must be called, when Application is destroied.
59 public synchronized static void stopManagerService() {
60 if (isConnectionManagerInitialized) {
61 CaInterface.caManagerTerminate();
62 isConnectionManagerInitialized = false;
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.
71 public synchronized static void setAutoConnectionDevice(String address)
73 CaInterface.initCheckForConnectionManager();
74 CaInterface.caManagerSetAutoConnectionDeviceInfo(address);
78 * Method unset device information for Auto-Connection.
79 * @param address LE address of scanned bluetooth device.
81 public synchronized static void unsetAutoConnectionDevice(String address)
83 CaInterface.initCheckForConnectionManager();
84 CaInterface.caManagerUnsetAutoConnectionDeviceInfo(address);
88 * Interface for connection manager state listener.
89 * Event listeners are notified asynchronously.
91 public interface OnConnectionManagerStateListener {
92 public void onAdapterStateChanged(OcConnectivityType type, boolean enabled);
93 public void onConnectionStateChanged(OcConnectivityType type, String address,
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");
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);
112 * start bluetooth pairing service.
113 * @param context application context
115 public synchronized static void startBtPairingService(Context context,
116 OnBtDeviceFoundListener listener) {
117 if (!isBtPairingInitialized) {
118 CaInterface.caBtPairingInitialize(context, listener);
120 isBtPairingInitialized = true;
125 * stop bluetooth pairing service.
127 public synchronized static void stopBtPairingService() {
128 if (isBtPairingInitialized) {
129 CaInterface.caBtPairingTerminate();
131 isBtPairingInitialized = false;
136 * start bluetooth device scan.
138 public synchronized static void startScan()
140 CaInterface.initCheckForBtPairingUtil();
141 CaInterface.caBtPairingStartScan();
145 * stop bluetooth device scan.
147 public synchronized static void stopScan()
149 CaInterface.initCheckForBtPairingUtil();
150 CaInterface.caBtPairingStopScan();
156 public synchronized static void createBond(BluetoothDevice device)
158 CaInterface.initCheckForBtPairingUtil();
159 CaInterface.caBtPairingCreateBond(device);
162 public interface OnBtDeviceFoundListener {
163 public void onBtDeviceFound(BluetoothDevice device) throws OcException;
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");
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);