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.content.BroadcastReceiver;
36 import android.content.Context;
37 import android.content.Intent;
38 import android.content.IntentFilter;
39 import android.util.Log;
41 public class CaLeClientInterface {
43 private static String SERVICE_UUID = "ADE3D529-C784-4F63-A987-EB69F70EE816";
44 private static String TAG = "Sample_Service : CaLeClientInterface";
46 private CaLeClientInterface(Context context) {
48 caLeRegisterLeScanCallback(mLeScanCallback);
49 caLeRegisterGattCallback(mGattCallback);
51 registerIntentFilter(context);
54 public static void getLeScanCallback() {
55 caLeRegisterLeScanCallback(mLeScanCallback);
58 public static void getLeGattCallback() {
59 caLeRegisterGattCallback(mGattCallback);
62 private static IntentFilter registerIntentFilter(Context context) {
63 IntentFilter filter = new IntentFilter();
64 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
65 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
66 context.registerReceiver(mReceiver, filter);
70 private native static void caLeRegisterLeScanCallback(BluetoothAdapter.LeScanCallback callback);
72 private native static void caLeRegisterGattCallback(BluetoothGattCallback callback);
74 // BluetoothAdapter.LeScanCallback
75 private native static void caLeScanCallback(BluetoothDevice device);
77 // BluetoothGattCallback
78 private native static void caLeGattConnectionStateChangeCallback(
79 BluetoothGatt gatt, int status, int newState);
81 private native static void caLeGattServicesDiscoveredCallback(BluetoothGatt gatt, int status);
83 private native static void caLeGattCharacteristicWriteCallback(
84 BluetoothGatt gatt, byte[] data, int status);
86 private native static void caLeGattCharacteristicChangedCallback(
87 BluetoothGatt gatt, byte[] data);
89 private native static void caLeGattDescriptorWriteCallback(BluetoothGatt gatt, int status);
91 private native static void caLeGattReliableWriteCompletedCallback(BluetoothGatt gatt,
94 private native static void caLeGattReadRemoteRssiCallback(BluetoothGatt gatt, int rssi,
98 private native static void caLeStateChangedCallback(int state);
101 private native static void caLeBondStateChangedCallback(String address);
104 private static BluetoothAdapter.LeScanCallback mLeScanCallback =
105 new BluetoothAdapter.LeScanCallback() {
108 public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
111 List<UUID> uuids = getUuids(scanRecord);
112 for (UUID uuid : uuids) {
113 Log.d(TAG, "UUID : " + uuid.toString());
114 if(uuid.toString().contains(SERVICE_UUID.toLowerCase())) {
115 Log.d(TAG, "we found that has the Device");
116 caLeScanCallback(device);
119 } catch(UnsatisfiedLinkError e) {
125 private static List<UUID> getUuids(final byte[] scanRecord) {
126 List<UUID> uuids = new ArrayList<UUID>();
129 while (offset < (scanRecord.length - 2)) {
130 int len = scanRecord[offset++];
134 int type = scanRecord[offset++];
140 int uuid16 = scanRecord[offset++];
141 uuid16 += (scanRecord[offset++] << 8);
143 uuids.add(UUID.fromString(String.format(
144 "%08x-0000-1000-8000-00805f9b34fb", uuid16)));
151 ByteBuffer buffer = ByteBuffer.wrap(scanRecord, offset++, 16).
152 order(ByteOrder.LITTLE_ENDIAN);
153 long mostSigBits = buffer.getLong();
154 long leastSigBits = buffer.getLong();
155 uuids.add(new UUID(leastSigBits, mostSigBits));
156 } catch (IndexOutOfBoundsException e) {
157 Log.e(TAG, e.toString());
173 private static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
176 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
177 super.onConnectionStateChange(gatt, status, newState);
179 caLeGattConnectionStateChangeCallback(gatt, status, newState);
183 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
184 super.onServicesDiscovered(gatt, status);
186 caLeGattServicesDiscoveredCallback(gatt, status);
190 public void onCharacteristicRead(BluetoothGatt gatt,
191 BluetoothGattCharacteristic characteristic, int status) {
192 super.onCharacteristicRead(gatt, characteristic, status);
196 public void onCharacteristicWrite(BluetoothGatt gatt,
197 BluetoothGattCharacteristic characteristic, int status) {
198 super.onCharacteristicWrite(gatt, characteristic, status);
200 caLeGattCharacteristicWriteCallback(gatt, characteristic.getValue(), status);
204 public void onCharacteristicChanged(BluetoothGatt gatt,
205 BluetoothGattCharacteristic characteristic) {
206 super.onCharacteristicChanged(gatt, characteristic);
208 caLeGattCharacteristicChangedCallback(gatt, characteristic.getValue());
212 public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
214 super.onDescriptorRead(gatt, descriptor, status);
218 public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
220 super.onDescriptorWrite(gatt, descriptor, status);
222 caLeGattDescriptorWriteCallback(gatt, status);
226 public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
227 super.onReliableWriteCompleted(gatt, status);
231 public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
232 super.onReadRemoteRssi(gatt, rssi, status);
236 private static final BroadcastReceiver mReceiver = new BroadcastReceiver() {
239 public void onReceive(Context context, Intent intent) {
241 String action = intent.getAction();
243 if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
245 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
246 BluetoothAdapter.ERROR);
248 if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
250 caLeStateChangedCallback(state);
254 if (action != null && action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
256 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
257 BluetoothDevice.ERROR);
259 if (bondState == BluetoothDevice.BOND_NONE) {
260 if ((intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
261 BluetoothDevice.ERROR) == BluetoothDevice.BOND_BONDED)) {
262 BluetoothDevice device = intent
263 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
265 caLeBondStateChangedCallback(device.getAddress());