MCPcopy Create free account
hub / github.com/deviceplug/btleplug / try_from

Method try_from

src/droidplug/jni/objects.rs:738–857  ·  view source on GitHub ↗
(result: JScanResult<'a, 'b>)

Source from the content-addressed store, hash-verified

736 type Error = crate::Error;
737
738 fn try_from(result: JScanResult<'a, 'b>) -> std::result::Result<Self, Self::Error> {
739 use std::str::FromStr;
740
741 let device = result.get_device()?;
742
743 let addr_obj = device.get_address()?;
744 let addr_str = JavaStr::from_env(result.env, addr_obj)?;
745 let addr = BDAddr::from_str(
746 addr_str
747 .to_str()
748 .map_err(|e| Self::Error::Other(e.into()))?,
749 )?;
750
751 let record = result.get_scan_record()?;
752 let record_obj: &JObject = &record;
753 let properties = if result
754 .env
755 .is_same_object(record_obj.clone(), JObject::null())?
756 {
757 None
758 } else {
759 let device_name_obj = record.get_device_name()?;
760 let device_name = if result
761 .env
762 .is_same_object(device_name_obj, JObject::null())?
763 {
764 None
765 } else {
766 let device_name_str = JavaStr::from_env(result.env, device_name_obj)?;
767 // On Android, there is a chance that a device name may not actually be valid UTF-8.
768 // We're given the full buffer, regardless of if it's just UTF-8 characters,
769 // possibly c str with null characters, or whatever. We should try UTF-8 first, if
770 // that doesn't work out, see if there's a null termination character in it and try
771 // parsing that.
772 Some(
773 String::from_utf8_lossy(device_name_str.to_bytes())
774 .chars()
775 .filter(|&c| c != '\u{fffd}')
776 .collect(),
777 )
778 };
779
780 let tx_power_level = result.get_tx_power()?;
781 const TX_POWER_NOT_PRESENT: jint = 127; // from ScanResult documentation
782 let tx_power_level = if tx_power_level == TX_POWER_NOT_PRESENT {
783 None
784 } else {
785 Some(tx_power_level as i16)
786 };
787
788 let rssi = Some(result.get_rssi()? as i16);
789
790 let manufacturer_specific_data_array = record.get_manufacturer_specific_data()?;
791 let manufacturer_specific_data_obj: &JObject = &manufacturer_specific_data_array;
792 let mut manufacturer_data = HashMap::new();
793 if !result
794 .env
795 .is_same_object(manufacturer_specific_data_obj.clone(), JObject::null())?

Callers

nothing calls this directly

Calls 14

byte_array_to_vecFunction · 0.85
get_deviceMethod · 0.80
get_addressMethod · 0.80
get_scan_recordMethod · 0.80
get_device_nameMethod · 0.80
get_tx_powerMethod · 0.80
get_rssiMethod · 0.80
iterMethod · 0.80
into_innerMethod · 0.80
get_service_dataMethod · 0.80
as_uuidMethod · 0.80

Tested by

no test coverage detected