@@ -8,15 +8,15 @@ use futures_channel::mpsc::{Receiver, Sender};
8
8
use futures_core:: Stream ;
9
9
use futures_lite:: { stream, StreamExt } ;
10
10
use jni_glue:: { Argument , ByteArray , Env , Global , Local , PrimitiveArray , VM } ;
11
- use tracing:: { info , warn} ;
11
+ use tracing:: { debug , warn} ;
12
12
use uuid:: Uuid ;
13
13
14
14
use super :: bindings:: android:: bluetooth:: le:: { BluetoothLeScanner , ScanResult } ;
15
15
use super :: bindings:: android:: bluetooth:: { BluetoothAdapter , BluetoothManager } ;
16
16
use super :: bindings:: android:: os:: ParcelUuid ;
17
17
use super :: bindings:: com:: github:: alexmoon:: bluest:: android:: BluestScanCallback ;
18
18
use super :: device:: DeviceImpl ;
19
- use super :: JavaIterator ;
19
+ use super :: { JavaIterator , OptionExt } ;
20
20
use crate :: android:: bindings:: java:: util:: Map_Entry ;
21
21
use crate :: util:: defer;
22
22
use crate :: {
@@ -41,8 +41,8 @@ impl AdapterImpl {
41
41
42
42
vm. with_env ( |env| {
43
43
let local_manager = manager. with ( env) ;
44
- let adapter = local_manager. getAdapter ( ) ?. unwrap ( ) ;
45
- let le_scanner = adapter. getBluetoothLeScanner ( ) ?. unwrap ( ) ;
44
+ let adapter = local_manager. getAdapter ( ) ?. non_null ( ) ? ;
45
+ let le_scanner = adapter. getBluetoothLeScanner ( ) ?. non_null ( ) ? ;
46
46
47
47
Ok ( Self {
48
48
inner : Arc :: new ( AdapterInner {
@@ -89,8 +89,10 @@ impl AdapterImpl {
89
89
self . inner . manager . vm ( ) . with_env ( |env| {
90
90
let callback = callback_global. with ( env) ;
91
91
let scanner = self . inner . le_scanner . with ( env) ;
92
- scanner. stopScan_ScanCallback ( & * * callback) . unwrap ( ) ;
93
- info ! ( "stopped scan" ) ;
92
+ match scanner. stopScan_ScanCallback ( & * * callback) {
93
+ Ok ( ( ) ) => debug ! ( "stopped scan" ) ,
94
+ Err ( e) => warn ! ( "failed to stop scan: {:?}" , e) ,
95
+ } ;
94
96
} ) ;
95
97
} ) ;
96
98
@@ -225,53 +227,60 @@ pub extern "system" fn Java_com_github_alexmoon_bluest_android_BluestScanCallbac
225
227
callback_type : i32 ,
226
228
scan_result : Argument < ScanResult > ,
227
229
) {
228
- let scan_result = unsafe { scan_result. with_unchecked ( env) } . unwrap ( ) ;
230
+ if let Err ( e) = on_scan_result ( env, id, callback_type, scan_result) {
231
+ warn ! ( "on_scan_result failed: {:?}" , e) ;
232
+ }
233
+ }
229
234
230
- tracing:: info!( "got callback! {} {}" , id, callback_type) ;
235
+ fn convert_uuid ( uuid : Local < ' _ , ParcelUuid > ) -> Result < Uuid > {
236
+ let uuid = uuid. getUuid ( ) ?. non_null ( ) ?;
237
+ let lsb = uuid. getLeastSignificantBits ( ) ? as u64 ;
238
+ let msb = uuid. getMostSignificantBits ( ) ? as u64 ;
239
+ Ok ( Uuid :: from_u64_pair ( msb, lsb) )
240
+ }
231
241
232
- let scan_record = scan_result. getScanRecord ( ) . unwrap ( ) . unwrap ( ) ;
233
- let device = scan_result. getDevice ( ) . unwrap ( ) . unwrap ( ) ;
242
+ #[ no_mangle]
243
+ fn on_scan_result ( env : Env < ' _ > , id : i32 , callback_type : i32 , scan_result : Argument < ScanResult > ) -> Result < ( ) > {
244
+ let scan_result = unsafe { scan_result. with_unchecked ( env) } . non_null ( ) ?;
234
245
235
- fn convert_uuid ( uuid : Local < ' _ , ParcelUuid > ) -> Uuid {
236
- let uuid = uuid. getUuid ( ) . unwrap ( ) . unwrap ( ) ;
237
- let lsb = uuid. getLeastSignificantBits ( ) . unwrap ( ) as u64 ;
238
- let msb = uuid. getMostSignificantBits ( ) . unwrap ( ) as u64 ;
239
- Uuid :: from_u64_pair ( msb, lsb)
240
- }
246
+ tracing:: info!( "got callback! {} {}" , id, callback_type) ;
241
247
242
- let address = device. getAddress ( ) . unwrap ( ) . unwrap ( ) . to_string_lossy ( ) ;
243
- let rssi = scan_result. getRssi ( ) . unwrap ( ) ;
244
- let is_connectable = scan_result. isConnectable ( ) . unwrap ( ) ;
245
- let local_name = scan_record. getDeviceName ( ) . unwrap ( ) . map ( |s| s. to_string_lossy ( ) ) ;
246
- let tx_power_level = scan_record. getTxPowerLevel ( ) . unwrap ( ) ;
248
+ let scan_record = scan_result. getScanRecord ( ) ?. non_null ( ) ?;
249
+ let device = scan_result. getDevice ( ) ?. non_null ( ) ?;
250
+
251
+ let address = device. getAddress ( ) ?. non_null ( ) ?. to_string_lossy ( ) ;
252
+ let rssi = scan_result. getRssi ( ) ?;
253
+ let is_connectable = scan_result. isConnectable ( ) ?;
254
+ let local_name = scan_record. getDeviceName ( ) ?. map ( |s| s. to_string_lossy ( ) ) ;
255
+ let tx_power_level = scan_record. getTxPowerLevel ( ) ?;
247
256
248
257
// Services
249
258
let mut services = Vec :: new ( ) ;
250
- if let Some ( uuids) = scan_record. getServiceUuids ( ) . unwrap ( ) {
251
- for uuid in JavaIterator ( uuids. iterator ( ) . unwrap ( ) . unwrap ( ) ) {
252
- services. push ( convert_uuid ( uuid. cast ( ) . unwrap ( ) ) )
259
+ if let Some ( uuids) = scan_record. getServiceUuids ( ) ? {
260
+ for uuid in JavaIterator ( uuids. iterator ( ) ? . non_null ( ) ? ) {
261
+ services. push ( convert_uuid ( uuid. cast ( ) ? ) ? )
253
262
}
254
263
}
255
264
256
265
// Service data
257
266
let mut service_data = HashMap :: new ( ) ;
258
- let sd = scan_record. getServiceData ( ) . unwrap ( ) . unwrap ( ) ;
259
- let sd = sd. entrySet ( ) . unwrap ( ) . unwrap ( ) ;
260
- for entry in JavaIterator ( sd. iterator ( ) . unwrap ( ) . unwrap ( ) ) {
261
- let entry: Local < Map_Entry > = entry. cast ( ) . unwrap ( ) ;
262
- let key: Local < ParcelUuid > = entry. getKey ( ) . unwrap ( ) . unwrap ( ) . cast ( ) . unwrap ( ) ;
263
- let val: Local < ByteArray > = entry. getValue ( ) . unwrap ( ) . unwrap ( ) . cast ( ) . unwrap ( ) ;
264
- service_data. insert ( convert_uuid ( key) , val. as_vec ( ) . into_iter ( ) . map ( |i| i as u8 ) . collect ( ) ) ;
267
+ let sd = scan_record. getServiceData ( ) ? . non_null ( ) ? ;
268
+ let sd = sd. entrySet ( ) ? . non_null ( ) ? ;
269
+ for entry in JavaIterator ( sd. iterator ( ) ? . non_null ( ) ? ) {
270
+ let entry: Local < Map_Entry > = entry. cast ( ) ? ;
271
+ let key: Local < ParcelUuid > = entry. getKey ( ) ? . non_null ( ) ? . cast ( ) ? ;
272
+ let val: Local < ByteArray > = entry. getValue ( ) ? . non_null ( ) ? . cast ( ) ? ;
273
+ service_data. insert ( convert_uuid ( key) ? , val. as_vec ( ) . into_iter ( ) . map ( |i| i as u8 ) . collect ( ) ) ;
265
274
}
266
275
267
276
// Manufacturer data
268
277
let mut manufacturer_data = None ;
269
- let msd = scan_record. getManufacturerSpecificData ( ) . unwrap ( ) . unwrap ( ) ;
278
+ let msd = scan_record. getManufacturerSpecificData ( ) ? . non_null ( ) ? ;
270
279
// TODO there can be multiple manufacturer data entries, but the bluest API only supports one. So grab just the first.
271
- if msd. size ( ) . unwrap ( ) != 0 {
272
- let val: Local < ' _ , ByteArray > = msd. valueAt ( 0 ) . unwrap ( ) . unwrap ( ) . cast ( ) . unwrap ( ) ;
280
+ if msd. size ( ) ? != 0 {
281
+ let val: Local < ' _ , ByteArray > = msd. valueAt ( 0 ) ? . non_null ( ) ? . cast ( ) ? ;
273
282
manufacturer_data = Some ( ManufacturerData {
274
- company_id : msd. keyAt ( 0 ) . unwrap ( ) as _ ,
283
+ company_id : msd. keyAt ( 0 ) ? as _ ,
275
284
data : val. as_vec ( ) . into_iter ( ) . map ( |i| i as u8 ) . collect ( ) ,
276
285
} ) ;
277
286
}
@@ -291,6 +300,8 @@ pub extern "system" fn Java_com_github_alexmoon_bluest_android_BluestScanCallbac
291
300
rssi : Some ( rssi as _ ) ,
292
301
} ;
293
302
SCAN_CALLBACKS . callback ( id, d) ;
303
+
304
+ Ok ( ( ) )
294
305
}
295
306
296
307
#[ no_mangle]
0 commit comments