Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ objc2-foundation = { version = "0.2.2", default-features = false, features = [
"NSString",
"NSUUID",
"NSValue",
"NSProcessInfo",
] }
objc2-core-bluetooth = { version = "0.2.2", default-features = false, features = [
"std",
Expand Down
36 changes: 35 additions & 1 deletion src/corebluetooth/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ use objc2_core_bluetooth::{
CBCharacteristicProperties, CBCharacteristicWriteType, CBDescriptor, CBManager,
CBManagerAuthorization, CBManagerState, CBPeripheral, CBPeripheralState, CBService, CBUUID,
};
use objc2_foundation::{NSArray, NSData, NSMutableDictionary, NSNumber};
use objc2_foundation::{NSArray, NSData, NSMutableDictionary, NSNumber, NSProcessInfo};
use std::{
collections::{BTreeSet, HashMap, VecDeque},
ffi::CString,
fmt::{self, Debug, Formatter},
ops::Deref,
thread,
time::Duration,
};
use tokio::runtime;
use uuid::Uuid;
Expand Down Expand Up @@ -361,6 +362,12 @@ impl PeripheralInternal {
}
}

/// Optional CoreBluetooth API capabilities that are available depending on the macOS version.
struct CoreBluetoothFeatures {
/// `peripheral.canSendWriteWithoutResponse` property is available (since macOS 11.2.0)
can_send_write_without_response: bool,
}

// All of CoreBluetooth is basically async. It's all just waiting on delegate
// events/callbacks. Therefore, we should be able to round up all of our wacky
// ass mut *Object values, keep them in a single struct, in a single thread, and
Expand All @@ -375,6 +382,7 @@ struct CoreBluetoothInternal {
// task::block this when sending even though it'll never actually block.
event_sender: Sender<CoreBluetoothEvent>,
message_receiver: Fuse<Receiver<CoreBluetoothMessage>>,
features: CoreBluetoothFeatures,
}

impl Debug for CoreBluetoothInternal {
Expand Down Expand Up @@ -473,6 +481,16 @@ pub enum CoreBluetoothEvent {
},
}

fn get_features() -> CoreBluetoothFeatures {
let process_info = NSProcessInfo::processInfo();
let version = process_info.operatingSystemVersion();
let current = (version.majorVersion, version.minorVersion);

CoreBluetoothFeatures {
can_send_write_without_response: current >= (11, 2),
}
}

impl CoreBluetoothInternal {
pub fn new(
message_receiver: Receiver<CoreBluetoothMessage>,
Expand All @@ -498,6 +516,7 @@ impl CoreBluetoothInternal {
event_sender,
message_receiver: message_receiver.fuse(),
delegate,
features: get_features(),
}
}

Expand Down Expand Up @@ -887,6 +906,21 @@ impl CoreBluetoothInternal {
{
trace!("Writing value! With kind {:?}", kind);
unsafe {
if kind == WriteType::WithoutResponse
&& self.features.can_send_write_without_response
{
// probably better idea would be to wait for the result of peripheral.peripheralIsReadyToSendWriteWithoutResponse
let mut attempts = 0;
while !peripheral.peripheral.canSendWriteWithoutResponse()
&& attempts < 100
{
attempts += 1;
// min. connection interval time is 15ms. see the document:
// https://developer.apple.com/library/archive/qa/qa1931/_index.html
thread::sleep(Duration::from_millis(15));
}
}

peripheral.peripheral.writeValue_forCharacteristic_type(
&NSData::from_vec(data),
&characteristic.characteristic,
Expand Down
Loading