Merge branch 'master' into resource-manipulation
[platform/upstream/iotivity.git] / android / android_api / base / 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 java.nio.ByteBuffer;
24 import java.nio.ByteOrder;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.UUID;
28
29 import android.bluetooth.BluetoothAdapter;
30 import android.bluetooth.BluetoothDevice;
31 import android.bluetooth.BluetoothGatt;
32 import android.bluetooth.BluetoothGattCallback;
33 import android.bluetooth.BluetoothGattCharacteristic;
34 import android.bluetooth.BluetoothGattDescriptor;
35 import android.bluetooth.BluetoothGattServerCallback;
36 import android.bluetooth.BluetoothGattService;
37 import android.bluetooth.le.AdvertiseCallback;
38 import android.bluetooth.le.AdvertiseSettings;
39 import android.content.BroadcastReceiver;
40 import android.content.Context;
41 import android.content.Intent;
42 import android.content.IntentFilter;
43 import android.util.Log;
44
45 public class CaLeServerInterface {
46
47     private CaLeServerInterface() {
48
49         caLeRegisterGattServerCallback(mGattServerCallback);
50         caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
51     }
52
53     public static void getLeGattServerCallback() {
54         caLeRegisterGattServerCallback(mGattServerCallback);
55     }
56
57     public static void getBluetoothLeAdvertiseCallback() {
58         caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
59     }
60
61     private native static void caLeRegisterGattServerCallback(BluetoothGattServerCallback callback);
62
63     private native static void caLeRegisterBluetoothLeAdvertiseCallback(AdvertiseCallback callback);
64
65     // BluetoothGattServerCallback
66     private native static void caLeGattServerConnectionStateChangeCallback(
67             BluetoothDevice device, int status, int newState);
68
69     private native static void caLeGattServerServiceAddedCallback(int status,
70                                                                   BluetoothGattService service);
71
72     private native static void caLeGattServerCharacteristicReadRequestCallback(
73             BluetoothDevice device,
74             int requestId, int offset, BluetoothGattCharacteristic characteristic, byte[] data);
75
76     private native static void caLeGattServerCharacteristicWriteRequestCallback(
77             BluetoothDevice device, int requestId,
78             BluetoothGattCharacteristic characteristic, byte[] data, boolean preparedWrite,
79             boolean responseNeeded, int offset, byte[] value);
80
81     private native static void caLeGattServerDescriptorReadRequestCallback(
82             BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor);
83
84     public native static void caLeGattServerDescriptorWriteRequestCallback(
85             BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
86             boolean preparedWrite, boolean responseNeeded, int offset, byte[] value);
87
88     private native static void caLeGattServerExecuteWriteCallback(BluetoothDevice device,
89                                                                  int requestId, boolean execute);
90
91     private native static void caLeGattServerNotificationSentCallback(BluetoothDevice device,
92                                                                      int status);
93
94     // AdvertiseCallback
95     private native static void caLeAdvertiseStartSuccessCallback(
96             AdvertiseSettings settingsInEffect);
97
98     private native static void caLeAdvertiseStartFailureCallback(int errorCode);
99
100     private static final BluetoothGattServerCallback mGattServerCallback =
101                          new BluetoothGattServerCallback() {
102
103         @Override
104         public void onConnectionStateChange(BluetoothDevice device, int status,
105                 int newState) {
106             super.onConnectionStateChange(device, status, newState);
107
108             caLeGattServerConnectionStateChangeCallback(device, status, newState);
109         }
110
111         @Override
112         public void onServiceAdded(int status, BluetoothGattService service) {
113             super.onServiceAdded(status, service);
114
115             caLeGattServerServiceAddedCallback(status, service);
116         }
117
118         @Override
119         public void onCharacteristicReadRequest(
120                 BluetoothDevice device, int requestId, int offset,
121                 BluetoothGattCharacteristic characteristic) {
122             super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
123
124             caLeGattServerCharacteristicReadRequestCallback(device, requestId, offset,
125                                                             characteristic,
126                                                             characteristic.getValue());
127         }
128
129         @Override
130         public void onCharacteristicWriteRequest(
131                 BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
132                 boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
133             super.onCharacteristicWriteRequest(device, requestId, characteristic,
134                     preparedWrite, responseNeeded, offset, value);
135
136             caLeGattServerCharacteristicWriteRequestCallback(device, requestId, characteristic,
137                                                              value, preparedWrite, responseNeeded,
138                                                              offset, value);
139         }
140
141         @Override
142         public void onDescriptorReadRequest(
143                 BluetoothDevice device,
144                 int requestId, int offset, BluetoothGattDescriptor descriptor) {
145             super.onDescriptorReadRequest(device, requestId, offset, descriptor);
146
147             caLeGattServerDescriptorReadRequestCallback(device, requestId, offset, descriptor);
148         }
149
150         @Override
151         public void onDescriptorWriteRequest(
152                 BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
153                 boolean preparedWrite, boolean responseNeeded, int offset,
154                 byte[] value) {
155             super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite,
156                                            responseNeeded, offset, value);
157
158             caLeGattServerDescriptorWriteRequestCallback(device, requestId, descriptor,
159                                                          preparedWrite, responseNeeded, offset,
160                                                          value);
161         }
162
163         @Override
164         public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
165             super.onExecuteWrite(device, requestId, execute);
166
167             caLeGattServerExecuteWriteCallback(device, requestId, execute);
168         }
169
170         @Override
171         public void onNotificationSent(BluetoothDevice device, int status) {
172             super.onNotificationSent(device, status);
173
174             caLeGattServerNotificationSentCallback(device, status);
175         }
176     };
177
178     private static final AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
179
180         @Override
181         public void onStartSuccess(AdvertiseSettings settingsInEffect) {
182             super.onStartSuccess(settingsInEffect);
183
184             caLeAdvertiseStartSuccessCallback(settingsInEffect);
185         }
186
187         @Override
188         public void onStartFailure(int errorCode) {
189             super.onStartFailure(errorCode);
190
191             caLeAdvertiseStartFailureCallback(errorCode);
192         }
193     };
194 }
195