webbluetooth_android/
lib.rs1#[doc(hidden)]
11pub mod backend;
12#[cfg(target_os = "android")]
14pub mod peripheral_backend;
15
16pub mod ble;
17pub mod bluetooth;
18#[cfg(feature = "classic-l2cap")]
19pub mod classic_l2cap;
20pub mod dex;
21pub mod jni;
22pub mod l2cap;
23#[cfg(feature = "rfcomm")]
24pub mod rfcomm;
25pub mod runtime;
26
27use dex::DexBuilder;
28
29pub const PACKAGE: &str = "dev/webbluetooth";
31
32pub fn dex_classes() -> Vec<(&'static str, Vec<u8>)> {
39 vec![
40 ("gatt", gatt_callback_dex()),
41 ("scan", scan_callback_dex()),
42 #[cfg(feature = "classic")]
43 ("classic-discovery", classic_discovery_receiver_dex()),
44 ("server", gatt_server_callback_dex()),
45 ("advertise", advertise_callback_dex()),
46 ]
47}
48
49pub fn gatt_callback_dex() -> Vec<u8> {
51 let mut builder = DexBuilder::new(
59 &format!("{PACKAGE}/GattCallback"),
60 "android/bluetooth/BluetoothGattCallback",
61 );
62 for (name, descriptor, _) in crate::bluetooth::gatt_natives() {
63 builder = builder.native_method(name, descriptor);
64 }
65 builder.build()
66}
67
68pub fn scan_callback_dex() -> Vec<u8> {
70 DexBuilder::new(
71 &format!("{PACKAGE}/ScanCallback"),
72 "android/bluetooth/le/ScanCallback",
73 )
74 .native_method("onScanResult", "(ILandroid/bluetooth/le/ScanResult;)V")
75 .native_method("onBatchScanResults", "(Ljava/util/List;)V")
76 .native_method("onScanFailed", "(I)V")
77 .build()
78}
79
80#[cfg(feature = "classic")]
82pub fn classic_discovery_receiver_dex() -> Vec<u8> {
83 DexBuilder::new(
84 &format!("{PACKAGE}/ClassicDiscoveryReceiver"),
85 "android/content/BroadcastReceiver",
86 )
87 .native_method(
88 "onReceive",
89 "(Landroid/content/Context;Landroid/content/Intent;)V",
90 )
91 .build()
92}
93
94pub fn gatt_server_callback_dex() -> Vec<u8> {
96 const DEVICE: &str = "Landroid/bluetooth/BluetoothDevice;";
97 const CHARACTERISTIC: &str = "Landroid/bluetooth/BluetoothGattCharacteristic;";
98 const DESCRIPTOR: &str = "Landroid/bluetooth/BluetoothGattDescriptor;";
99 const SERVICE: &str = "Landroid/bluetooth/BluetoothGattService;";
100 DexBuilder::new(
101 &format!("{PACKAGE}/GattServerCallback"),
102 "android/bluetooth/BluetoothGattServerCallback",
103 )
104 .native_method("onConnectionStateChange", &format!("({DEVICE}II)V"))
105 .native_method("onServiceAdded", &format!("(I{SERVICE})V"))
106 .native_method(
107 "onCharacteristicReadRequest",
108 &format!("({DEVICE}II{CHARACTERISTIC})V"),
109 )
110 .native_method(
111 "onCharacteristicWriteRequest",
112 &format!("({DEVICE}I{CHARACTERISTIC}ZZI[B)V"),
113 )
114 .native_method(
115 "onDescriptorReadRequest",
116 &format!("({DEVICE}II{DESCRIPTOR})V"),
117 )
118 .native_method(
119 "onDescriptorWriteRequest",
120 &format!("({DEVICE}I{DESCRIPTOR}ZZI[B)V"),
121 )
122 .native_method("onNotificationSent", &format!("({DEVICE}I)V"))
123 .native_method("onMtuChanged", &format!("({DEVICE}I)V"))
124 .build()
125}
126
127pub fn advertise_callback_dex() -> Vec<u8> {
129 DexBuilder::new(
130 &format!("{PACKAGE}/AdvertiseCallback"),
131 "android/bluetooth/le/AdvertiseCallback",
132 )
133 .native_method(
134 "onStartSuccess",
135 "(Landroid/bluetooth/le/AdvertiseSettings;)V",
136 )
137 .native_method("onStartFailure", "(I)V")
138 .build()
139}