2 package com.iotivity.jar;
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;
13 public class CALeInterface {
15 public CALeInterface() {
16 CARegisterLeScanCallback(mLeScanCallback);
17 CARegisterLeGattCallback(mGattCallback);
18 // CARegisterLeGattServerCallback(mGattServerCallback);
19 // CARegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
20 // CARegisterBluetoothLeScanCallback(mScanCallback);
23 public static void getLeScanCallback() {
24 CARegisterLeScanCallback(mLeScanCallback);
27 public static void getLeGattCallback() {
28 CARegisterLeGattCallback(mGattCallback);
31 // public static void getLeGattServerCallback() {
32 // CARegisterLeGattServerCallback(mGattServerCallback);
35 // public static void getBluetoothLeAdvertiseCallback() {
36 // CARegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
39 // public static void getBluetoothLeScanCallback() {
40 // CARegisterBluetoothLeScanCallback(mScanCallback);
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);
49 // BluetoothAdapter.LeScanCallback
50 public native static void CALeScanCallback(BluetoothDevice device, int rssi, byte[] scanRecord);
52 // BluetoothGattCallback
53 public native static void CALeGattConnectionStateChangeCallback(BluetoothGatt gatt, int status, int newState);
55 public native static void CALeGattServicesDiscoveredCallback(BluetoothGatt gatt, int status);
57 public native static void CALeGattCharacteristicReadCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, String data, int status);
59 public native static void CALeGattCharacteristicWriteCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, String data, int status);
61 public native static void CALeGattCharacteristicChangedCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic);
63 public native static void CALeGattDescriptorReadCallback(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status);
65 public native static void CALeGattDescriptorWriteCallback(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status);
67 public native static void CALeGattReliableWriteCompletedCallback(BluetoothGatt gatt, int status);
69 public native static void CALeGattReadRemoteRssiCallback(BluetoothGatt gatt, int rssi, int status);
71 // BluetoothGattServerCallback
72 public native static void CALeGattServerConnectionStateChangeCallback(BluetoothDevice device, int status, int newState);
74 public native static void CALeGattServerServiceAddedCallback(int status, BluetoothGattService service);
76 public native static void CALeGattServerCharacteristicReadRequestCallback(BluetoothDevice device,
77 int requestId, int offset, BluetoothGattCharacteristic characteristic);
79 public native static void CALeGattServerCharacteristicWriteRequestCallback(BluetoothDevice device, int requestId,
80 BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value);
82 public native static void CALeGattServerDescriptorReadRequestCallback(BluetoothDevice device,
83 int requestId, int offset, BluetoothGattDescriptor descriptor);
85 public native static void CALeGattServerDescriptorWriteRequestCallback(BluetoothDevice device, int requestId,
86 BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value);
88 public native static void CALeGattServerExecuteWriteCallback(BluetoothDevice device, int requestId, boolean execute);
90 public native static void CALeGattServerNotificationSentCallback(BluetoothDevice device, int status);
93 // public native static void CALeAdvertiseStartSuccessCallback(AdvertiseSettings settingsInEffect);
95 // public native static void CALeAdvertiseStartFailureCallback(int errorCode);
98 // public native static void CABluetoothLeScanResultCallback(int callbackType, ScanResult result);
100 // public native static void CABluetoothLeBatchScanResultsCallback(List<ScanResult> results);
102 // public native static void CABluetoothLeScanFailedCallback(int errorCode);
105 private static BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
108 public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
111 CALeScanCallback(device, rssi, scanRecord);
112 } catch(UnsatisfiedLinkError e) {
118 private static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
121 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
122 super.onConnectionStateChange(gatt, status, newState);
124 CALeGattConnectionStateChangeCallback(gatt, status, newState);
128 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
129 super.onServicesDiscovered(gatt, status);
131 CALeGattServicesDiscoveredCallback(gatt, status);
135 public void onCharacteristicRead(BluetoothGatt gatt,
136 BluetoothGattCharacteristic characteristic, int status) {
137 super.onCharacteristicRead(gatt, characteristic, status);
139 String data = new String(characteristic.getValue());
140 CALeGattCharacteristicReadCallback(gatt, characteristic, data, status);
144 public void onCharacteristicWrite(BluetoothGatt gatt,
145 BluetoothGattCharacteristic characteristic, int status) {
146 super.onCharacteristicWrite(gatt, characteristic, status);
148 String data = new String(characteristic.getValue());
149 CALeGattCharacteristicWriteCallback(gatt, characteristic, data, status);
153 public void onCharacteristicChanged(BluetoothGatt gatt,
154 BluetoothGattCharacteristic characteristic) {
155 super.onCharacteristicChanged(gatt, characteristic);
157 String data = new String(characteristic.getValue());
158 CALeGattCharacteristicChangedCallback(gatt, characteristic, data);
162 public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
164 super.onDescriptorRead(gatt, descriptor, status);
166 CALeGattDescriptorReadCallback(gatt, descriptor, status);
170 public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
172 super.onDescriptorWrite(gatt, descriptor, status);
174 CALeGattDescriptorWriteCallback(gatt, descriptor, status);
178 public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
179 super.onReliableWriteCompleted(gatt, status);
181 CALeGattReliableWriteCompletedCallback(gatt, status);
185 public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
186 super.onReadRemoteRssi(gatt, rssi, status);
188 CALeGattReadRemoteRssiCallback(gatt, rssi, status);
193 private static final BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
196 public void onConnectionStateChange(BluetoothDevice device, int status,
198 super.onConnectionStateChange(device, status, newState);
200 CALeGattServerConnectionStateChangeCallback(device, status, newState);
204 public void onServiceAdded(int status, BluetoothGattService service) {
205 super.onServiceAdded(status, service);
207 CALeGattServerServiceAddedCallback(status, service);
211 public void onCharacteristicReadRequest(BluetoothDevice device,
212 int requestId, int offset,
213 BluetoothGattCharacteristic characteristic) {
214 super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
216 CALeGattServerCharacteristicReadRequestCallback(device, requestId, offset, characteristic);
220 public void onCharacteristicWriteRequest(BluetoothDevice device,
221 int requestId, BluetoothGattCharacteristic characteristic,
222 boolean preparedWrite, boolean responseNeeded, int offset,
224 super.onCharacteristicWriteRequest(device, requestId, characteristic,
225 preparedWrite, responseNeeded, offset, value);
227 CALeGattServerCharacteristicWriteRequestCallback(device, requestId, characteristic,
228 preparedWrite, responseNeeded, offset, value);
232 public void onDescriptorReadRequest(BluetoothDevice device,
233 int requestId, int offset, BluetoothGattDescriptor descriptor) {
234 super.onDescriptorReadRequest(device, requestId, offset, descriptor);
236 CALeGattServerDescriptorReadRequestCallback(device, requestId, offset, descriptor);
240 public void onDescriptorWriteRequest(BluetoothDevice device,
241 int requestId, BluetoothGattDescriptor descriptor,
242 boolean preparedWrite, boolean responseNeeded, int offset,
244 super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite,
245 responseNeeded, offset, value);
247 CALeGattServerDescriptorWriteRequestCallback(device, requestId, descriptor, preparedWrite,
248 responseNeeded, offset, value);
252 public void onExecuteWrite(BluetoothDevice device, int requestId,
254 super.onExecuteWrite(device, requestId, execute);
256 CALeGattServerExecuteWriteCallback(device, requestId, execute);
260 public void onNotificationSent(BluetoothDevice device, int status) {
261 super.onNotificationSent(device, status);
263 CALeGattServerNotificationSentCallback(device, status);
267 private static final AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
270 public void onStartSuccess(AdvertiseSettings settingsInEffect) {
271 super.onStartSuccess(settingsInEffect);
273 CALeAdvertiseStartSuccessCallback(settingsInEffect);
277 public void onStartFailure(int errorCode) {
278 super.onStartFailure(errorCode);
280 CALeAdvertiseStartFailureCallback(errorCode);
284 private static final ScanCallback mScanCallback = new ScanCallback() {
287 public void onScanResult(int callbackType, ScanResult result) {
288 super.onScanResult(callbackType, result);
290 CABluetoothLeScanResultCallback(callbackType, result);
294 public void onBatchScanResults(List<ScanResult> results) {
295 super.onBatchScanResults(results);
297 CABluetoothLeBatchScanResultsCallback(results);
301 public void onScanFailed(int errorCode) {
302 super.onScanFailed(errorCode);
304 CABluetoothLeScanFailedCallback(errorCode);