OSDN Git Service

Added ACL connect/disconnect state callbacks
authorKausik Sinnaswamy <kausik@broadcom.com>
Wed, 4 Apr 2012 06:39:10 +0000 (12:09 +0530)
committerAndroid (Google) Code Review <android-gerrit@google.com>
Tue, 17 Jul 2012 05:03:21 +0000 (22:03 -0700)
Change-Id: Ibd44de8601521a11b106711ccad9261fb40beca8

jni/com_android_bluetooth_btservice_AdapterService.cpp
src/com/android/bluetooth/btservice/AbstractionLayer.java
src/com/android/bluetooth/btservice/JniCallbacks.java
src/com/android/bluetooth/btservice/RemoteDevices.java

index a18ab55..0d139c4 100755 (executable)
@@ -26,6 +26,7 @@ static jmethodID method_deviceFoundCallback;
 static jmethodID method_pinRequestCallback;
 static jmethodID method_sspRequestCallback;
 static jmethodID method_bondStateChangeCallback;
+static jmethodID method_aclStateChangeCallback;
 static jmethodID method_discoveryStateChangeCallback;
 
 static const bt_interface_t *sBluetoothInterface = NULL;
@@ -301,6 +302,32 @@ static void bond_state_changed_callback(bt_status_t status, bt_bdaddr_t *bd_addr
     callbackEnv->DeleteLocalRef(addr);
 }
 
+static void acl_state_changed_callback(bt_status_t status, bt_bdaddr_t *bd_addr,
+                                       bt_acl_state_t state)
+{
+    jbyteArray addr;
+    int i;
+    if (!checkCallbackThread()) {
+       LOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__);
+       return;
+    }
+    if (!bd_addr) {
+        LOGE("Address is null in %s", __FUNCTION__);
+        return;
+    }
+    addr = callbackEnv->NewByteArray(sizeof(bt_bdaddr_t));
+    if (addr == NULL) {
+       LOGE("Address allocation failed in %s", __FUNCTION__);
+       return;
+    }
+    callbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte *)bd_addr);
+
+    callbackEnv->CallVoidMethod(sJniCallbacksObj, method_aclStateChangeCallback, (jint) status,
+                                addr, (jint)state);
+    checkAndClearExceptionFromCallback(callbackEnv, __FUNCTION__);
+    callbackEnv->DeleteLocalRef(addr);
+}
+
 static void discovery_state_changed_callback(bt_discovery_state_t state) {
     jbyteArray addr;
     if (!checkCallbackThread()) {
@@ -414,6 +441,7 @@ bt_callbacks_t sBluetoothCallbacks = {
     pin_request_callback,
     ssp_request_callback,
     bond_state_changed_callback,
+    acl_state_changed_callback,
     callback_thread_event,
 };
 
@@ -446,6 +474,8 @@ static void classInitNative(JNIEnv* env, jclass clazz) {
     method_bondStateChangeCallback = env->GetMethodID(jniCallbackClass,
                                                      "bondStateChangeCallback", "(I[BI)V");
 
+    method_aclStateChangeCallback = env->GetMethodID(jniCallbackClass,
+                                                    "aclStateChangeCallback", "(I[BI)V");
     char value[PROPERTY_VALUE_MAX];
     property_get("bluetooth.mock_stack", value, "");
 
index cdd74c7..a726843 100644 (file)
@@ -48,6 +48,9 @@ final public class AbstractionLayer {
     static final int BT_DISCOVERY_STOPPED = 0x00;
     static final int BT_DISCOVERY_STARTED = 0x01;
 
+    static final int BT_ACL_STATE_CONNECTED = 0x00;
+    static final int BT_ACL_STATE_DISCONNECTED = 0x01;
+
     static final int BT_UUID_SIZE = 16; // bytes
 
     public static final int BT_STATUS_SUCCESS = 0;
index d829c19..5d29976 100644 (file)
@@ -60,6 +60,10 @@ final class JniCallbacks {
         mBondStateMachine.bondStateChangeCallback(status, address, newState);
     }
 
+    void aclStateChangeCallback(int status, byte[] address, int newState) {
+               mRemoteDevices.aclStateChangeCallback(status, address, newState);
+    }
+
     void stateChangeCallback(int status) {
         mAdapterStateMachine.stateChangeCallback(status);
     }
index 477977b..df98ee0 100755 (executable)
@@ -378,6 +378,26 @@ final class RemoteDevices {
         mContext.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
     }
 
+    void aclStateChangeCallback(int status, byte[] address, int newState) {
+        BluetoothDevice device = getDevice(address);
+
+        if (device == null) {
+            errorLog("aclStateChangeCallback: Device is NULL");
+            return;
+        }
+
+        Intent intent = null;
+        if (newState == AbstractionLayer.BT_ACL_STATE_CONNECTED) {
+            intent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED);
+            debugLog("aclStateChangeCallback: State:Connected to Device:" + device);
+        } else {
+            intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
+            debugLog("aclStateChangeCallback: State:DisConnected to Device:" + device);
+        }
+        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
+        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
+        mContext.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
+    }
 
     void fetchUuids(BluetoothDevice device) {
         if (mSdpTracker.contains(device)) return;