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,
76 int rssi, byte[] scanRecord);
78 // BluetoothGattCallback
79 private native static void caLeGattConnectionStateChangeCallback(
80 BluetoothGatt gatt, int status, int newState);
82 private native static void caLeGattServicesDiscoveredCallback(BluetoothGatt gatt, int status);
84 private native static void caLeGattCharacteristicReadCallback(
85 BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
86 byte[] data, int status);
88 private native static void caLeGattCharacteristicWriteCallback(
89 BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
90 byte[] data, int status);
92 private native static void caLeGattCharacteristicChangedCallback(
93 BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] data);
95 private native static void caLeGattDescriptorReadCallback(BluetoothGatt gatt,
96 BluetoothGattDescriptor descriptor,
99 private native static void caLeGattDescriptorWriteCallback(BluetoothGatt gatt,
100 BluetoothGattDescriptor descriptor,
103 private native static void caLeGattReliableWriteCompletedCallback(BluetoothGatt gatt,
106 private native static void caLeGattReadRemoteRssiCallback(BluetoothGatt gatt, int rssi,
110 private native static void caLeStateChangedCallback(int state);
113 private native static void caLeBondStateChangedCallback(String address);
116 private static BluetoothAdapter.LeScanCallback mLeScanCallback =
117 new BluetoothAdapter.LeScanCallback() {
120 public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
123 List<UUID> uuids = getUuids(scanRecord);
124 for (UUID uuid : uuids) {
125 Log.d(TAG, "UUID : " + uuid.toString());
126 if(uuid.toString().contains(SERVICE_UUID.toLowerCase())) {
127 Log.d(TAG, "we found that has the Device");
128 caLeScanCallback(device, rssi, scanRecord);
131 } catch(UnsatisfiedLinkError e) {
137 private static List<UUID> getUuids(final byte[] scanRecord) {
138 List<UUID> uuids = new ArrayList<UUID>();
141 while (offset < (scanRecord.length - 2)) {
142 int len = scanRecord[offset++];
146 int type = scanRecord[offset++];
152 int uuid16 = scanRecord[offset++];
153 uuid16 += (scanRecord[offset++] << 8);
155 uuids.add(UUID.fromString(String.format(
156 "%08x-0000-1000-8000-00805f9b34fb", uuid16)));
163 ByteBuffer buffer = ByteBuffer.wrap(scanRecord, offset++, 16).
164 order(ByteOrder.LITTLE_ENDIAN);
165 long mostSigBits = buffer.getLong();
166 long leastSigBits = buffer.getLong();
167 uuids.add(new UUID(leastSigBits, mostSigBits));
168 } catch (IndexOutOfBoundsException e) {
169 Log.e(TAG, e.toString());
185 private static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
188 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
189 super.onConnectionStateChange(gatt, status, newState);
191 caLeGattConnectionStateChangeCallback(gatt, status, newState);
195 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
196 super.onServicesDiscovered(gatt, status);
198 caLeGattServicesDiscoveredCallback(gatt, status);
202 public void onCharacteristicRead(BluetoothGatt gatt,
203 BluetoothGattCharacteristic characteristic, int status) {
204 super.onCharacteristicRead(gatt, characteristic, status);
206 caLeGattCharacteristicReadCallback(gatt, characteristic,
207 characteristic.getValue(), status);
211 public void onCharacteristicWrite(BluetoothGatt gatt,
212 BluetoothGattCharacteristic characteristic, int status) {
213 super.onCharacteristicWrite(gatt, characteristic, status);
215 caLeGattCharacteristicWriteCallback(gatt, characteristic,
216 characteristic.getValue(), status);
220 public void onCharacteristicChanged(BluetoothGatt gatt,
221 BluetoothGattCharacteristic characteristic) {
222 super.onCharacteristicChanged(gatt, characteristic);
224 caLeGattCharacteristicChangedCallback(gatt, characteristic,
225 characteristic.getValue());
229 public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
231 super.onDescriptorRead(gatt, descriptor, status);
233 caLeGattDescriptorReadCallback(gatt, descriptor, status);
237 public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
239 super.onDescriptorWrite(gatt, descriptor, status);
241 caLeGattDescriptorWriteCallback(gatt, descriptor, status);
245 public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
246 super.onReliableWriteCompleted(gatt, status);
248 caLeGattReliableWriteCompletedCallback(gatt, status);
252 public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
253 super.onReadRemoteRssi(gatt, rssi, status);
255 caLeGattReadRemoteRssiCallback(gatt, rssi, status);
259 private static final BroadcastReceiver mReceiver = new BroadcastReceiver() {
262 public void onReceive(Context context, Intent intent) {
264 String action = intent.getAction();
266 if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
268 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
269 BluetoothAdapter.ERROR);
271 if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
273 caLeStateChangedCallback(state);
277 if (action != null && action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
279 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
280 BluetoothDevice.ERROR);
282 if (bondState == BluetoothDevice.BOND_NONE) {
283 if ((intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
284 BluetoothDevice.ERROR) == BluetoothDevice.BOND_BONDED)) {
285 BluetoothDevice device = intent
286 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
288 caLeBondStateChangedCallback(device.getAddress());