question

frankzhang-3676 avatar image
0 Votes"
frankzhang-3676 asked frankzhang-3676 commented

Is there a way for BLE peripheral to diff from two centrals subscribe notify , and only notify to one of them specified ?(UWP)

In my BLE case, there seems to be a unsolved problem:
I have two centrals MasterA and MasterB , both of them do subscribe to one peripheral DeviceC. I know both subscribe actions will trigger SubscribedClientsChanged event , but the params for SubscribedClientsChanged can not figure out which event comes from MasterA or MasterB, the only thing seems valuable is GattLocalCharacteristic::SubscribedClients, but SubscribedClients can not thell any clue such as ble address, device name or other things for MasterA/MasterB. Through the SubscribedClientsChanged event, only things I can know is how many Masters do this subscribe, but can not diff one from another.

When DeviceC want to do notify only to MasterA, even I know the API for DeviceC only notify to one Master :

notifyCharacteristic->NotifyValueAsync(writer->DetachBuffer(), / SubscribedClient of MasterA/);

I can not figure out which SubscribedClient I should add into it.

Am I tell the problem clear ? And any solution for it? help....

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered frankzhang-3676 commented

Hello,
Welcome to Microsoft Q&A!

but the params for SubscribedClientsChanged can not figure out which event comes from MasterA or MasterB

You could get each client device from SubscribedClientsChanged event. you could get clients with List<GattSubscribedClient> clients = sender.SubscribedClients; in SubscribedClientsChanged event


 void _notifyCharacteristic_SubscribedClientsChanged(GattLocalCharacteristic sender, object args)
 {
     List<GattSubscribedClient> clients = sender.SubscribedClients;
     // Diff the new list of clients from a previously saved one 
     // to get which device has subscribed for notifications. 
    
     // You can also just validate that the list of clients is expected for this app.  
 }

Iterate the list, you could get each GattSubscribedClient, and GattSubscribedClient has DeviceId value in the Session propety that could use to detect which is target device. Record it with global variable then pass to NotifyValueAsync method. For more info please refer to Bluetooth GATT Server documention.

Thank you.



If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have already known it and dumped ID from DeviceId:

test code:
void BLEServer::notifyChangedHandler(Bluetooth::GenericAttributeProfile::GattLocalCharacteristic^ characteristic, Platform::Object^ args)
{
std::cout << "notify subscribe changed..." << std::endl;
for (int i = 0; i < characteristic->SubscribedClients->Size; i++) {
std::cout <<"id: " << platStrToStdStr(characteristic->SubscribedClients->GetAt(i)->Session->DeviceId->Id).c_str() << std::endl;
}

}

dump log first time:

notify subscribe changed...
id: BluetoothLE#BluetoothLEf8:5e:a0:2b:97:96-53:9d:9d:1b:82:b7
id: BluetoothLE#BluetoothLEf8:5e:a0:2b:97:96-43:65:4b:8e:7b:0e

dump log second time:

notify subscribe changed...
id: BluetoothLE#BluetoothLEf8:5e:a0:2b:97:96-43:65:4b:8e:7b:0e
id: BluetoothLE#BluetoothLEf8:5e:a0:2b:97:96-51:7a:0b:17:c7:57

Do you mean f8:5e:a0:2b:97:96 is DeviceC hardwre address? But address of MasterA and MasterB are changeable ? Can you tell how to relate it to MasterA and MasterB?


0 Votes 0 ·

You could notify those two device one by one, and then send device name request from Device A and B, and then you could get this request from ResultCharacteristic_ReadRequestedAsync event. GattReadRequestedEventArgs also contain Session property, you could use it to map device name with Id.

0 Votes 0 ·

Maybe you got it !

You mean that if one master is connected with one device, the session in GattLocalCharacteristic::SubscribedClients::session of write/read/notifyChanged characteristic handler input param are all the same ? If so , I think it will be the solution. I will have a try, thanks.





0 Votes 0 ·