#![allow(clippy::not_unsafe_ptr_arg_deref)]
use crate::bluetooth::{Event, EventSink, Ref};
use crate::jni::{Env, JObject, JValue};
use crate::runtime::{Error, Runtime};
use std::sync::Arc;
type Result<T> = std::result::Result<T, Error>;
const TRANSPORT_LE: i32 = 2;
pub const WRITE_TYPE_DEFAULT: i32 = 2;
pub const WRITE_TYPE_NO_RESPONSE: i32 = 1;
pub const GATT_SUCCESS: i32 = 0;
fn missing(what: &str) -> Error {
Error::MissingMethod(what.into())
}
pub fn call_object(
env: Env,
object: JObject,
name: &str,
signature: &str,
args: &[JValue],
) -> Result<JObject> {
let method = env
.method_id(env.get_object_class(object), name, signature)
.ok_or_else(|| missing(name))?;
Ok(env.call_object(object, method, args))
}
pub fn call_bool(
env: Env,
object: JObject,
name: &str,
signature: &str,
args: &[JValue],
) -> Result<bool> {
let method = env
.method_id(env.get_object_class(object), name, signature)
.ok_or_else(|| missing(name))?;
Ok(env.call_bool(object, method, args))
}
pub fn call_int(
env: Env,
object: JObject,
name: &str,
signature: &str,
args: &[JValue],
) -> Result<i32> {
let method = env
.method_id(env.get_object_class(object), name, signature)
.ok_or_else(|| missing(name))?;
Ok(env.call_int(object, method, args))
}
pub fn call_void(
env: Env,
object: JObject,
name: &str,
signature: &str,
args: &[JValue],
) -> Result<()> {
let method = env
.method_id(env.get_object_class(object), name, signature)
.ok_or_else(|| missing(name))?;
env.call_void(object, method, args);
Ok(())
}
pub fn uuid(env: Env, canonical: &str) -> Result<JObject> {
let class = env
.find_class("java/util/UUID")
.ok_or_else(|| Error::MissingClass("java.util.UUID".into()))?;
let from_string = env
.static_method_id(class, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;")
.ok_or_else(|| missing("UUID.fromString"))?;
let s = env.new_string(canonical);
Ok(env.call_static_object(class, from_string, &[JValue::object(s)]))
}
pub fn uuid_string(env: Env, object: JObject) -> Option<String> {
if object.is_null() {
return None;
}
let s = call_object(env, object, "toString", "()Ljava/lang/String;", &[]).ok()?;
env.get_string(s).map(|s| s.to_ascii_lowercase())
}
fn parcel_uuid(env: Env, canonical: &str) -> Result<JObject> {
let class = env
.find_class("android/os/ParcelUuid")
.ok_or_else(|| Error::MissingClass("android.os.ParcelUuid".into()))?;
let ctor = env
.method_id(class, "<init>", "(Ljava/util/UUID;)V")
.ok_or_else(|| missing("ParcelUuid.<init>"))?;
let inner = uuid(env, canonical)?;
Ok(env.new_object(class, ctor, &[JValue::object(inner)]))
}
pub fn for_each(env: Env, list: JObject, mut f: impl FnMut(JObject)) {
if list.is_null() {
return;
}
let Ok(size) = call_int(env, list, "size", "()I", &[]) else {
return;
};
for i in 0..size {
if let Ok(item) = call_object(env, list, "get", "(I)Ljava/lang/Object;", &[JValue::int(i)])
{
f(item);
}
}
}
pub struct Adapter {
adapter: Ref,
}
impl Adapter {
pub fn open(runtime: &Runtime) -> Result<Self> {
let env = runtime.env()?;
let context_class = env
.find_class("android/content/Context")
.ok_or_else(|| Error::MissingClass("android.content.Context".into()))?;
let field = env
.static_field_id(context_class, "BLUETOOTH_SERVICE", "Ljava/lang/String;")
.ok_or_else(|| missing("Context.BLUETOOTH_SERVICE"))?;
let name = env.static_object_field(context_class, field);
let manager = call_object(
env,
runtime.context(),
"getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;",
&[JValue::object(name)],
)?;
if manager.is_null() {
return Err(Error::MissingClass(
"BluetoothManager (no Bluetooth on this device)".into(),
));
}
let adapter = call_object(
env,
manager,
"getAdapter",
"()Landroid/bluetooth/BluetoothAdapter;",
&[],
)?;
let adapter = Ref::new(env, adapter)
.ok_or_else(|| Error::MissingClass("BluetoothAdapter (none present)".into()))?;
Ok(Self { adapter })
}
pub fn as_ptr(&self) -> JObject {
self.adapter.as_ptr()
}
pub fn is_enabled(&self, env: Env) -> bool {
call_bool(env, self.as_ptr(), "isEnabled", "()Z", &[]).unwrap_or(false)
}
pub fn name(&self, env: Env) -> Option<String> {
let name = call_object(env, self.as_ptr(), "getName", "()Ljava/lang/String;", &[]).ok()?;
env.get_string(name)
}
pub fn address(&self, env: Env) -> Option<String> {
let address = call_object(
env,
self.as_ptr(),
"getAddress",
"()Ljava/lang/String;",
&[],
)
.ok()?;
env.get_string(address)
}
pub fn scanner(&self, env: Env) -> Result<JObject> {
call_object(
env,
self.as_ptr(),
"getBluetoothLeScanner",
"()Landroid/bluetooth/le/BluetoothLeScanner;",
&[],
)
}
pub fn remote_device(&self, env: Env, address: &str) -> Result<JObject> {
let address = env.new_string(address);
call_object(
env,
self.as_ptr(),
"getRemoteDevice",
"(Ljava/lang/String;)Landroid/bluetooth/BluetoothDevice;",
&[JValue::object(address)],
)
}
pub fn bonded_device_addresses(&self, env: Env) -> Result<Vec<String>> {
let bonded = call_object(
env,
self.as_ptr(),
"getBondedDevices",
"()Ljava/util/Set;",
&[],
)?;
let iterator = call_object(env, bonded, "iterator", "()Ljava/util/Iterator;", &[])?;
let iterator_class = env.get_object_class(iterator);
let has_next = env
.method_id(iterator_class, "hasNext", "()Z")
.ok_or_else(|| Error::MissingMethod("Iterator.hasNext".into()))?;
let next = env
.method_id(iterator_class, "next", "()Ljava/lang/Object;")
.ok_or_else(|| Error::MissingMethod("Iterator.next".into()))?;
let mut addresses = Vec::new();
while env.call_bool(iterator, has_next, &[]) {
let device = env.call_object(iterator, next, &[]);
if let Some(address) = device_address(env, device) {
addresses.push(address);
}
}
Ok(addresses)
}
pub fn device_service_uuids(&self, env: Env, device: JObject) -> Result<Vec<String>> {
let uuids = call_object(env, device, "getUuids", "()[Landroid/os/ParcelUuid;", &[])?;
if uuids.is_null() {
return Ok(Vec::new());
}
let mut result = Vec::new();
let array = uuids as crate::jni::JArray;
for index in 0..env.array_length(array) {
let parcel = env.object_array_element(array, index);
let uuid = call_object(env, parcel, "getUuid", "()Ljava/util/UUID;", &[])?;
if let Some(value) = uuid_string(env, uuid) {
result.push(value);
}
}
Ok(result)
}
pub fn advertiser(&self, env: Env) -> Result<JObject> {
call_object(
env,
self.as_ptr(),
"getBluetoothLeAdvertiser",
"()Landroid/bluetooth/le/BluetoothLeAdvertiser;",
&[],
)
}
}
pub fn start_scan(
env: Env,
scanner: JObject,
callback: JObject,
service_uuids: &[String],
) -> Result<()> {
let filters = build_scan_filters(env, service_uuids)?;
let settings = build_scan_settings(env)?;
call_void(
env,
scanner,
"startScan",
"(Ljava/util/List;Landroid/bluetooth/le/ScanSettings;Landroid/bluetooth/le/ScanCallback;)V",
&[
JValue::object(filters),
JValue::object(settings),
JValue::object(callback),
],
)
}
pub fn stop_scan(env: Env, scanner: JObject, callback: JObject) -> Result<()> {
call_void(
env,
scanner,
"stopScan",
"(Landroid/bluetooth/le/ScanCallback;)V",
&[JValue::object(callback)],
)
}
fn build_scan_filters(env: Env, service_uuids: &[String]) -> Result<JObject> {
let list_class = env
.find_class("java/util/ArrayList")
.ok_or_else(|| Error::MissingClass("java.util.ArrayList".into()))?;
let ctor = env
.method_id(list_class, "<init>", "()V")
.ok_or_else(|| missing("ArrayList"))?;
let list = env.new_object(list_class, ctor, &[]);
let filter_builder_class = env
.find_class("android/bluetooth/le/ScanFilter$Builder")
.ok_or_else(|| Error::MissingClass("ScanFilter.Builder".into()))?;
let builder_ctor = env
.method_id(filter_builder_class, "<init>", "()V")
.ok_or_else(|| missing("ScanFilter.Builder.<init>"))?;
for canonical in service_uuids {
let builder = env.new_object(filter_builder_class, builder_ctor, &[]);
let parcel = parcel_uuid(env, canonical)?;
call_object(
env,
builder,
"setServiceUuid",
"(Landroid/os/ParcelUuid;)Landroid/bluetooth/le/ScanFilter$Builder;",
&[JValue::object(parcel)],
)?;
let filter = call_object(
env,
builder,
"build",
"()Landroid/bluetooth/le/ScanFilter;",
&[],
)?;
call_bool(
env,
list,
"add",
"(Ljava/lang/Object;)Z",
&[JValue::object(filter)],
)?;
}
Ok(list)
}
fn build_scan_settings(env: Env) -> Result<JObject> {
let class = env
.find_class("android/bluetooth/le/ScanSettings$Builder")
.ok_or_else(|| Error::MissingClass("ScanSettings.Builder".into()))?;
let ctor = env
.method_id(class, "<init>", "()V")
.ok_or_else(|| missing("ScanSettings.Builder.<init>"))?;
let builder = env.new_object(class, ctor, &[]);
call_object(
env,
builder,
"setScanMode",
"(I)Landroid/bluetooth/le/ScanSettings$Builder;",
&[JValue::int(2)],
)?;
call_object(
env,
builder,
"build",
"()Landroid/bluetooth/le/ScanSettings;",
&[],
)
}
pub fn set_preferred_phy(env: Env, gatt: JObject, tx: i32, rx: i32, options: i32) -> Result<()> {
call_void(
env,
gatt,
"setPreferredPhy",
"(III)V",
&[JValue::int(tx), JValue::int(rx), JValue::int(options)],
)
}
pub fn read_phy(env: Env, gatt: JObject) -> Result<()> {
call_void(env, gatt, "readPhy", "()V", &[])
}
pub fn create_bond(env: Env, device: JObject) -> Result<bool> {
call_bool(env, device, "createBond", "()Z", &[])
}
pub fn bond_state(env: Env, device: JObject) -> Result<i32> {
call_int(env, device, "getBondState", "()I", &[])
}
pub fn connect_gatt(
env: Env,
runtime: &Runtime,
device: JObject,
callback: JObject,
) -> Result<JObject> {
call_object(
env,
device,
"connectGatt",
"(Landroid/content/Context;ZLandroid/bluetooth/BluetoothGattCallback;I)Landroid/bluetooth/BluetoothGatt;",
&[
JValue::object(runtime.context()),
JValue::bool(false),
JValue::object(callback),
JValue::int(TRANSPORT_LE),
],
)
}
pub fn discover_services(env: Env, gatt: JObject) -> Result<bool> {
call_bool(env, gatt, "discoverServices", "()Z", &[])
}
pub fn services(env: Env, gatt: JObject) -> Result<JObject> {
call_object(env, gatt, "getServices", "()Ljava/util/List;", &[])
}
pub fn service_characteristics(env: Env, service: JObject) -> Result<JObject> {
call_object(
env,
service,
"getCharacteristics",
"()Ljava/util/List;",
&[],
)
}
pub fn service_included_services(env: Env, service: JObject) -> Result<JObject> {
call_object(
env,
service,
"getIncludedServices",
"()Ljava/util/List;",
&[],
)
}
pub fn characteristic_descriptors(env: Env, characteristic: JObject) -> Result<JObject> {
call_object(
env,
characteristic,
"getDescriptors",
"()Ljava/util/List;",
&[],
)
}
pub fn attribute_uuid(env: Env, attribute: JObject) -> Option<String> {
let uuid = call_object(env, attribute, "getUuid", "()Ljava/util/UUID;", &[]).ok()?;
uuid_string(env, uuid)
}
pub fn characteristic_properties(env: Env, characteristic: JObject) -> i32 {
call_int(env, characteristic, "getProperties", "()I", &[]).unwrap_or(0)
}
pub fn characteristic_value(env: Env, characteristic: JObject) -> Option<Vec<u8>> {
let array = call_object(env, characteristic, "getValue", "()[B", &[]).ok()?;
env.byte_array(array)
}
pub fn read_characteristic(env: Env, gatt: JObject, characteristic: JObject) -> Result<bool> {
call_bool(
env,
gatt,
"readCharacteristic",
"(Landroid/bluetooth/BluetoothGattCharacteristic;)Z",
&[JValue::object(characteristic)],
)
}
pub fn write_characteristic(
env: Env,
gatt: JObject,
characteristic: JObject,
value: &[u8],
write_type: i32,
) -> Result<bool> {
let bytes = env.new_byte_array(value);
if let Some(method) = env.method_id(
env.get_object_class(gatt),
"writeCharacteristic",
"(Landroid/bluetooth/BluetoothGattCharacteristic;[BI)I",
) {
let status = env.call_int(
gatt,
method,
&[
JValue::object(characteristic),
JValue::object(bytes),
JValue::int(write_type),
],
);
return Ok(status == GATT_SUCCESS);
}
call_int(
env,
characteristic,
"setWriteType",
"(I)V",
&[JValue::int(write_type)],
)
.ok();
call_bool(
env,
characteristic,
"setValue",
"([B)Z",
&[JValue::object(bytes)],
)?;
call_bool(
env,
gatt,
"writeCharacteristic",
"(Landroid/bluetooth/BluetoothGattCharacteristic;)Z",
&[JValue::object(characteristic)],
)
}
pub fn set_notify(
env: Env,
gatt: JObject,
characteristic: JObject,
enable: bool,
indicate: bool,
) -> Result<bool> {
call_bool(
env,
gatt,
"setCharacteristicNotification",
"(Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z",
&[JValue::object(characteristic), JValue::bool(enable)],
)?;
let cccd = uuid(env, "00002902-0000-1000-8000-00805f9b34fb")?;
let descriptor = call_object(
env,
characteristic,
"getDescriptor",
"(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattDescriptor;",
&[JValue::object(cccd)],
)?;
if descriptor.is_null() {
return Ok(false);
}
let value: &[u8] = match (enable, indicate) {
(false, _) => &[0x00, 0x00],
(true, false) => &[0x01, 0x00],
(true, true) => &[0x02, 0x00],
};
write_descriptor(env, gatt, descriptor, value)
}
pub fn read_descriptor(env: Env, gatt: JObject, descriptor: JObject) -> Result<bool> {
call_bool(
env,
gatt,
"readDescriptor",
"(Landroid/bluetooth/BluetoothGattDescriptor;)Z",
&[JValue::object(descriptor)],
)
}
pub fn write_descriptor(
env: Env,
gatt: JObject,
descriptor: JObject,
value: &[u8],
) -> Result<bool> {
let bytes = env.new_byte_array(value);
if let Some(method) = env.method_id(
env.get_object_class(gatt),
"writeDescriptor",
"(Landroid/bluetooth/BluetoothGattDescriptor;[B)I",
) {
let status = env.call_int(
gatt,
method,
&[JValue::object(descriptor), JValue::object(bytes)],
);
return Ok(status == GATT_SUCCESS);
}
call_bool(
env,
descriptor,
"setValue",
"([B)Z",
&[JValue::object(bytes)],
)?;
call_bool(
env,
gatt,
"writeDescriptor",
"(Landroid/bluetooth/BluetoothGattDescriptor;)Z",
&[JValue::object(descriptor)],
)
}
pub fn request_mtu(env: Env, gatt: JObject, mtu: i32) -> Result<bool> {
call_bool(env, gatt, "requestMtu", "(I)Z", &[JValue::int(mtu)])
}
pub fn read_remote_rssi(env: Env, gatt: JObject) -> Result<bool> {
call_bool(env, gatt, "readRemoteRssi", "()Z", &[])
}
pub fn request_connection_priority(env: Env, gatt: JObject, priority: i32) -> Result<bool> {
call_bool(
env,
gatt,
"requestConnectionPriority",
"(I)Z",
&[JValue::int(priority)],
)
}
pub fn disconnect(env: Env, gatt: JObject) -> Result<()> {
call_void(env, gatt, "disconnect", "()V", &[])
}
pub fn close(env: Env, gatt: JObject) -> Result<()> {
call_void(env, gatt, "close", "()V", &[])
}
pub fn create_l2cap_channel(env: Env, device: JObject, psm: i32) -> Result<JObject> {
call_object(
env,
device,
"createL2capChannel",
"(I)Landroid/bluetooth/BluetoothSocket;",
&[JValue::int(psm)],
)
}
#[cfg(feature = "classic-l2cap")]
pub fn create_classic_l2cap_socket(env: Env, device: JObject, psm: i32) -> Result<JObject> {
call_object(
env,
device,
"createL2capSocket",
"(I)Landroid/bluetooth/BluetoothSocket;",
&[JValue::int(psm)],
)
}
#[cfg(feature = "rfcomm")]
pub fn create_rfcomm_socket(env: Env, device: JObject, service_uuid: &str) -> Result<JObject> {
let service_uuid = uuid(env, service_uuid)?;
call_object(
env,
device,
"createRfcommSocketToServiceRecord",
"(Ljava/util/UUID;)Landroid/bluetooth/BluetoothSocket;",
&[JValue::object(service_uuid)],
)
}
pub fn socket_connect(env: Env, socket: JObject) -> Result<()> {
call_void(env, socket, "connect", "()V", &[])
}
pub fn socket_close(env: Env, socket: JObject) -> Result<()> {
call_void(env, socket, "close", "()V", &[])
}
pub fn socket_input_stream(env: Env, socket: JObject) -> Result<JObject> {
call_object(
env,
socket,
"getInputStream",
"()Ljava/io/InputStream;",
&[],
)
}
pub fn socket_output_stream(env: Env, socket: JObject) -> Result<JObject> {
call_object(
env,
socket,
"getOutputStream",
"()Ljava/io/OutputStream;",
&[],
)
}
pub fn stream_read(env: Env, stream: JObject, buffer: usize) -> Result<Option<Vec<u8>>> {
let array = env.new_byte_array(&vec![0u8; buffer]);
let n = call_int(env, stream, "read", "([B)I", &[JValue::object(array)])?;
if n < 0 {
return Ok(None);
}
let mut bytes = env.byte_array(array).unwrap_or_default();
bytes.truncate(n as usize);
Ok(Some(bytes))
}
pub fn stream_write(env: Env, stream: JObject, data: &[u8]) -> Result<()> {
let array = env.new_byte_array(data);
call_void(env, stream, "write", "([B)V", &[JValue::object(array)])?;
call_void(env, stream, "flush", "()V", &[])
}
pub struct Callback {
object: Ref,
}
impl Callback {
pub fn new(
runtime: &Runtime,
binary_name: &str,
dex: Vec<u8>,
natives: &[(&str, &str, *const std::ffi::c_void)],
sink: Arc<dyn EventSink>,
) -> Result<Self> {
let class = runtime.define_class(binary_name, dex, natives)?;
let object = runtime.new_instance(class)?;
let env = runtime.env()?;
let object = Ref::new(env, object)
.ok_or_else(|| Error::ClassLoad(format!("{binary_name} produced a null instance")))?;
crate::bluetooth::bind_sink(&object, sink);
Ok(Self { object })
}
pub fn as_ptr(&self) -> JObject {
self.object.as_ptr()
}
}
impl Drop for Callback {
fn drop(&mut self) {
crate::bluetooth::unbind_sink(&self.object);
}
}
pub struct Discard;
impl EventSink for Discard {
fn emit(&self, _: Event) {}
}
const SERVICE_TYPE_PRIMARY: i32 = 0;
const SERVICE_TYPE_SECONDARY: i32 = 1;
pub const ATT_SUCCESS: i32 = 0;
pub fn permissions_to_android(permissions: u32) -> i32 {
const READABLE: u32 = 0x01;
const WRITEABLE: u32 = 0x02;
const READ_ENCRYPTED: u32 = 0x04;
const WRITE_ENCRYPTED: u32 = 0x08;
let mut out = 0;
if permissions & READABLE != 0 {
out |= 1; }
if permissions & READ_ENCRYPTED != 0 {
out |= 2; }
if permissions & WRITEABLE != 0 {
out |= 16; }
if permissions & WRITE_ENCRYPTED != 0 {
out |= 32; }
out
}
pub fn open_gatt_server(env: Env, runtime: &Runtime, callback: JObject) -> Result<JObject> {
let context_class = env
.find_class("android/content/Context")
.ok_or_else(|| Error::MissingClass("android.content.Context".into()))?;
let field = env
.static_field_id(context_class, "BLUETOOTH_SERVICE", "Ljava/lang/String;")
.ok_or_else(|| missing("Context.BLUETOOTH_SERVICE"))?;
let name = env.static_object_field(context_class, field);
let manager = call_object(
env,
runtime.context(),
"getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;",
&[JValue::object(name)],
)?;
call_object(
env,
manager,
"openGattServer",
"(Landroid/content/Context;Landroid/bluetooth/BluetoothGattServerCallback;)Landroid/bluetooth/BluetoothGattServer;",
&[JValue::object(runtime.context()), JValue::object(callback)],
)
}
pub fn new_service(env: Env, canonical: &str, primary: bool) -> Result<JObject> {
let class = env
.find_class("android/bluetooth/BluetoothGattService")
.ok_or_else(|| Error::MissingClass("BluetoothGattService".into()))?;
let ctor = env
.method_id(class, "<init>", "(Ljava/util/UUID;I)V")
.ok_or_else(|| missing("BluetoothGattService.<init>"))?;
let uuid = uuid(env, canonical)?;
let kind = if primary {
SERVICE_TYPE_PRIMARY
} else {
SERVICE_TYPE_SECONDARY
};
Ok(env.new_object(class, ctor, &[JValue::object(uuid), JValue::int(kind)]))
}
pub fn new_characteristic(
env: Env,
canonical: &str,
properties: u32,
permissions: u32,
) -> Result<JObject> {
let class = env
.find_class("android/bluetooth/BluetoothGattCharacteristic")
.ok_or_else(|| Error::MissingClass("BluetoothGattCharacteristic".into()))?;
let ctor = env
.method_id(class, "<init>", "(Ljava/util/UUID;II)V")
.ok_or_else(|| missing("BluetoothGattCharacteristic.<init>"))?;
let uuid = uuid(env, canonical)?;
Ok(env.new_object(
class,
ctor,
&[
JValue::object(uuid),
JValue::int(properties as i32),
JValue::int(permissions_to_android(permissions)),
],
))
}
pub fn new_descriptor(env: Env, canonical: &str, permissions: u32) -> Result<JObject> {
let class = env
.find_class("android/bluetooth/BluetoothGattDescriptor")
.ok_or_else(|| Error::MissingClass("BluetoothGattDescriptor".into()))?;
let ctor = env
.method_id(class, "<init>", "(Ljava/util/UUID;I)V")
.ok_or_else(|| missing("BluetoothGattDescriptor.<init>"))?;
let uuid = uuid(env, canonical)?;
Ok(env.new_object(
class,
ctor,
&[
JValue::object(uuid),
JValue::int(permissions_to_android(permissions)),
],
))
}
pub fn service_add_characteristic(
env: Env,
service: JObject,
characteristic: JObject,
) -> Result<bool> {
call_bool(
env,
service,
"addCharacteristic",
"(Landroid/bluetooth/BluetoothGattCharacteristic;)Z",
&[JValue::object(characteristic)],
)
}
pub fn characteristic_add_descriptor(
env: Env,
characteristic: JObject,
descriptor: JObject,
) -> Result<bool> {
call_bool(
env,
characteristic,
"addDescriptor",
"(Landroid/bluetooth/BluetoothGattDescriptor;)Z",
&[JValue::object(descriptor)],
)
}
pub fn set_value(env: Env, attribute: JObject, value: &[u8]) -> Result<bool> {
let bytes = env.new_byte_array(value);
call_bool(
env,
attribute,
"setValue",
"([B)Z",
&[JValue::object(bytes)],
)
}
pub fn server_add_service(env: Env, server: JObject, service: JObject) -> Result<bool> {
call_bool(
env,
server,
"addService",
"(Landroid/bluetooth/BluetoothGattService;)Z",
&[JValue::object(service)],
)
}
pub fn server_remove_service(env: Env, server: JObject, service: JObject) -> Result<bool> {
call_bool(
env,
server,
"removeService",
"(Landroid/bluetooth/BluetoothGattService;)Z",
&[JValue::object(service)],
)
}
pub fn server_clear_services(env: Env, server: JObject) -> Result<()> {
call_void(env, server, "clearServices", "()V", &[])
}
pub fn server_close(env: Env, server: JObject) -> Result<()> {
call_void(env, server, "close", "()V", &[])
}
pub fn server_send_response(
env: Env,
server: JObject,
device: JObject,
request_id: i32,
status: i32,
offset: i32,
value: &[u8],
) -> Result<bool> {
let bytes = env.new_byte_array(value);
call_bool(
env,
server,
"sendResponse",
"(Landroid/bluetooth/BluetoothDevice;III[B)Z",
&[
JValue::object(device),
JValue::int(request_id),
JValue::int(status),
JValue::int(offset),
JValue::object(bytes),
],
)
}
pub fn server_notify(
env: Env,
server: JObject,
device: JObject,
characteristic: JObject,
confirm: bool,
value: &[u8],
) -> Result<bool> {
let bytes = env.new_byte_array(value);
if let Some(method) = env.method_id(
env.get_object_class(server),
"notifyCharacteristicChanged",
"(Landroid/bluetooth/BluetoothDevice;Landroid/bluetooth/BluetoothGattCharacteristic;Z[B)I",
) {
let status = env.call_int(
server,
method,
&[
JValue::object(device),
JValue::object(characteristic),
JValue::bool(confirm),
JValue::object(bytes),
],
);
return Ok(status == ATT_SUCCESS);
}
set_value(env, characteristic, value)?;
call_bool(
env,
server,
"notifyCharacteristicChanged",
"(Landroid/bluetooth/BluetoothDevice;Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z",
&[
JValue::object(device),
JValue::object(characteristic),
JValue::bool(confirm),
],
)
}
pub fn server_connected_devices(env: Env, runtime: &Runtime) -> Result<JObject> {
let context_class = env
.find_class("android/content/Context")
.ok_or_else(|| Error::MissingClass("android.content.Context".into()))?;
let field = env
.static_field_id(context_class, "BLUETOOTH_SERVICE", "Ljava/lang/String;")
.ok_or_else(|| missing("Context.BLUETOOTH_SERVICE"))?;
let name = env.static_object_field(context_class, field);
let manager = call_object(
env,
runtime.context(),
"getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;",
&[JValue::object(name)],
)?;
call_object(
env,
manager,
"getConnectedDevices",
"(I)Ljava/util/List;",
&[JValue::int(8)],
)
}
pub fn device_address(env: Env, device: JObject) -> Option<String> {
let s = call_object(env, device, "getAddress", "()Ljava/lang/String;", &[]).ok()?;
env.get_string(s)
}
#[cfg(feature = "classic")]
pub fn start_classic_discovery(env: Env, adapter: JObject) -> Result<()> {
if call_bool(env, adapter, "startDiscovery", "()Z", &[])? {
Ok(())
} else {
Err(Error::MissingMethod(
"BluetoothAdapter.startDiscovery returned false".into(),
))
}
}
#[cfg(feature = "classic")]
pub fn cancel_classic_discovery(env: Env, adapter: JObject) -> Result<()> {
call_bool(env, adapter, "cancelDiscovery", "()Z", &[]).map(|_| ())
}
#[cfg(feature = "classic")]
pub fn classic_discovery_filter(env: Env) -> Result<JObject> {
let class = env
.find_class("android/content/IntentFilter")
.ok_or_else(|| Error::MissingClass("android.content.IntentFilter".into()))?;
let ctor = env
.method_id(class, "<init>", "()V")
.ok_or_else(|| missing("IntentFilter.<init>"))?;
let filter = env.new_object(class, ctor, &[]);
for action in [
"android.bluetooth.device.action.FOUND",
"android.bluetooth.adapter.action.DISCOVERY_STARTED",
"android.bluetooth.adapter.action.DISCOVERY_FINISHED",
] {
let action = env.new_string(action);
call_void(
env,
filter,
"addAction",
"(Ljava/lang/String;)V",
&[JValue::object(action)],
)?;
}
Ok(filter)
}
#[cfg(feature = "classic")]
pub fn register_classic_receiver(env: Env, receiver: JObject, filter: JObject) -> Result<()> {
call_object(
env,
Runtime::get().ok_or(Error::NoContext)?.context(),
"registerReceiver",
"(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;",
&[JValue::object(receiver), JValue::object(filter)],
)
.map(|_| ())
}
#[cfg(feature = "classic")]
pub fn unregister_classic_receiver(env: Env, receiver: JObject) -> Result<()> {
call_void(
env,
Runtime::get().ok_or(Error::NoContext)?.context(),
"unregisterReceiver",
"(Landroid/content/BroadcastReceiver;)V",
&[JValue::object(receiver)],
)
}
pub fn start_advertising(
env: Env,
advertiser: JObject,
callback: JObject,
include_name: bool,
service_uuids: &[String],
connectable: bool,
mode: i32,
) -> Result<()> {
let settings = build_advertise_settings(env, connectable, mode)?;
let data = build_advertise_data(env, include_name, service_uuids)?;
call_void(
env,
advertiser,
"startAdvertising",
concat!(
"(Landroid/bluetooth/le/AdvertiseSettings;",
"Landroid/bluetooth/le/AdvertiseData;",
"Landroid/bluetooth/le/AdvertiseCallback;)V"
),
&[
JValue::object(settings),
JValue::object(data),
JValue::object(callback),
],
)
}
pub fn stop_advertising(env: Env, advertiser: JObject, callback: JObject) -> Result<()> {
call_void(
env,
advertiser,
"stopAdvertising",
"(Landroid/bluetooth/le/AdvertiseCallback;)V",
&[JValue::object(callback)],
)
}
pub fn advertise_mode(max_interval: Option<std::time::Duration>) -> i32 {
match max_interval.map(|d| d.as_millis()) {
None => 2,
Some(ms) if ms <= 175 => 2,
Some(ms) if ms <= 500 => 1,
Some(_) => 0,
}
}
fn build_advertise_settings(env: Env, connectable: bool, mode: i32) -> Result<JObject> {
let class = env
.find_class("android/bluetooth/le/AdvertiseSettings$Builder")
.ok_or_else(|| Error::MissingClass("AdvertiseSettings.Builder".into()))?;
let ctor = env
.method_id(class, "<init>", "()V")
.ok_or_else(|| missing("AdvertiseSettings.Builder.<init>"))?;
let builder = env.new_object(class, ctor, &[]);
call_object(
env,
builder,
"setAdvertiseMode",
"(I)Landroid/bluetooth/le/AdvertiseSettings$Builder;",
&[JValue::int(mode)],
)?;
call_object(
env,
builder,
"setConnectable",
"(Z)Landroid/bluetooth/le/AdvertiseSettings$Builder;",
&[JValue::bool(connectable)],
)?;
call_object(
env,
builder,
"build",
"()Landroid/bluetooth/le/AdvertiseSettings;",
&[],
)
}
fn build_advertise_data(env: Env, include_name: bool, service_uuids: &[String]) -> Result<JObject> {
let class = env
.find_class("android/bluetooth/le/AdvertiseData$Builder")
.ok_or_else(|| Error::MissingClass("AdvertiseData.Builder".into()))?;
let ctor = env
.method_id(class, "<init>", "()V")
.ok_or_else(|| missing("AdvertiseData.Builder.<init>"))?;
let builder = env.new_object(class, ctor, &[]);
call_object(
env,
builder,
"setIncludeDeviceName",
"(Z)Landroid/bluetooth/le/AdvertiseData$Builder;",
&[JValue::bool(include_name)],
)?;
for canonical in service_uuids {
let parcel = parcel_uuid(env, canonical)?;
call_object(
env,
builder,
"addServiceUuid",
"(Landroid/os/ParcelUuid;)Landroid/bluetooth/le/AdvertiseData$Builder;",
&[JValue::object(parcel)],
)?;
}
call_object(
env,
builder,
"build",
"()Landroid/bluetooth/le/AdvertiseData;",
&[],
)
}
pub fn listen_rfcomm(
env: Env,
adapter: JObject,
name: &str,
service_uuid: &str,
) -> Result<JObject> {
let service_uuid = uuid(env, service_uuid)?;
let name = env.new_string(name);
call_object(
env,
adapter,
"listenUsingRfcommWithServiceRecord",
"(Ljava/lang/String;Ljava/util/UUID;)Landroid/bluetooth/BluetoothServerSocket;",
&[JValue::object(name), JValue::object(service_uuid)],
)
}
pub fn listen_l2cap(env: Env, adapter: JObject, secure: bool) -> Result<JObject> {
let name = if secure {
"listenUsingL2capChannel"
} else {
"listenUsingInsecureL2capChannel"
};
call_object(
env,
adapter,
name,
"()Landroid/bluetooth/BluetoothServerSocket;",
&[],
)
}
pub fn server_socket_psm(env: Env, socket: JObject) -> Result<i32> {
call_int(env, socket, "getPsm", "()I", &[])
}
pub fn server_socket_accept(env: Env, socket: JObject) -> Result<JObject> {
call_object(
env,
socket,
"accept",
"()Landroid/bluetooth/BluetoothSocket;",
&[],
)
}
pub fn server_socket_close(env: Env, socket: JObject) -> Result<()> {
call_void(env, socket, "close", "()V", &[])
}