Fix bug, local BT client name

The local BT name was set to the remote server as well by mistake
This commit is contained in:
Arik Caspi 2020-01-12 23:58:35 +02:00
parent 89731d5def
commit 7187d1f82a
1 changed files with 20 additions and 9 deletions

View File

@ -30,8 +30,8 @@ static void scanCompleteCB(BLEScanResults scanResults)
{ {
scanResults.dump(); scanResults.dump();
if (doScan) if (doScan)
{
pBLEScan->start(10, scanCompleteCB); pBLEScan->start(10, scanCompleteCB);
{
} }
} }
@ -54,12 +54,13 @@ public:
void * p = heap_caps_malloc(size, MALLOC_CAP_SPIRAM); void * p = heap_caps_malloc(size, MALLOC_CAP_SPIRAM);
return p; return p;
} }
// callbacks // callbacks
void(*_connectedCallback)() = NULL; void(*_connectedCallback)() = NULL;
void(*_disconnectedCallback)() = NULL; void(*_disconnectedCallback)() = NULL;
char _deviceName[32]; char _deviceName[32];
bool _doConnect; bool _doConnect;
BLEAdvertisedDevice * _advertising; BLEAdvertisedDevice * _advertising = NULL;
BLERemoteService* pRemoteService; BLERemoteService* pRemoteService;
BLERemoteCharacteristic* pRemoteCharacteristic; BLERemoteCharacteristic* pRemoteCharacteristic;
bool _connected; bool _connected;
@ -157,7 +158,8 @@ protected:
}; };
bool connectToServer() bool connectToServer()
{ {
const uint8_t bothOn[] = {0x3, 0x0};
// Connect to the remote BLE Server. // Connect to the remote BLE Server.
pClient->connect(_advertising); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private) pClient->connect(_advertising); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
@ -176,7 +178,8 @@ protected:
pClient->disconnect(); pClient->disconnect();
return false; return false;
} }
pRemoteCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)bothOn, 2, true);
return true; return true;
}; };
@ -192,7 +195,7 @@ public:
// TODO why must these functions be inline?? // TODO why must these functions be inline??
inline bool begin(const char* deviceName); inline bool begin(const char* thisDeviceName, const char *remoteDeviceName);
inline void read() inline void read()
{ {
@ -311,10 +314,18 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
return; return;
} }
// We have found a device, let us now see if it contains the service we are looking for. // We have found a device, let us now see if it contains the service we are looking for.
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(BLEUUID(SERVICE_UUID))) if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(BLEUUID(SERVICE_UUID)))
{ {
BLEDevice::getScan()->stop(); BLEDevice::getScan()->stop();
if (_BleMidiInterfaceClient->_advertising != NULL)
{
delete(_BleMidiInterfaceClient->_advertising);
_BleMidiInterfaceClient->_advertising = NULL;
}
_BleMidiInterfaceClient->_advertising = new BLEAdvertisedDevice(advertisedDevice); _BleMidiInterfaceClient->_advertising = new BLEAdvertisedDevice(advertisedDevice);
_BleMidiInterfaceClient->_doConnect = true; _BleMidiInterfaceClient->_doConnect = true;
doScan = false; doScan = false;
@ -324,14 +335,14 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
protected: protected:
BleMidiInterfaceClient* _BleMidiInterfaceClient; BleMidiInterfaceClient* _BleMidiInterfaceClient;
}; // MyAdvertisedDeviceCallbacks }; // MyAdvertisedDeviceCallbacks
bool BleMidiInterfaceClient::begin(const char* deviceName) bool BleMidiInterfaceClient::begin(const char* thisDeviceName, const char *remoteDeviceName)
{ {
BLEDevice::init(deviceName); BLEDevice::init(thisDeviceName);
strncpy(_deviceName, deviceName, 32); strncpy(_deviceName, remoteDeviceName, 32);
pClient = BLEDevice::createClient(); pClient = BLEDevice::createClient();
pClient->setClientCallbacks(new MyClientCallbacks(this)); pClient->setClientCallbacks(new MyClientCallbacks(this));