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";
45 private CaLeClientInterface(Context context) {
47 caLeRegisterLeScanCallback(mLeScanCallback);
48 caLeRegisterGattCallback(mGattCallback);
50 registerIntentFilter(context);
53 public static void getLeScanCallback() {
54 caLeRegisterLeScanCallback(mLeScanCallback);
57 public static void getLeGattCallback() {
58 caLeRegisterGattCallback(mGattCallback);
61 private static IntentFilter registerIntentFilter(Context context) {
62 IntentFilter filter = new IntentFilter();
63 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
64 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
65 context.registerReceiver(mReceiver, filter);
69 private native static void caLeRegisterLeScanCallback(BluetoothAdapter.LeScanCallback callback);
71 private native static void caLeRegisterGattCallback(BluetoothGattCallback callback);
73 // BluetoothAdapter.LeScanCallback
74 private native static void caLeScanCallback(BluetoothDevice device,
75 int rssi, byte[] scanRecord);
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 caLeGattCharacteristicReadCallback(
84 BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
85 byte[] data, int status);
87 private native static void caLeGattCharacteristicWriteCallback(
88 BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
89 byte[] data, int status);
91 private native static void caLeGattCharacteristicChangedCallback(
92 BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] data);
94 private native static void caLeGattDescriptorReadCallback(BluetoothGatt gatt,
95 BluetoothGattDescriptor descriptor,
98 private native static void caLeGattDescriptorWriteCallback(BluetoothGatt gatt,
99 BluetoothGattDescriptor descriptor,
102 private native static void caLeGattReliableWriteCompletedCallback(BluetoothGatt gatt,
105 private native static void caLeGattReadRemoteRssiCallback(BluetoothGatt gatt, int rssi,
109 private native static void caLeStateChangedCallback(int state);
112 private native static void caLeBondStateChangedCallback(String address);
115 private static BluetoothAdapter.LeScanCallback mLeScanCallback =
116 new BluetoothAdapter.LeScanCallback() {
119 public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
122 List<UUID> uuids = getUuids(scanRecord);
123 for (UUID uuid : uuids) {
124 Log.d("CA", "UUID : " + uuid.toString());
125 if(uuid.toString().contains(SERVICE_UUID.toLowerCase())) {
126 Log.d("CA", "we found that has the Device");
127 caLeScanCallback(device, rssi, scanRecord);
130 } catch(UnsatisfiedLinkError e) {
136 private static List<UUID> getUuids(final byte[] scanRecord) {
137 List<UUID> uuids = new ArrayList<UUID>();
140 while (offset < (scanRecord.length - 2)) {
141 int len = scanRecord[offset++];
145 int type = scanRecord[offset++];
151 int uuid16 = scanRecord[offset++];
152 uuid16 += (scanRecord[offset++] << 8);
154 uuids.add(UUID.fromString(String.format(
155 "%08x-0000-1000-8000-00805f9b34fb", uuid16)));
162 ByteBuffer buffer = ByteBuffer.wrap(scanRecord, offset++, 16).
163 order(ByteOrder.LITTLE_ENDIAN);
164 long mostSigBits = buffer.getLong();
165 long leastSigBits = buffer.getLong();
166 uuids.add(new UUID(leastSigBits, mostSigBits));
167 } catch (IndexOutOfBoundsException e) {
168 Log.e("CA", e.toString());
184 private static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
187 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
188 super.onConnectionStateChange(gatt, status, newState);
190 caLeGattConnectionStateChangeCallback(gatt, status, newState);
194 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
195 super.onServicesDiscovered(gatt, status);
197 caLeGattServicesDiscoveredCallback(gatt, status);
201 public void onCharacteristicRead(BluetoothGatt gatt,
202 BluetoothGattCharacteristic characteristic, int status) {
203 super.onCharacteristicRead(gatt, characteristic, status);
205 caLeGattCharacteristicReadCallback(gatt, characteristic,
206 characteristic.getValue(), status);
210 public void onCharacteristicWrite(BluetoothGatt gatt,
211 BluetoothGattCharacteristic characteristic, int status) {
212 super.onCharacteristicWrite(gatt, characteristic, status);
214 caLeGattCharacteristicWriteCallback(gatt, characteristic,
215 characteristic.getValue(), status);
219 public void onCharacteristicChanged(BluetoothGatt gatt,
220 BluetoothGattCharacteristic characteristic) {
221 super.onCharacteristicChanged(gatt, characteristic);
223 caLeGattCharacteristicChangedCallback(gatt, characteristic,
224 characteristic.getValue());
228 public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
230 super.onDescriptorRead(gatt, descriptor, status);
232 caLeGattDescriptorReadCallback(gatt, descriptor, status);
236 public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
238 super.onDescriptorWrite(gatt, descriptor, status);
240 caLeGattDescriptorWriteCallback(gatt, descriptor, status);
244 public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
245 super.onReliableWriteCompleted(gatt, status);
247 caLeGattReliableWriteCompletedCallback(gatt, status);
251 public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
252 super.onReadRemoteRssi(gatt, rssi, status);
254 caLeGattReadRemoteRssiCallback(gatt, rssi, status);
258 private static final BroadcastReceiver mReceiver = new BroadcastReceiver() {
261 public void onReceive(Context context, Intent intent) {
263 String action = intent.getAction();
265 if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
267 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
268 BluetoothAdapter.ERROR);
270 if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
272 caLeStateChangedCallback(state);
276 if (action != null && action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
278 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
279 BluetoothDevice.ERROR);
281 if (bondState == BluetoothDevice.BOND_NONE) {
282 if ((intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
283 BluetoothDevice.ERROR) == BluetoothDevice.BOND_BONDED)) {
284 BluetoothDevice device = intent
285 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
287 caLeBondStateChangedCallback(device.getAddress());