Reputation: 33
I'd like to discover the Chromecast device name to be able to generate some unique id. Alternatively is there other unique id I could use?
Upvotes: 0
Views: 618
Reputation: 438
There is no official api on the receiver side, however...
On the payload of SENDER_CONNECTED events there is a property which is not exposed by CAF. The property name is also scrambled so you can resort to regex to try and obtain the sender name from this payload.
You should always provide a backup if this method fails (or to show before the sender connected event is fired).
const context = cast.framework.CastReceiverContext.getInstance();
// listen for sender connections
context.addEventListener(cast.framework.system.EventType.SENDER_CONNECTED,
(evt) => {
console.log('device name: ' + tryExtractDeviceName(evt) ?? '<unknown>');
}
);
function isNonEmptyString(value: unknown): value is string {
return typeof(value) === 'string' && value.trim().length > 0;
}
function stringify(value: unknown): string {
// if this gives any issues with circular objects
// replace this function with a version that can cope with that.
return JSON.stringify(value);
}
function tryExtractDeviceName(evt: system.SenderConnectedEvent): string | void {
// evt.target[<letter>].deviceName exists on this event but isn't exposed in the API!
try {
const deviceName = /"deviceName":"([^"]+)"/.exec(stringify(evt))?.[1] ?? '';
if (isNonEmptyString(deviceName)) {
return deviceName;
}
} catch {
// ignore
}
}
Upvotes: 0
Reputation: 19074
I imagine by "name" you mean the name that you assign to your device, correct? If so, you cannot get that on the receiver side. The only way to get that which comes to my mind is to have a "sender" app sends that to the receiver. I am not sure how you are using it but names are not required to be unique, so keep that in mind; and they can be changed by a user.
There is no unique id that is exposed, as far as I know. Can you tell us what you'd like to do so we may be able to offer an alternate route?
Upvotes: 1