Discover and connect to devices with Bluetooth Classic, BLE, NFC, Wi-Fi, DNS-SD, TCP and UDP. See the AndroidAPI method index for the event format and other method groups.
Pass JSON arguments as strings with JSON.stringify(). Register event listeners before calling the method. When a result contains callbackId, match it to the request.
Bluetooth Classic and BLE
Use Classic for already paired serial devices and BLE for GATT peripherals. On Android 12+, declare Bluetooth Connect for connections and Bluetooth Scan for scanning. On older Android versions, BLE scanning uses location permission. Use full 128-bit UUIDs. Serialize GATT operations per device and check error before proceeding.
| Call | Return or event | Project permission | Usage |
|---|---|---|---|
AndroidAPI.bluetoothState() |
— | — | Return the adapter state as on, off or unsupported. |
AndroidAPI.requestEnableBluetooth(callbackId) |
bluetoothEnableResult |
BLUETOOTH_CONNECT |
Open Android’s Bluetooth enable prompt. |
AndroidAPI.btBondedDevices(callbackId) |
btBondedResult |
BLUETOOTH_CONNECT |
List paired Classic devices in devices with id, name and type. |
AndroidAPI.btConnect(address, uuid, callbackId) |
btConnection |
BLUETOOTH_CONNECT |
Open a Classic socket. An empty UUID uses Serial Port Profile. Incoming bytes arrive as Base64 in btData.value. |
AndroidAPI.btDisconnect(address) |
btConnection |
BLUETOOTH_CONNECT |
Close the Classic socket for the address. |
AndroidAPI.btWrite(address, base64, callbackId) |
btWriteResult |
BLUETOOTH_CONNECT |
Write Base64-encoded bytes to a Classic socket. |
AndroidAPI.bleStartScan(optionsJson, callbackId) |
bleDevice, bleScanResult |
BLUETOOTH_SCAN |
Scan with JSON filters services, namePrefix and timeoutMs (up to 30,000). Each found device includes id, name and rssi. |
AndroidAPI.bleStopScan() |
bleScanResult |
BLUETOOTH_SCAN |
End the active scan early. |
AndroidAPI.bleConnect(address, callbackId) |
bleConnection |
BLUETOOTH_CONNECT |
Connect to a BLE device using the ID from the scan. |
AndroidAPI.bleDisconnect(address) |
bleConnection |
BLUETOOTH_CONNECT |
Close the BLE connection. |
AndroidAPI.bleDiscover(address, callbackId) |
bleServices |
BLUETOOTH_CONNECT |
Discover services and characteristics, including each characteristic’s properties. |
AndroidAPI.bleRead(address, service, characteristic, callbackId) |
bleReadResult |
BLUETOOTH_CONNECT |
Read the characteristic’s Base64 value. |
AndroidAPI.bleWrite(address, service, characteristic, base64, withResponse, callbackId) |
bleWriteResult |
BLUETOOTH_CONNECT |
Write Base64 bytes. Choose withResponse according to the characteristic’s properties. |
AndroidAPI.bleSubscribe(address, service, characteristic, callbackId) |
bleSubscribeResult |
BLUETOOTH_CONNECT |
Enable notifications or indications. Updates arrive as bleNotify with id, service, characteristic and Base64 value. |
AndroidAPI.bleUnsubscribe(address, service, characteristic, callbackId) |
bleSubscribeResult |
BLUETOOTH_CONNECT |
Stop notifications for the characteristic. |
AndroidAPI.bleRequestMtu(address, mtu, callbackId) |
bleMtuResult |
BLUETOOTH_CONNECT |
Request an MTU and read the negotiated value from the result. |
window.addEventListener('bleDevice', ({ detail }) => {
if (detail.callbackId === 'sensor-scan') console.log(detail.id, detail.name);
});
window.addEventListener('bleScanResult', ({ detail }) => {
if (detail.callbackId === 'sensor-scan' && !detail.success) console.error(detail.error);
});
AndroidAPI.bleStartScan(JSON.stringify({ timeoutMs: 5000 }), 'sensor-scan');
Wi-Fi and local network
Use these methods with devices on your local network. TCP and UDP destinations are restricted to private addresses. If Android requests local-network access, handle not_declared, permission_denied and permission_permanently_denied before trying again. Treat TCP data as a byte stream and parse message boundaries in JavaScript.
| Call | Return or event | Project permission | Usage |
|---|---|---|---|
AndroidAPI.getWifiInfo() |
— | ACCESS_NETWORK_STATE, ACCESS_WIFI_STATE |
Read Wi-Fi connection details as JSON. Android can redact identifying fields. |
AndroidAPI.nsdDiscover(type, callbackId) |
nsdService, nsdResult |
CHANGE_WIFI_MULTICAST_STATE, ACCESS_LOCAL_NETWORK |
Discover DNS-SD services such as _http._tcp. Service events carry the resolved host and port. |
AndroidAPI.nsdStop(callbackId) |
nsdResult |
— | Stop service discovery. |
AndroidAPI.tcpConnect(host, port, callbackId) |
tcpConnection |
INTERNET, ACCESS_LOCAL_NETWORK |
Open a TCP connection. Use the returned id for writes and close. Read incoming Base64 data from tcpData. |
AndroidAPI.tcpSend(id, base64, callbackId) |
tcpSendResult |
— | Send Base64 bytes on an open connection. |
AndroidAPI.tcpClose(id) |
tcpConnection |
— | Close a connection by ID. |
AndroidAPI.udpSend(host, port, base64, callbackId) |
udpSendResult |
INTERNET, ACCESS_LOCAL_NETWORK |
Send one Base64 datagram to a private host and port. |
AndroidAPI.udpListen(port, callbackId) |
udpListenResult |
INTERNET, ACCESS_LOCAL_NETWORK |
Listen on a port. Incoming datagrams arrive as udpData events. |
AndroidAPI.udpClose(port) |
udpClosed |
— | Close the listener for a port. |
window.addEventListener('nsdService', ({ detail }) => {
if (detail.callbackId === 'discover') console.log(detail.host, detail.port);
});
AndroidAPI.nsdDiscover('_http._tcp', 'discover');
NFC tags
Enable NFC in the project for reading and writing, then bring a tag close to the phone while the app is on screen. Reading is free. Since builder 1.36, nfcWriteNdef requires Pro. APKs built with earlier releases can still write in free builds. A write waits up to 60 seconds and can fail with read_only, too_large, not_ndef or timeout.
| Call | Return or event | Project permission | Usage |
|---|---|---|---|
AndroidAPI.nfcState() |
— | — | Return unsupported, off or on. |
AndroidAPI.nfcStartReading(optionsJson, callbackId) |
nfcReadResult, nfcTag |
NFC |
Start reading. Tag events supply the tag ID, technologies and available NDEF data. |
AndroidAPI.nfcStopReading(callbackId) |
nfcReadResult |
— | Stop tag reading. |
AndroidAPI.nfcWriteNdef(recordsJson, optionsJson, callbackId) |
nfcWriteResult |
NFC |
Pro. A free build from 1.36 on reports pro_required in nfcWriteResult. Write an array of NDEF records to the next tag. For text, use [{type: "text", text: "Hello"}] as JSON. |
AndroidAPI.nfcCancelWrite(callbackId) |
nfcWriteResult |
— | Cancel a pending write. |
window.addEventListener('nfcTag', ({ detail }) => console.log(detail));
if (AndroidAPI.nfcState() === 'on') AndroidAPI.nfcStartReading('{}', 'read-tag');