Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added event handler for bluetooth adapter state change. #39

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion able/android/jni.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions able/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions able/src/org/able/BLE.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions able/src/org/able/PythonBluetooth.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}