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 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;
31 public class CaLeServerInterface {
33 private CaLeServerInterface() {
35 caLeRegisterGattServerCallback(mGattServerCallback);
36 caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
39 public static void getLeGattServerCallback() {
40 caLeRegisterGattServerCallback(mGattServerCallback);
43 public static void getBluetoothLeAdvertiseCallback() {
44 caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
47 private native static void caLeRegisterGattServerCallback(BluetoothGattServerCallback callback);
49 private native static void caLeRegisterBluetoothLeAdvertiseCallback(AdvertiseCallback callback);
51 // BluetoothGattServerCallback
52 private native static void caLeGattServerConnectionStateChangeCallback(
53 BluetoothDevice device, int status, int newState);
55 private native static void caLeGattServerNWConnectionStateChangeCallback(
56 BluetoothDevice device, int status, int newState);
58 private native static void caLeGattServerServiceAddedCallback(int status,
59 BluetoothGattService service);
61 private native static void caLeGattServerCharacteristicReadRequestCallback(
62 BluetoothDevice device, byte[] data);
64 private native static void caLeGattServerCharacteristicWriteRequestCallback(
65 BluetoothDevice device, byte[] data);
67 private native static void caLeGattServerNotificationSentCallback(BluetoothDevice device,
71 private native static void caLeAdvertiseStartSuccessCallback(
72 AdvertiseSettings settingsInEffect);
74 private native static void caLeAdvertiseStartFailureCallback(int errorCode);
76 private static final BluetoothGattServerCallback mGattServerCallback =
77 new BluetoothGattServerCallback() {
80 public void onConnectionStateChange(BluetoothDevice device, int status,
82 super.onConnectionStateChange(device, status, newState);
84 caLeGattServerConnectionStateChangeCallback(device, status, newState);
85 caLeGattServerNWConnectionStateChangeCallback(device, status, newState);
89 public void onServiceAdded(int status, BluetoothGattService service) {
90 super.onServiceAdded(status, service);
92 caLeGattServerServiceAddedCallback(status, service);
96 public void onCharacteristicReadRequest(
97 BluetoothDevice device, int requestId, int offset,
98 BluetoothGattCharacteristic characteristic) {
99 super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
101 caLeGattServerCharacteristicReadRequestCallback(device, characteristic.getValue());
105 public void onCharacteristicWriteRequest(
106 BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
107 boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
108 super.onCharacteristicWriteRequest(device, requestId, characteristic,
109 preparedWrite, responseNeeded, offset, value);
111 caLeGattServerCharacteristicWriteRequestCallback(device, value);
115 public void onDescriptorReadRequest(
116 BluetoothDevice device,
117 int requestId, int offset, BluetoothGattDescriptor descriptor) {
118 super.onDescriptorReadRequest(device, requestId, offset, descriptor);
122 public void onDescriptorWriteRequest(
123 BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
124 boolean preparedWrite, boolean responseNeeded, int offset,
126 super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite,
127 responseNeeded, offset, value);
131 public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
132 super.onExecuteWrite(device, requestId, execute);
136 public void onNotificationSent(BluetoothDevice device, int status) {
137 super.onNotificationSent(device, status);
139 caLeGattServerNotificationSentCallback(device, status);
143 private static final AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
146 public void onStartSuccess(AdvertiseSettings settingsInEffect) {
147 super.onStartSuccess(settingsInEffect);
149 caLeAdvertiseStartSuccessCallback(settingsInEffect);
153 public void onStartFailure(int errorCode) {
154 super.onStartFailure(errorCode);
156 caLeAdvertiseStartFailureCallback(errorCode);