Skip to content

Commit 17cc552

Browse files
committed
fixing android ble
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
1 parent 1888ed5 commit 17cc552

11 files changed

Lines changed: 553 additions & 100 deletions

File tree

src/board_controller/muse/muse.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ int Muse::prepare_session ()
162162
res = (int)BrainFlowExitCodes::BOARD_NOT_READY_ERROR;
163163
}
164164
simpleble_adapter_scan_stop (muse_adapter);
165+
simpleble_adapter_set_callback_on_scan_found (muse_adapter, NULL, NULL);
165166
if (res == (int)BrainFlowExitCodes::STATUS_OK)
166167
{
167168
// for safety

src/board_controller/muse/muse_athena.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "muse_options.h"
1414
#include "timestamp.h"
1515

16-
1716
MuseAthena::SensorConfig::SensorConfig ()
1817
: type (SensorType::UNKNOWN)
1918
, n_channels (0)
@@ -234,6 +233,7 @@ int MuseAthena::prepare_session ()
234233
res = (int)BrainFlowExitCodes::BOARD_NOT_READY_ERROR;
235234
}
236235
simpleble_adapter_scan_stop (muse_adapter);
236+
simpleble_adapter_set_callback_on_scan_found (muse_adapter, NULL, NULL);
237237
if (res == (int)BrainFlowExitCodes::STATUS_OK)
238238
{
239239
// for safety
@@ -570,7 +570,9 @@ void MuseAthena::adapter_on_scan_found (
570570
}
571571
else
572572
{
573-
if (strncmp (peripheral_identified, "MuseS", 5) == 0)
573+
if ((strncmp (peripheral_identified, "MuseS", 5) == 0) ||
574+
(strncmp (peripheral_identified, "MuseAthena", 10) == 0) ||
575+
(strncmp (peripheral_identified, "Muse Athena", 11) == 0))
574576
{
575577
found = true;
576578
}

third_party/SimpleBLE/brainflow.patch

Lines changed: 374 additions & 0 deletions
Large diffs are not rendered by default.

third_party/SimpleBLE/simpleble/src/backends/android/AdapterAndroid.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ bool AdapterAndroid::bluetooth_enabled() { return BackendAndroid::get()->bluetoo
1818

1919
AdapterAndroid::AdapterAndroid() {
2020
_btScanCallback.set_callback_onScanResult([this](Android::ScanResult scan_result) {
21+
if (!this->scanning_) {
22+
return;
23+
}
24+
2125
std::string address = scan_result.getDevice().getAddress();
2226

2327
if (this->peripherals_.count(address) == 0) {
@@ -68,14 +72,14 @@ bool AdapterAndroid::is_powered() { return _btAdapter.isEnabled(); }
6872

6973
void AdapterAndroid::scan_start() {
7074
seen_peripherals_.clear();
71-
_btScanner.startScan(_btScanCallback);
7275
scanning_ = true;
76+
_btScanner.startScan(_btScanCallback);
7377
SAFE_CALLBACK_CALL(this->_callback_on_scan_start);
7478
}
7579

7680
void AdapterAndroid::scan_stop() {
77-
_btScanner.stopScan(_btScanCallback);
7881
scanning_ = false;
82+
_btScanner.stopScan(_btScanCallback);
7983
SAFE_CALLBACK_CALL(this->_callback_on_scan_stop);
8084
}
8185

third_party/SimpleBLE/simpleble/src/backends/android/PeripheralAndroid.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include "DescriptorBase.h"
77
#include "ServiceBase.h"
88

9-
#include <simpleble/Exceptions.h>
109
#include <simpleble/Config.h>
10+
#include <simpleble/Exceptions.h>
1111
#include <algorithm>
1212
#include "CommonUtils.h"
1313
#include "LoggingInternal.h"
@@ -16,15 +16,21 @@
1616
using namespace SimpleBLE;
1717
using namespace std::chrono_literals;
1818

19+
namespace {
20+
constexpr int ANDROID_REQUESTED_MTU = 517;
21+
}
22+
1923
PeripheralAndroid::PeripheralAndroid(Android::BluetoothDevice device) : _device(device) {
2024
_btGattCallback.set_callback_onConnectionStateChange([this](bool connected) {
2125
if (connected) {
2226
if (Config::Android::connection_priority_request != Config::Android::ConnectionPriorityRequest::DISABLED) {
2327
_gatt.requestConnectionPriority(static_cast<int>(Config::Android::connection_priority_request));
2428
}
2529

26-
// If a connection has been established, request service discovery.
27-
_gatt.discoverServices();
30+
if (!_gatt.requestMtu(ANDROID_REQUESTED_MTU)) {
31+
SIMPLEBLE_LOG_WARN("Failed to request Android GATT MTU");
32+
_gatt.discoverServices();
33+
}
2834
} else {
2935
// If a connection has been lost, close the GATT object.
3036
_gatt.close();
@@ -33,6 +39,8 @@ PeripheralAndroid::PeripheralAndroid(Android::BluetoothDevice device) : _device(
3339
}
3440
});
3541

42+
_btGattCallback.set_callback_onMtuChanged([this]() { _gatt.discoverServices(); });
43+
3644
_btGattCallback.set_callback_onServicesDiscovered([this]() {
3745
// Once services have been discovered, store them and notify the user.
3846
_services = _gatt.getServices();

third_party/SimpleBLE/simpleble/src/backends/android/bridge/BluetoothGattCallback.cpp

Lines changed: 83 additions & 42 deletions
Large diffs are not rendered by default.

third_party/SimpleBLE/simpleble/src/backends/android/bridge/BluetoothGattCallback.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BluetoothGattCallback {
2121

2222
void set_callback_onConnectionStateChange(std::function<void(bool)> callback);
2323
void set_callback_onServicesDiscovered(std::function<void(void)> callback);
24+
void set_callback_onMtuChanged(std::function<void(void)> callback);
2425

2526
void set_callback_onCharacteristicChanged(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> characteristic,
2627
std::function<void(std::vector<uint8_t> value)> callback);
@@ -31,15 +32,18 @@ class BluetoothGattCallback {
3132
void wait_flag_characteristicWritePending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> characteristic);
3233

3334
void set_flag_characteristicReadPending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> characteristic);
34-
void clear_flag_characteristicReadPending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> characteristic, std::vector<uint8_t> value);
35-
std::vector<uint8_t> wait_flag_characteristicReadPending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> characteristic);
35+
void clear_flag_characteristicReadPending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> characteristic,
36+
std::vector<uint8_t> value);
37+
std::vector<uint8_t> wait_flag_characteristicReadPending(
38+
SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> characteristic);
3639

3740
void set_flag_descriptorWritePending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> descriptor);
3841
void clear_flag_descriptorWritePending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> descriptor);
3942
void wait_flag_descriptorWritePending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> descriptor);
4043

4144
void set_flag_descriptorReadPending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> descriptor);
42-
void clear_flag_descriptorReadPending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> descriptor, std::vector<uint8_t> value);
45+
void clear_flag_descriptorReadPending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> descriptor,
46+
std::vector<uint8_t> value);
4347
std::vector<uint8_t> wait_flag_descriptorReadPending(SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> descriptor);
4448

4549
bool connected;
@@ -77,20 +81,32 @@ class BluetoothGattCallback {
7781
static SimpleJNI::GlobalRef<jclass> _cls;
7882
static jmethodID _constructor;
7983

80-
static kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, BluetoothGattCallback*, SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>> _map;
84+
static kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, BluetoothGattCallback*,
85+
SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>>
86+
_map;
8187

8288
SimpleJNI::Object<SimpleJNI::GlobalRef, jobject> _obj;
8389

8490
kvn::safe_callback<void(bool)> _callback_onConnectionStateChange;
8591
kvn::safe_callback<void()> _callback_onServicesDiscovered;
92+
kvn::safe_callback<void()> _callback_onMtuChanged;
8693

87-
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, kvn::safe_callback<void(std::vector<uint8_t>)>, SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>>
94+
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, kvn::safe_callback<void(std::vector<uint8_t>)>,
95+
SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>>
8896
_callback_onCharacteristicChanged;
8997

90-
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, FlagData, SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>> _flag_characteristicWritePending;
91-
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, FlagData, SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>> _flag_characteristicReadPending;
92-
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, FlagData, SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>> _flag_descriptorWritePending;
93-
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, FlagData, SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>> _flag_descriptorReadPending;
98+
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, FlagData,
99+
SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>>
100+
_flag_characteristicWritePending;
101+
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, FlagData,
102+
SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>>
103+
_flag_characteristicReadPending;
104+
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, FlagData,
105+
SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>>
106+
_flag_descriptorWritePending;
107+
kvn::safe_map<SimpleJNI::Object<SimpleJNI::GlobalRef, jobject>, FlagData,
108+
SimpleJNI::ObjectComparator<SimpleJNI::GlobalRef, jobject>>
109+
_flag_descriptorReadPending;
94110

95111
// Static JNI resources managed by Registrar
96112
static const SimpleJNI::JNIDescriptor descriptor;
@@ -99,4 +115,4 @@ class BluetoothGattCallback {
99115

100116
} // namespace Bridge
101117
} // namespace Android
102-
} // namespace SimpleBLE
118+
} // namespace SimpleBLE

third_party/SimpleBLE/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@ jmethodID BluetoothGatt::_method_setCharacteristicNotification = nullptr;
1919
jmethodID BluetoothGatt::_method_writeCharacteristic = nullptr;
2020
jmethodID BluetoothGatt::_method_writeDescriptor = nullptr;
2121
jmethodID BluetoothGatt::_method_requestConnectionPriority = nullptr;
22+
jmethodID BluetoothGatt::_method_requestMtu = nullptr;
2223
// Define the JNI descriptor
2324
const SimpleJNI::JNIDescriptor BluetoothGatt::descriptor{
24-
"android/bluetooth/BluetoothGatt", // Java class name
25-
&_cls, // Where to store the jclass
26-
{ // Methods to preload
27-
{"close", "()V", &_method_close},
28-
{"connect", "()Z", &_method_connect},
29-
{"disconnect", "()V", &_method_disconnect},
30-
{"discoverServices", "()Z", &_method_discoverServices},
31-
{"getServices", "()Ljava/util/List;", &_method_getServices},
32-
{"readCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z", &_method_readCharacteristic},
33-
{"readDescriptor", "(Landroid/bluetooth/BluetoothGattDescriptor;)Z", &_method_readDescriptor},
34-
{"setCharacteristicNotification", "(Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z", &_method_setCharacteristicNotification},
35-
{"writeCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z", &_method_writeCharacteristic},
36-
{"writeDescriptor", "(Landroid/bluetooth/BluetoothGattDescriptor;)Z", &_method_writeDescriptor},
37-
{"requestConnectionPriority", "(I)Z", &_method_requestConnectionPriority}
38-
}};
25+
"android/bluetooth/BluetoothGatt", // Java class name
26+
&_cls, // Where to store the jclass
27+
{ // Methods to preload
28+
{"close", "()V", &_method_close},
29+
{"connect", "()Z", &_method_connect},
30+
{"disconnect", "()V", &_method_disconnect},
31+
{"discoverServices", "()Z", &_method_discoverServices},
32+
{"getServices", "()Ljava/util/List;", &_method_getServices},
33+
{"readCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z", &_method_readCharacteristic},
34+
{"readDescriptor", "(Landroid/bluetooth/BluetoothGattDescriptor;)Z", &_method_readDescriptor},
35+
{"setCharacteristicNotification", "(Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z",
36+
&_method_setCharacteristicNotification},
37+
{"writeCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z", &_method_writeCharacteristic},
38+
{"writeDescriptor", "(Landroid/bluetooth/BluetoothGattDescriptor;)Z", &_method_writeDescriptor},
39+
{"requestConnectionPriority", "(I)Z", &_method_requestConnectionPriority},
40+
{"requestMtu", "(I)Z", &_method_requestMtu}}};
3941

4042
const SimpleJNI::AutoRegister<BluetoothGatt> BluetoothGatt::registrar{&descriptor};
4143

@@ -111,5 +113,10 @@ bool BluetoothGatt::requestConnectionPriority(int connectionPriority) {
111113
return _obj.call_boolean_method(_method_requestConnectionPriority, connectionPriority);
112114
}
113115

116+
bool BluetoothGatt::requestMtu(int mtu) {
117+
if (!_obj) throw std::runtime_error("BluetoothGatt is not initialized");
118+
return _obj.call_boolean_method(_method_requestMtu, mtu);
119+
}
120+
114121
} // namespace Android
115122
} // namespace SimpleBLE

third_party/SimpleBLE/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Android {
1212

1313
class BluetoothGatt {
1414
public:
15-
1615
static constexpr int CONNECTION_PRIORITY_BALANCED = 0;
1716
static constexpr int CONNECTION_PRIORITY_HIGH = 1;
1817
static constexpr int CONNECTION_PRIORITY_LOW_POWER = 2;
@@ -42,7 +41,7 @@ class BluetoothGatt {
4241
// void readPhy();
4342
// bool readRemoteRssi();
4443
bool requestConnectionPriority(int connectionPriority);
45-
// bool requestMtu(int mtu);
44+
bool requestMtu(int mtu);
4645
bool setCharacteristicNotification(BluetoothGattCharacteristic characteristic, bool enable);
4746
// void setPreferredPhy(int txPhy, int rxPhy, int phyOptions);
4847
bool writeCharacteristic(BluetoothGattCharacteristic characteristic);
@@ -66,6 +65,7 @@ class BluetoothGatt {
6665
static jmethodID _method_writeCharacteristic;
6766
static jmethodID _method_writeDescriptor;
6867
static jmethodID _method_requestConnectionPriority;
68+
static jmethodID _method_requestMtu;
6969
// JNI descriptor for auto-registration
7070
static const SimpleJNI::JNIDescriptor descriptor;
7171
static const SimpleJNI::AutoRegister<BluetoothGatt> registrar;

third_party/SimpleBLE/simplecble/src/adapter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,16 @@ simpleble_err_t simpleble_adapter_set_callback_on_scan_updated(
372372

373373
simpleble_err_t simpleble_adapter_set_callback_on_scan_found(
374374
simpleble_adapter_t handle, void (*callback)(simpleble_adapter_t, simpleble_peripheral_t, void*), void* userdata) {
375-
if (handle == nullptr || callback == nullptr) {
375+
if (handle == nullptr) {
376376
return SIMPLEBLE_FAILURE;
377377
}
378378

379379
SimpleBLE::Adapter* adapter = (SimpleBLE::Adapter*)handle;
380380
try {
381+
if (callback == nullptr) {
382+
adapter->set_callback_on_scan_found(nullptr);
383+
return SIMPLEBLE_SUCCESS;
384+
}
381385
adapter->set_callback_on_scan_found([=](SimpleBLE::Peripheral peripheral) {
382386
SimpleBLE::Peripheral* peripheral_handle = new SimpleBLE::Peripheral(peripheral);
383387
callback(handle, (simpleble_peripheral_t)peripheral_handle, userdata);
@@ -386,4 +390,4 @@ simpleble_err_t simpleble_adapter_set_callback_on_scan_found(
386390
} catch (...) {
387391
return SIMPLEBLE_FAILURE;
388392
}
389-
}
393+
}

0 commit comments

Comments
 (0)