6c29709befc5ffb9a0f83965063dc0ab184c1b1c
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / android / sample_service / src / com / iotivity / jar / CALeInterface.java
1
2 package com.iotivity.jar;
3
4 import android.bluetooth.BluetoothAdapter;
5 import android.bluetooth.BluetoothDevice;
6 import android.bluetooth.BluetoothGatt;
7 import android.bluetooth.BluetoothGattCallback;
8 import android.bluetooth.BluetoothGattCharacteristic;
9 import android.bluetooth.BluetoothGattDescriptor;
10 import android.bluetooth.BluetoothGattServerCallback;
11 import android.bluetooth.BluetoothGattService;
12
13 public class CALeInterface {
14     
15     public CALeInterface() {
16         CARegisterLeScanCallback(mLeScanCallback);
17         CARegisterLeGattCallback(mGattCallback);
18 //        CARegisterLeGattServerCallback(mGattServerCallback);
19 //        CARegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
20 //        CARegisterBluetoothLeScanCallback(mScanCallback);
21     }
22     
23     public static void getLeScanCallback() {
24         CARegisterLeScanCallback(mLeScanCallback);
25     }
26     
27     public static void getLeGattCallback() {
28         CARegisterLeGattCallback(mGattCallback);
29     }
30     
31 //    public static void getLeGattServerCallback() {
32 //        CARegisterLeGattServerCallback(mGattServerCallback);
33 //    }
34     
35 //    public static void getBluetoothLeAdvertiseCallback() {
36 //        CARegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
37 //    }
38 //    
39 //    public static void getBluetoothLeScanCallback() {
40 //        CARegisterBluetoothLeScanCallback(mScanCallback);
41 //    }
42
43     public native static void CARegisterLeScanCallback(BluetoothAdapter.LeScanCallback callback);
44     public native static void CARegisterLeGattCallback(BluetoothGattCallback callback);
45     public native static void CARegisterLeGattServerCallback(BluetoothGattServerCallback callback);
46 //    public native static void CARegisterBluetoothLeAdvertiseCallback(AdvertiseCallback callback);
47 //    public native static void CARegisterBluetoothLeScanCallback(ScanCallback callback);
48     
49     // BluetoothAdapter.LeScanCallback
50     public native static void CALeScanCallback(BluetoothDevice device, int rssi, byte[] scanRecord);
51     
52     // BluetoothGattCallback
53     public native static void CALeGattConnectionStateChangeCallback(BluetoothGatt gatt, int status, int newState);
54     
55     public native static void CALeGattServicesDiscoveredCallback(BluetoothGatt gatt, int status);
56     
57     public native static void CALeGattCharacteristicReadCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, String data, int status);
58     
59     public native static void CALeGattCharacteristicWriteCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, String data, int status);
60     
61     public native static void CALeGattCharacteristicChangedCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic);
62     
63     public native static void CALeGattDescriptorReadCallback(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status);
64     
65     public native static void CALeGattDescriptorWriteCallback(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status);
66     
67     public native static void CALeGattReliableWriteCompletedCallback(BluetoothGatt gatt, int status);
68     
69     public native static void CALeGattReadRemoteRssiCallback(BluetoothGatt gatt, int rssi, int status);
70     
71     // BluetoothGattServerCallback
72     public native static void CALeGattServerConnectionStateChangeCallback(BluetoothDevice device, int status, int newState);
73     
74     public native static void CALeGattServerServiceAddedCallback(int status, BluetoothGattService service);
75     
76     public native static void CALeGattServerCharacteristicReadRequestCallback(BluetoothDevice device,
77             int requestId, int offset, BluetoothGattCharacteristic characteristic);
78     
79     public native static void CALeGattServerCharacteristicWriteRequestCallback(BluetoothDevice device, int requestId,
80             BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value);
81     
82     public native static void CALeGattServerDescriptorReadRequestCallback(BluetoothDevice device,
83             int requestId, int offset, BluetoothGattDescriptor descriptor);
84     
85     public native static void CALeGattServerDescriptorWriteRequestCallback(BluetoothDevice device, int requestId,
86             BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value);
87     
88     public native static void CALeGattServerExecuteWriteCallback(BluetoothDevice device, int requestId, boolean execute);
89     
90     public native static void CALeGattServerNotificationSentCallback(BluetoothDevice device, int status);
91
92     // AdvertiseCallback
93 //    public native static void CALeAdvertiseStartSuccessCallback(AdvertiseSettings settingsInEffect);
94 //    
95 //    public native static void CALeAdvertiseStartFailureCallback(int errorCode);
96     
97     // ScanCallback
98 //    public native static void CABluetoothLeScanResultCallback(int callbackType, ScanResult result);
99 //    
100 //    public native static void CABluetoothLeBatchScanResultsCallback(List<ScanResult> results);
101 //    
102 //    public native static void CABluetoothLeScanFailedCallback(int errorCode);
103     
104      
105     private static BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
106         
107         @Override
108         public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
109                 
110             try {
111                 CALeScanCallback(device, rssi, scanRecord);     
112             } catch(UnsatisfiedLinkError e) {
113             
114             }
115         }
116     };
117
118     private static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
119
120         @Override
121         public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
122             super.onConnectionStateChange(gatt, status, newState);
123             
124             CALeGattConnectionStateChangeCallback(gatt, status, newState);
125         }
126
127         @Override
128         public void onServicesDiscovered(BluetoothGatt gatt, int status) {
129             super.onServicesDiscovered(gatt, status);
130             
131             CALeGattServicesDiscoveredCallback(gatt, status);
132         }
133
134         @Override
135         public void onCharacteristicRead(BluetoothGatt gatt,
136                 BluetoothGattCharacteristic characteristic, int status) {
137             super.onCharacteristicRead(gatt, characteristic, status);
138             
139             String data = new String(characteristic.getValue());
140             CALeGattCharacteristicReadCallback(gatt, characteristic, data, status);
141         }
142
143         @Override
144         public void onCharacteristicWrite(BluetoothGatt gatt,
145                 BluetoothGattCharacteristic characteristic, int status) {
146             super.onCharacteristicWrite(gatt, characteristic, status);
147             
148             String data = new String(characteristic.getValue());
149             CALeGattCharacteristicWriteCallback(gatt, characteristic, data, status);
150         }
151
152         @Override
153         public void onCharacteristicChanged(BluetoothGatt gatt,
154                 BluetoothGattCharacteristic characteristic) {
155             super.onCharacteristicChanged(gatt, characteristic);
156             
157             String data = new String(characteristic.getValue());
158             CALeGattCharacteristicChangedCallback(gatt, characteristic, data);
159         }
160
161         @Override
162         public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
163                 int status) {
164             super.onDescriptorRead(gatt, descriptor, status);
165             
166             CALeGattDescriptorReadCallback(gatt, descriptor, status);
167         }
168
169         @Override
170         public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
171                 int status) {
172             super.onDescriptorWrite(gatt, descriptor, status);
173             
174             CALeGattDescriptorWriteCallback(gatt, descriptor, status);
175         }
176
177         @Override
178         public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
179             super.onReliableWriteCompleted(gatt, status);
180             
181             CALeGattReliableWriteCompletedCallback(gatt, status);
182         }
183
184         @Override
185         public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
186             super.onReadRemoteRssi(gatt, rssi, status);
187             
188             CALeGattReadRemoteRssiCallback(gatt, rssi, status);
189         }
190     };
191     
192     /*
193     private static final BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
194
195         @Override
196         public void onConnectionStateChange(BluetoothDevice device, int status,
197                 int newState) {
198             super.onConnectionStateChange(device, status, newState);
199             
200             CALeGattServerConnectionStateChangeCallback(device, status, newState);
201         }
202
203         @Override
204         public void onServiceAdded(int status, BluetoothGattService service) {
205             super.onServiceAdded(status, service);
206             
207             CALeGattServerServiceAddedCallback(status, service);
208         }
209
210         @Override
211         public void onCharacteristicReadRequest(BluetoothDevice device,
212                 int requestId, int offset,
213                 BluetoothGattCharacteristic characteristic) {
214             super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
215             
216             CALeGattServerCharacteristicReadRequestCallback(device, requestId, offset, characteristic);
217         }
218
219         @Override
220         public void onCharacteristicWriteRequest(BluetoothDevice device,
221                 int requestId, BluetoothGattCharacteristic characteristic,
222                 boolean preparedWrite, boolean responseNeeded, int offset,
223                 byte[] value) {
224             super.onCharacteristicWriteRequest(device, requestId, characteristic,
225                     preparedWrite, responseNeeded, offset, value);
226             
227             CALeGattServerCharacteristicWriteRequestCallback(device, requestId, characteristic,
228                     preparedWrite, responseNeeded, offset, value);
229         }
230
231         @Override
232         public void onDescriptorReadRequest(BluetoothDevice device,
233                 int requestId, int offset, BluetoothGattDescriptor descriptor) {
234             super.onDescriptorReadRequest(device, requestId, offset, descriptor);
235             
236             CALeGattServerDescriptorReadRequestCallback(device, requestId, offset, descriptor);
237         }
238
239         @Override
240         public void onDescriptorWriteRequest(BluetoothDevice device,
241                 int requestId, BluetoothGattDescriptor descriptor,
242                 boolean preparedWrite, boolean responseNeeded, int offset,
243                 byte[] value) {
244             super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite,
245                     responseNeeded, offset, value);
246             
247             CALeGattServerDescriptorWriteRequestCallback(device, requestId, descriptor, preparedWrite,
248                     responseNeeded, offset, value);
249         }
250
251         @Override
252         public void onExecuteWrite(BluetoothDevice device, int requestId,
253                 boolean execute) {
254             super.onExecuteWrite(device, requestId, execute);
255             
256             CALeGattServerExecuteWriteCallback(device, requestId, execute);
257         }
258
259         @Override
260         public void onNotificationSent(BluetoothDevice device, int status) {
261             super.onNotificationSent(device, status);
262             
263             CALeGattServerNotificationSentCallback(device, status);
264         }
265     };
266     
267     private static final AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
268
269         @Override
270         public void onStartSuccess(AdvertiseSettings settingsInEffect) {
271             super.onStartSuccess(settingsInEffect);
272             
273             CALeAdvertiseStartSuccessCallback(settingsInEffect);
274         }
275
276         @Override
277         public void onStartFailure(int errorCode) {
278             super.onStartFailure(errorCode);
279             
280             CALeAdvertiseStartFailureCallback(errorCode);
281         }
282     };
283     
284     private static final ScanCallback mScanCallback = new ScanCallback() {
285
286         @Override
287         public void onScanResult(int callbackType, ScanResult result) {
288             super.onScanResult(callbackType, result);
289             
290             CABluetoothLeScanResultCallback(callbackType, result);
291         }
292
293         @Override
294         public void onBatchScanResults(List<ScanResult> results) {
295             super.onBatchScanResults(results);
296             
297             CABluetoothLeBatchScanResultsCallback(results);
298         }
299
300         @Override
301         public void onScanFailed(int errorCode) {
302             super.onScanFailed(errorCode);
303             
304             CABluetoothLeScanFailedCallback(errorCode);
305         }
306     };
307     */
308 }