From 39bf745e92b79891c43b42de662fb41c42b3b30e Mon Sep 17 00:00:00 2001 From: Andrei Emeltchenko Date: Fri, 25 Oct 2013 14:34:46 +0300 Subject: [PATCH] android/hal: Associate thread to Java Without thread assiciation callbacks are not received by Java. They are blocked by JNI: E/BluetoothServiceJni( 2844): Callback env check fail: env: 0x0, callback: 0x0 E/BluetoothServiceJni( 2844): Callback: 'adapter_state_change_callback' is not called on the correct thread --- android/hal-bluetooth.c | 12 ++++++++++++ android/hal-ipc.c | 4 ++++ android/hal.h | 2 ++ 3 files changed, 18 insertions(+) diff --git a/android/hal-bluetooth.c b/android/hal-bluetooth.c index 9c7fbeae8..894f5b5cf 100644 --- a/android/hal-bluetooth.c +++ b/android/hal-bluetooth.c @@ -66,6 +66,18 @@ static void handle_adapter_props_changed(void *buf, uint16_t len) bt_hal_cbacks->adapter_properties_cb(ev->status, ev->num_props, props); } +void bt_thread_associate(void) +{ + if (bt_hal_cbacks->thread_evt_cb) + bt_hal_cbacks->thread_evt_cb(ASSOCIATE_JVM); +} + +void bt_thread_disassociate(void) +{ + if (bt_hal_cbacks->thread_evt_cb) + bt_hal_cbacks->thread_evt_cb(DISASSOCIATE_JVM); +} + /* will be called from notification thread context */ void bt_notify_adapter(uint16_t opcode, void *buf, uint16_t len) { diff --git a/android/hal-ipc.c b/android/hal-ipc.c index c7764aba4..57238410d 100644 --- a/android/hal-ipc.c +++ b/android/hal-ipc.c @@ -67,6 +67,8 @@ static void *notification_handler(void *data) ssize_t ret; int fd; + bt_thread_associate(); + while (true) { memset(&msg, 0, sizeof(msg)); memset(buf, 0, sizeof(buf)); @@ -133,6 +135,8 @@ static void *notification_handler(void *data) close(notif_sk); notif_sk = -1; + bt_thread_disassociate(); + DBG("exit"); return NULL; diff --git a/android/hal.h b/android/hal.h index ef9a10730..5cd5cabf7 100644 --- a/android/hal.h +++ b/android/hal.h @@ -27,3 +27,5 @@ btpan_interface_t *bt_get_pan_interface(void); btav_interface_t *bt_get_av_interface(void); void bt_notify_adapter(uint16_t opcode, void *buf, uint16_t len); +void bt_thread_associate(void); +void bt_thread_disassociate(void); -- 2.11.0