1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 package org.iotivity.ca;
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;
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;
45 public class CaLeServerInterface {
47 private CaLeServerInterface() {
49 caLeRegisterGattServerCallback(mGattServerCallback);
50 caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
53 public static void getLeGattServerCallback() {
54 caLeRegisterGattServerCallback(mGattServerCallback);
57 public static void getBluetoothLeAdvertiseCallback() {
58 caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
61 private native static void caLeRegisterGattServerCallback(BluetoothGattServerCallback callback);
63 private native static void caLeRegisterBluetoothLeAdvertiseCallback(AdvertiseCallback callback);
65 // BluetoothGattServerCallback
66 private native static void caLeGattServerConnectionStateChangeCallback(
67 BluetoothDevice device, int status, int newState);
69 private native static void caLeGattServerServiceAddedCallback(int status,
70 BluetoothGattService service);
72 private native static void caLeGattServerCharacteristicReadRequestCallback(
73 BluetoothDevice device,
74 int requestId, int offset, BluetoothGattCharacteristic characteristic, byte[] data);
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);
81 private native static void caLeGattServerDescriptorReadRequestCallback(
82 BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor);
84 public native static void caLeGattServerDescriptorWriteRequestCallback(
85 BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
86 boolean preparedWrite, boolean responseNeeded, int offset, byte[] value);
88 private native static void caLeGattServerExecuteWriteCallback(BluetoothDevice device,
89 int requestId, boolean execute);
91 private native static void caLeGattServerNotificationSentCallback(BluetoothDevice device,
95 private native static void caLeAdvertiseStartSuccessCallback(
96 AdvertiseSettings settingsInEffect);
98 private native static void caLeAdvertiseStartFailureCallback(int errorCode);
100 private static final BluetoothGattServerCallback mGattServerCallback =
101 new BluetoothGattServerCallback() {
104 public void onConnectionStateChange(BluetoothDevice device, int status,
106 super.onConnectionStateChange(device, status, newState);
108 caLeGattServerConnectionStateChangeCallback(device, status, newState);
112 public void onServiceAdded(int status, BluetoothGattService service) {
113 super.onServiceAdded(status, service);
115 caLeGattServerServiceAddedCallback(status, service);
119 public void onCharacteristicReadRequest(
120 BluetoothDevice device, int requestId, int offset,
121 BluetoothGattCharacteristic characteristic) {
122 super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
124 caLeGattServerCharacteristicReadRequestCallback(device, requestId, offset,
126 characteristic.getValue());
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);
136 caLeGattServerCharacteristicWriteRequestCallback(device, requestId, characteristic,
137 value, preparedWrite, responseNeeded,
142 public void onDescriptorReadRequest(
143 BluetoothDevice device,
144 int requestId, int offset, BluetoothGattDescriptor descriptor) {
145 super.onDescriptorReadRequest(device, requestId, offset, descriptor);
147 caLeGattServerDescriptorReadRequestCallback(device, requestId, offset, descriptor);
151 public void onDescriptorWriteRequest(
152 BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
153 boolean preparedWrite, boolean responseNeeded, int offset,
155 super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite,
156 responseNeeded, offset, value);
158 caLeGattServerDescriptorWriteRequestCallback(device, requestId, descriptor,
159 preparedWrite, responseNeeded, offset,
164 public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
165 super.onExecuteWrite(device, requestId, execute);
167 caLeGattServerExecuteWriteCallback(device, requestId, execute);
171 public void onNotificationSent(BluetoothDevice device, int status) {
172 super.onNotificationSent(device, status);
174 caLeGattServerNotificationSentCallback(device, status);
178 private static final AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
181 public void onStartSuccess(AdvertiseSettings settingsInEffect) {
182 super.onStartSuccess(settingsInEffect);
184 caLeAdvertiseStartSuccessCallback(settingsInEffect);
188 public void onStartFailure(int errorCode) {
189 super.onStartFailure(errorCode);
191 caLeAdvertiseStartFailureCallback(errorCode);