- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 36
Add available_ble_devices() method #66
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
Conversation
Also refactored BleHandler: * added available_peripherals method, factoring out logic out of BleHandler::find_ble_radio. * added NameAndMac to the BleId enum * added comments
| } | ||
|  | ||
| /// Bluetooth Low Energy ID, used to filter available devices. | ||
| pub enum BleId { | 
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.
In my code I wanted to derive Debug and CLone for this struct:
#[derive(Debug, Clone)]
pub enum DiscoveryEvent {
    BLERadioFound(BleId),
    BLERadioLost(BleId),
}
but neither are provided for BleId
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.
Added
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.
Thanks.
I wanted to maintain a Vec of found (in previous iteration of discovery loop) radios, and then detect new discoveries and "losses".
But because of the form of BleId, this is kind of hard to do.... as you can't in the code be sure what you will get back each loop. I guess I will need to convert both to string and store that in the Vec and hope the BleId returned doesn't shift over time (why would it!?)
This is done like this as you cannot be guaranteed one or the other?
e.g. it couldn't be  a struct with a MacAddress (for sure) and an Option for the name?
pub enum BleId {
    /// ID constructed from a name
    Name(String),
    /// ID represented from a MAC address
    MacAddress(BDAddr),
}
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.
Also, detecting if I already have discovered it in the Vec will require PartialEq:-(
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.
This is the part of the API I originally struggled with and that's why I haven't posted this for two months.
I have another idea for API:
- available_ble_devicesreturns a tuple- (BDaddr, String)(or it could be a new struct), not BleID. This is to indicate that we always have both MAC + name available.
- The users convert BDaddr to MacAddress to use it in build_ble_stream. We will provide a.into()method for simplicity.
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.
If they are both always there, you could just make it a struct with BDaddr and String. Would make using it easier. PartialEq could use address (I assume two devices could be assigned the same name by their respective users?).
Display or to_string() could use name.
It would remove the need for matching on enums to do almost anything with BleId.
| Might also be nice to include this in  | 
| 
 I'll do this in a followup PR, let's get this working first. Network Management Client claims it works: meshtastic/network-management-client#515 (comment) | 
| 
 Sounds good @lukipuki, there's rustdocs so the example script is just icing on the cake | 
| In case you missed the comment. For me to be able to use BleId in a vec and then determine if a particular BleId is there or not, it would also need to derive PartialEq. I can't implement it due to the foreign crate + foreign trait limitation. | 
| I implemented my own  While testing, it seems that using available_devices() with a 1 or 2 or 3 second delay, oscillates between returning my one device, or returning zero. I had to extend the scan time to 4 seconds to get a stable list (of 1 device) being returned. I guess that's just the nature of BTLE and the radio I happen to have. It makes the list less "fresh" though. The ideal API would be some form of streaming discovery API with messages for devices lost and found. | 
| My next step will be to connect to device, using build_ble_stream() I assume. | 
| For me, this new method is working. Do you plan to do more before merging the PR. If you wanted to capture some of the above comments above for possible future enhancements, I could write an issue or two? | 
| Thanks for the offer, but there's no need to create a GitHub issue for this. I've just been busy with a hobby project that involves using Meshtastic in the field, broadcasting split times for sport events over LoRa. Now that the colder months are approaching, I'll be spending less time outdoors and more time coding again. I'll hopefully get to this within the next week or two. | 
| Closing in favor of another approach, I'll drop  | 
| Continuing in #68 | 
Summary
The
available_ble_devices()is requested by many users to detect BLE devices running Meshtastic.Also refactored BleHandler:
Related Issues
#32
Checklist