Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / android / casample / cAInterface / src / main / java / org / iotivity / ca / CaLeServerInterface.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.BluetoothDevice;
24 import android.bluetooth.BluetoothGattCharacteristic;
25 import android.bluetooth.BluetoothGattDescriptor;
26 import android.bluetooth.BluetoothGattServerCallback;
27 import android.bluetooth.BluetoothGattService;
28 import android.bluetooth.le.AdvertiseCallback;
29 import android.bluetooth.le.AdvertiseSettings;
30
31 public class CaLeServerInterface {
32
33     private CaLeServerInterface() {
34
35         caLeRegisterGattServerCallback(mGattServerCallback);
36         caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
37     }
38
39     public static void getLeGattServerCallback() {
40         caLeRegisterGattServerCallback(mGattServerCallback);
41     }
42
43     public static void getBluetoothLeAdvertiseCallback() {
44         caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
45     }
46
47     private native static void caLeRegisterGattServerCallback(BluetoothGattServerCallback callback);
48
49     private native static void caLeRegisterBluetoothLeAdvertiseCallback(AdvertiseCallback callback);
50
51     // BluetoothGattServerCallback
52     private native static void caLeGattServerConnectionStateChangeCallback(
53             BluetoothDevice device, int status, int newState);
54
55     private native static void caLeGattServerServiceAddedCallback(int status,
56                                                                   BluetoothGattService service);
57
58     private native static void caLeGattServerCharacteristicReadRequestCallback(
59             BluetoothDevice device, byte[] data);
60
61     private native static void caLeGattServerCharacteristicWriteRequestCallback(
62             BluetoothDevice device, byte[] data);
63
64     private native static void caLeGattServerNotificationSentCallback(BluetoothDevice device,
65                                                                      int status);
66
67     // AdvertiseCallback
68     private native static void caLeAdvertiseStartSuccessCallback(
69             AdvertiseSettings settingsInEffect);
70
71     private native static void caLeAdvertiseStartFailureCallback(int errorCode);
72
73     private static final BluetoothGattServerCallback mGattServerCallback =
74                          new BluetoothGattServerCallback() {
75
76         @Override
77         public void onConnectionStateChange(BluetoothDevice device, int status,
78                 int newState) {
79             super.onConnectionStateChange(device, status, newState);
80
81             caLeGattServerConnectionStateChangeCallback(device, status, newState);
82         }
83
84         @Override
85         public void onServiceAdded(int status, BluetoothGattService service) {
86             super.onServiceAdded(status, service);
87
88             caLeGattServerServiceAddedCallback(status, service);
89         }
90
91         @Override
92         public void onCharacteristicReadRequest(
93                 BluetoothDevice device, int requestId, int offset,
94                 BluetoothGattCharacteristic characteristic) {
95             super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
96
97             caLeGattServerCharacteristicReadRequestCallback(device, characteristic.getValue());
98         }
99
100         @Override
101         public void onCharacteristicWriteRequest(
102                 BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
103                 boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
104             super.onCharacteristicWriteRequest(device, requestId, characteristic,
105                     preparedWrite, responseNeeded, offset, value);
106
107             caLeGattServerCharacteristicWriteRequestCallback(device, value);
108         }
109
110         @Override
111         public void onDescriptorReadRequest(
112                 BluetoothDevice device,
113                 int requestId, int offset, BluetoothGattDescriptor descriptor) {
114             super.onDescriptorReadRequest(device, requestId, offset, descriptor);
115         }
116
117         @Override
118         public void onDescriptorWriteRequest(
119                 BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
120                 boolean preparedWrite, boolean responseNeeded, int offset,
121                 byte[] value) {
122             super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite,
123                                            responseNeeded, offset, value);
124         }
125
126         @Override
127         public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
128             super.onExecuteWrite(device, requestId, execute);
129         }
130
131         @Override
132         public void onNotificationSent(BluetoothDevice device, int status) {
133             super.onNotificationSent(device, status);
134
135             caLeGattServerNotificationSentCallback(device, status);
136         }
137     };
138
139     private static final AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
140
141         @Override
142         public void onStartSuccess(AdvertiseSettings settingsInEffect) {
143             super.onStartSuccess(settingsInEffect);
144
145             caLeAdvertiseStartSuccessCallback(settingsInEffect);
146         }
147
148         @Override
149         public void onStartFailure(int errorCode) {
150             super.onStartFailure(errorCode);
151
152             caLeAdvertiseStartFailureCallback(errorCode);
153         }
154     };
155 }