| 1498 | } |
| 1499 | |
| 1500 | pub fn run_corebluetooth_thread( |
| 1501 | event_sender: Sender<CoreBluetoothEvent>, |
| 1502 | ) -> Result<Sender<CoreBluetoothMessage>, Error> { |
| 1503 | let authorization = unsafe { CBManager::authorization_class() }; |
| 1504 | if authorization != CBManagerAuthorization::AllowedAlways |
| 1505 | && authorization != CBManagerAuthorization::NotDetermined |
| 1506 | { |
| 1507 | warn!("Authorization status {:?}", authorization); |
| 1508 | return Err(Error::PermissionDenied); |
| 1509 | } else { |
| 1510 | trace!("Authorization status {:?}", authorization); |
| 1511 | } |
| 1512 | let (sender, receiver) = mpsc::channel::<CoreBluetoothMessage>(256); |
| 1513 | // CoreBluetoothInternal is !Send, so we need to keep it on a single thread. |
| 1514 | thread::spawn(move || { |
| 1515 | let runtime = runtime::Builder::new_current_thread().build().unwrap(); |
| 1516 | runtime.block_on(async move { |
| 1517 | let mut cbi = CoreBluetoothInternal::new(receiver, event_sender); |
| 1518 | loop { |
| 1519 | cbi.wait_for_message().await; |
| 1520 | } |
| 1521 | }) |
| 1522 | }); |
| 1523 | Ok(sender) |
| 1524 | } |