diff --git a/able/android/jni.py b/able/android/jni.py index fb9c80f..bac7c50 100644 --- a/able/android/jni.py +++ b/able/android/jni.py @@ -34,7 +34,6 @@ def on_scan_result(self, result): device ) - @java_method('(Z)V') def on_scan_started(self, success): Logger.debug("on_scan_started") @@ -51,6 +50,11 @@ def on_connection_state_change(self, status, state): status, state)) self.dispatcher.dispatch('on_connection_state_change', status, state) + @java_method('(I)V') + def on_bluetooth_adapter_state_change(self, state): + Logger.debug("on_bluetooth_adapter_state_change state: {}".format(state)) + self.dispatcher.dispatch('on_bluetooth_adapter_state_change', state) + @java_method('(ILjava/util/List;)V') def on_services(self, status, services): services_dict = Services() diff --git a/able/dispatcher.py b/able/dispatcher.py index 738aac2..fe8a35d 100644 --- a/able/dispatcher.py +++ b/able/dispatcher.py @@ -29,6 +29,7 @@ class BluetoothDispatcherBase(EventDispatcher): "on_scan_completed", "on_services", "on_connection_state_change", + "on_bluetooth_adapter_state_change", "on_characteristic_changed", "on_characteristic_read", "on_characteristic_write", @@ -279,6 +280,13 @@ def on_connection_state_change(self, status, state): """ pass + def on_bluetooth_adapter_state_change(self, state): + """`bluetooth_adapter_state_change` event handler + Allows the user to detect when bluetooth adapter is turned on/off. + + :param state: STATE_OFF, STATE_TURNING_OFF, STATE_ON, STATE_TURNING_ON + """ + def on_services(self, services, status): """`services` event handler diff --git a/able/src/org/able/BLE.java b/able/src/org/able/BLE.java index 7aac35c..80d3414 100644 --- a/able/src/org/able/BLE.java +++ b/able/src/org/able/BLE.java @@ -2,6 +2,8 @@ import android.app.Activity; import android.content.Intent; +import android.content.IntentFilter; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.pm.PackageManager; import android.bluetooth.BluetoothAdapter; @@ -66,6 +68,7 @@ public BLE(PythonBluetooth python) { final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); + mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); } public BluetoothAdapter getAdapter(int EnableBtCode) { @@ -171,6 +174,18 @@ public void closeGatt() { } } + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { + Log.d(TAG, "onReceive - BluetoothAdapter state changed"); + int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); + mPython.on_bluetooth_adapter_state_change(state); + } + } + }; + private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override diff --git a/able/src/org/able/PythonBluetooth.java b/able/src/org/able/PythonBluetooth.java index fd20f5b..8179ca2 100644 --- a/able/src/org/able/PythonBluetooth.java +++ b/able/src/org/able/PythonBluetooth.java @@ -19,6 +19,7 @@ interface PythonBluetooth public void on_descriptor_read(BluetoothGattDescriptor descriptor, int status); public void on_descriptor_write(BluetoothGattDescriptor descriptor, int status); public void on_connection_state_change(int status, int state); + public void on_bluetooth_adapter_state_change(int state); public void on_rssi_updated(int rssi, int status); public void on_mtu_changed (int mtu, int status); }