2
2
3
3
import android .bluetooth .BluetoothAdapter ;
4
4
import android .bluetooth .BluetoothDevice ;
5
- import android .os .ParcelUuid ;
6
- import android .util .SparseArray ;
7
5
8
6
import org .junit .Test ;
9
7
10
- import java .lang .reflect .Constructor ;
11
- import java .util .ArrayList ;
12
- import java .util .HashMap ;
13
- import java .util .List ;
14
- import java .util .Map ;
8
+ import java .lang .reflect .InvocationTargetException ;
9
+ import java .lang .reflect .Method ;
15
10
16
- import static org .junit .Assert .assertArrayEquals ;
17
- import static org .junit .Assert .assertEquals ;
18
- import static org .junit .Assert .assertNotNull ;
19
- import static org .junit .Assert .assertSame ;
20
- import static org .junit .Assert .fail ;
11
+ import static com .google .common .truth .Truth .assertThat ;
21
12
22
13
public class BluetoothLeScannerImplOreoTest {
23
14
24
15
@ Test
25
16
public void toImpl () {
26
17
// Build mock data
27
- List <ParcelUuid > serviceUuids = new ArrayList <>();
28
- serviceUuids .add (ParcelUuid .fromString ("00001809-0000-1000-8000-00805F9B34FB" ));
18
+ final byte [] bytes = new byte []{
19
+ 2 , 1 , 6 , // Flags
20
+ 5 , 8 , 'T' , 'e' , 's' , 't' , // Shortened Local Name (Test)
21
+ 6 , (byte ) 0xFF , 0x59 , 0x00 , 1 , 2 , 3 , // Manufacturer Data (Nordic Semi -> 0x010203)
22
+ 3 , 0x16 , 0x09 , 0x18 , // Service Data - 16-bit UUID (0x1809)
23
+ 2 , 0x0A , 1 // Tx Power Level (1 dBm)
24
+ };
29
25
30
- SparseArray < byte []> manufacturerData = new SparseArray <>();
31
- manufacturerData . append ( 0x0059 , new byte [] { 1 , 2 , 3 } );
26
+ final BluetoothDevice device =
27
+ BluetoothAdapter . getDefaultAdapter (). getRemoteDevice ( "01:02:03:04:05:06" );
32
28
33
- Map <ParcelUuid , byte []> serviceData = new HashMap <>();
34
- serviceData .put (ParcelUuid .fromString ("00001809-0000-1000-8000-00805F9B34FB" ), new byte [] { 0x64 });
29
+ final android .bluetooth .le .ScanRecord _record = parseScanRecord (bytes );
35
30
36
- final byte [] bytes = new byte [] { 2 , 1 , 6 , 5 , 8 , 'T' , 'e' , 's' , 't' , 6 , (byte ) 0xFF , 0x59 , 0x00 , 1 , 2 , 3 , 4 , 0x16 , 0x09 , 0x18 , 0x64 , 2 , 0x0A , 1 };
31
+ android .bluetooth .le .ScanResult _result = new android .bluetooth .le .ScanResult (device ,
32
+ 0b000001, 1 , 2 , 0 ,
33
+ android .bluetooth .le .ScanResult .TX_POWER_NOT_PRESENT , -70 ,
34
+ android .bluetooth .le .ScanResult .PERIODIC_INTERVAL_NOT_PRESENT , _record ,
35
+ 123456789L );
37
36
38
- try {
39
- BluetoothDevice device =
40
- BluetoothAdapter .getDefaultAdapter ().getRemoteDevice ("01:02:03:04:05:06" );
41
-
42
- final Constructor constructor =
43
- android .bluetooth .le .ScanRecord .class .getDeclaredConstructor (List .class ,
44
- SparseArray .class , Map .class , int .class , int .class , String .class , byte [].class );
45
- constructor .setAccessible (true );
46
- final android .bluetooth .le .ScanRecord _record = (android .bluetooth .le .ScanRecord )
47
- constructor .newInstance (serviceUuids , manufacturerData , serviceData , 0x06 , 1 , "Test" , bytes );
48
-
49
- android .bluetooth .le .ScanResult _result = new android .bluetooth .le .ScanResult (device ,
50
- 0b000001, 1 , 2 , 0 ,
51
- android .bluetooth .le .ScanResult .TX_POWER_NOT_PRESENT , -70 ,
52
- android .bluetooth .le .ScanResult .PERIODIC_INTERVAL_NOT_PRESENT , _record ,
53
- 123456789L );
37
+ // Convert to support.v18.ScanResult
38
+ final BluetoothLeScannerImplOreo impl = new BluetoothLeScannerImplOreo ();
39
+ final ScanResult result = impl .fromNativeScanResult (_result );
54
40
55
- // Convert to support.v18.ScanResult
56
- final BluetoothLeScannerImplOreo impl = new BluetoothLeScannerImplOreo ();
57
- final ScanResult result = impl .fromNativeScanResult (_result );
41
+ // Validate
42
+ assertThat (result ).isNotNull ();
43
+ assertThat (_record ).isNotNull ();
44
+ assertThat (_result .isLegacy ()).isEqualTo (result .isLegacy ());
45
+ assertThat (_result .isConnectable ()).isEqualTo (result .isConnectable ());
46
+ assertThat (result .getDataStatus ()).isEqualTo (ScanResult .DATA_COMPLETE );
47
+ assertThat (result .getScanRecord ()).isNotNull ();
48
+ final ScanRecord record = result .getScanRecord ();
49
+ assertThat (record .getAdvertiseFlags ()).isEqualTo (6 );
50
+ assertThat (bytes ).isEqualTo (record .getBytes ());
51
+ assertThat (record .getManufacturerSpecificData (0x0059 )).isNotNull ();
52
+ assertThat (_record .getManufacturerSpecificData (0x0059 ))
53
+ .isEqualTo (record .getManufacturerSpecificData (0x0059 ));
54
+ assertThat (result .getPeriodicAdvertisingInterval ())
55
+ .isEqualTo (ScanResult .PERIODIC_INTERVAL_NOT_PRESENT );
56
+ assertThat (result .getTxPower ()).isEqualTo (ScanResult .TX_POWER_NOT_PRESENT );
57
+ assertThat (result .getTimestampNanos ()).isEqualTo (123456789L );
58
+ assertThat (_result .getDevice ()).isEqualTo (result .getDevice ());
59
+ assertThat (device ).isEqualTo (result .getDevice ());
60
+ }
58
61
59
- // Validate
60
- assertEquals (_result .isLegacy (), result .isLegacy ());
61
- assertEquals (_result .isConnectable (), result .isConnectable ());
62
- assertEquals (ScanResult .DATA_COMPLETE , result .getDataStatus ());
63
- assertNotNull (result .getScanRecord ());
64
- final ScanRecord record = result .getScanRecord ();
65
- assertEquals (6 , record .getAdvertiseFlags ());
66
- assertArrayEquals (bytes , record .getBytes ());
67
- assertNotNull (record .getManufacturerSpecificData (0x0059 ));
68
- assertArrayEquals (_record .getManufacturerSpecificData (0x0059 ),
69
- record .getManufacturerSpecificData (0x0059 ));
70
- assertEquals (ScanResult .PERIODIC_INTERVAL_NOT_PRESENT , result .getPeriodicAdvertisingInterval ());
71
- assertEquals (ScanResult .TX_POWER_NOT_PRESENT , result .getTxPower ());
72
- assertEquals (123456789L , result .getTimestampNanos ());
73
- assertSame (_result .getDevice (), result .getDevice ());
74
- assertSame (device , result .getDevice ());
75
- } catch (Exception e ) {
76
- fail (e .getMessage ());
62
+ /**
63
+ * Utility method to call hidden ScanRecord.parseFromBytes method.
64
+ */
65
+ static android .bluetooth .le .ScanRecord parseScanRecord (byte [] bytes ) {
66
+ final Class <?> scanRecordClass = android .bluetooth .le .ScanRecord .class ;
67
+ try {
68
+ final Method method = scanRecordClass .getDeclaredMethod ("parseFromBytes" , byte [].class );
69
+ return (android .bluetooth .le .ScanRecord ) method .invoke (null , bytes );
70
+ } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
71
+ | InvocationTargetException e ) {
72
+ return null ;
77
73
}
78
74
}
79
75
}
0 commit comments