I am developing an Android app featuring the RTT ranging between two WiFi aware peers (one publisher and one subscriber), similar to the demo app from Google. I have been following the Android documentation on WiFiAware and at this stage the two devices (publisher and subcriber) can find each other and send message bi-directionally. However I just couldn't enable RTT ranging between the two, which should be easily enabled by following addWifiAwarePeer to RangingRequest.
I tried testing my app with the Android demo app. With my app being the publisher, the subscriber from Google demo app can obtain ranging results. However with my app being the subcriber, the Google demo app (publisher) just crashed whenever the subcriber discovered it. So I think theres probably something wrong with my subscriber side. Below is my code in Java:
private void subscribeService() {
mServiceMap.clear();
SubscribeConfig config = new SubscribeConfig.Builder()
.setServiceName(SERVICE_NAME)
.build();
wifiAwareSession.subscribe(config, new DiscoverySessionCallback() {
@Override
/** Called when subscribe is started successfully */
public void onSubscribeStarted(@NonNull SubscribeDiscoverySession session) {
super.onSubscribeStarted(session);
subscribeDiscoverySession = session;
}
@Override
/** Called when a subscribe operation results in a service discovery */
public void onServiceDiscovered(PeerHandle peerHandle, byte[] serviceSpecificInfo, List<byte[]> matchFilter) {
super.onServiceDiscovered(peerHandle, serviceSpecificInfo, matchFilter);
String key = new String(serviceSpecificInfo);
mServiceMap.put(key, peerHandle);
List<String> list = new ArrayList<>(mServiceMap.keySet());
mNormalAdapter.setNewData(list);
subscribeDiscoverySession.sendMessage(peerHandle, 1, "".getBytes());
}
@Override
/** Called when a message is received from a discovery session peer*/
public void onMessageReceived(PeerHandle peerHandle, byte[] message) {
super.onMessageReceived(peerHandle, message);
String receiveMsg = new String(message);
if (StringUtils.isEmpty(receiveMsg)) {
return;
}
EditText editText = binding.etMsg;
editText.setText(receiveMsg);
}
}, null);
}
The peerhandle from onServiceDiscovered was stored in mServiceMap and the key was the serviceSpecificInfo. I used this peerhandle for RTT ranging and it returned status 1 meaning operation failed. I really dont know where it went wrong and any help is appreciated!