I'm new in Android BLE so my question may be uncorrect or naive in some way. If this is the case please explain me where I'm wrong and kindly show me the correct way to manage this scenario.
Scenario is the following: my Android app communicates with a BLE device sending commands and getting answers from device using BLE characteristics.
Sequence is:
- Device wakes up the app (the
onConnectionStateChange
method is called) - My app writes a command in a characteristic (I call
writeCharacteristic
putting the command invalue
parameter). - Device sends back the answer to command to my app (the
onCharacteristicChanged
method is triggered andvalue
parameter contains the answer)
After waking up the app, the device doesn't do anything until a command is sent via writeCharacteristic
. The device accepts different commands.
All good so far, but recently I developed a second different app to communicate with same device.
When I run both apps on same Android phone, one app sends a command to the device and the response is received by both apps! Of course the app that didn't sent the command receives an unexpected answer and goes to an unexpected status.
Ok, knowing the problem I can modify both my apps to handle this situation, but the question is: Is this behavior normal when two apps in same device communicate with same BLE device?
Is there a way for an app to establish a communication channel with a BLE device to avoid sending answer to specific commands to any other app except the one that sent the request?
My guess is that writeCharacteristic
and onNotificationChanged
aren't the right functions for such kind of communication, but in this case which are the alternatives?