-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(macos): reading descriptor value #391
Conversation
trace!("Getting data!"); | ||
let v = unsafe { descriptor.value() }.map(|value| unsafe { | ||
match descriptor.UUID().UUIDString().to_string().as_str() { | ||
"2901" => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's special about "2901" here? Why can't this work for other descriptor UUIDs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are predefined UUIDs for descriptors for which we know the type for (in case of 2901
, it is NSString). I think it's possible to create custom descriptors, but without knowing what the underlying type if for that it's not possible to get the value (AFAIK)
The predefined descriptor UUIDs are in this pdf, in section 3.7:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot to mention the root problem: descriptor.value()
is a Retained<AnyObject>
. It must be cast to a type and it will throw an error if you try to cast it to an invalid type. So in this case if I would try to cast the value to for example NSData
instead of NSString
, it will throw an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I can see in the Apple documentation, it looks like the value will either be an NSString
, NSData
, NSNumber
or UInt16
. Can you use AnyObject::class
to check which type it is, then cast and convert it accordingly? This should be more robust and general than special-casing each UUID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to trace back to the superclass of the class because in my case I have the same descriptor within 5 different characteristic and in one characteristic the type was NSTaggedPointerString
, in the other one it was __NSCFString
. Both are subclasses of NSString
and can be safely cast to it. This way it's a bit hacky to get the value of NSNumber
. Do you have any suggestions how should I get the class of a number this way? Also, should we panic if there are no matching class type?
Reading out the value of the descriptors was not correctly implemented before. My solution needs to be extended with all the possible standards descriptor UUIDs but it provides a good starting point.
Also fixes a delegate bug (there were incorrect methods assigned to the functions)