The BluetoothCommunicationPlugin is a Capacitor plugin that provides Bluetooth Classic communication functionality. It supports managing Bluetooth devices (scanning, connecting, and disconnecting), as well as sending and receiving data over Bluetooth RFCOMM sockets. Web-specific implementations are available as stubs for testing purposes. Note: This plugin currently supports Android only.
- Enable Bluetooth: Enable Bluetooth on the device.
- Scan Devices: Scan for paired Bluetooth devices.
- Start/Stop Server: Start and stop a Bluetooth RFCOMM server for device connections.
- Connect/Disconnect: Connect to a Bluetooth device and disconnect from it.
- Send Data: Send data to the connected Bluetooth device.
- Listen for Incoming Data: Listen for incoming data from the connected Bluetooth device.
- Android: Full functionality (Bluetooth scanning, connection, data transfer).
- Web: Stub implementation (for testing purposes, Bluetooth functionality is not supported on the web).
To install the plugin, run the following command:
npm install @yesprasoon/capacitor-bluetooth-communication
npx cap sync
- Android: Fully supported
- Web: Stubbed support (Bluetooth is not available on the web)
- iOS: Not supported by this plugin
This plugin requires Bluetooth and location permissions:
-
Bluetooth:
BLUETOOTH
BLUETOOTH_ADMIN
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
-
Bluetooth Scan:
BLUETOOTH_SCAN
BLUETOOTH_CONNECT
For the web, Bluetooth functionalities are stubbed and not available.
To initialize the plugin, call the initialize
method:
import { BluetoothCommunication } from '@yesprasoon/capacitor-bluetooth-communication';
BluetoothCommunication.initialize()
.then(() => console.log('Bluetooth initialized'))
.catch(error => console.error('Error initializing Bluetooth:', error));
To enable Bluetooth on the device:
BluetoothCommunication.enableBluetooth()
.then(() => console.log('Bluetooth enabled'))
.catch(error => console.error('Error enabling Bluetooth:', error));
To scan for paired Bluetooth devices:
BluetoothCommunication.scanDevices()
.then(result => {
const devices = result.devices;
console.log('Found devices:', devices);
})
.catch(error => console.error('Error scanning devices:', error));
To start the Bluetooth server:
BluetoothCommunication.startServer()
.then(() => console.log('Server started'))
.catch(error => console.error('Error starting server:', error));
To stop the Bluetooth server:
BluetoothCommunication.stopServer()
.then(() => console.log('Server stopped'))
.catch(error => console.error('Error stopping server:', error));
To connect to a Bluetooth device using its MAC address:
const address = 'XX:XX:XX:XX:XX:XX'; // Replace with the device address
BluetoothCommunication.connect({ address })
.then(() => console.log('Connected to device'))
.catch(error => console.error('Error connecting to device:', error));
To disconnect from the currently connected Bluetooth device:
BluetoothCommunication.disconnect()
.then(() => console.log('Disconnected'))
.catch(error => console.error('Error disconnecting:', error));
To send data to the connected Bluetooth device:
const data = 'Hello, Bluetooth!';
BluetoothCommunication.sendData({ data })
.then(() => console.log('Data sent'))
.catch(error => console.error('Error sending data:', error));
To listen for incoming data:
BluetoothCommunication.addListener('dataReceived', (event) => {
console.log('Data received:', event.data);
});
On the web, Bluetooth functionalities are stubbed. You can still call methods, but they will not work in a web environment:
BluetoothCommunication.enableBluetooth() // Throws unavailable error
BluetoothCommunication.scanDevices() // Returns empty list
BluetoothCommunication.startServer() // Throws unavailable error
Here’s an example of a simple use case in an Ionic/Angular app:
- Initialize the Plugin: Set up Bluetooth on the device.
- Enable Bluetooth: Ensure Bluetooth is enabled on the device.
- Start Listening for Connections: Start the server to listen for incoming client connections.
- Handle Data Reception: Use an event listener to receive data from the client.
import { BluetoothCommunication } from '@yesprasoon/capacitor-bluetooth-communication';
// Initialize the Bluetooth plugin
await BluetoothCommunication.initialize();
// Enable Bluetooth if it's not already enabled
await BluetoothCommunication.enableBluetooth();
// Start the server to listen for incoming connections
await BluetoothCommunication.startServer();
// Listen for incoming data from connected clients
BluetoothCommunication.addListener('dataReceived', (data) => {
console.log('Received data:', data);
});
- Initialize the Plugin: Set up Bluetooth on the device.
- Enable Bluetooth: Ensure Bluetooth is enabled on the device.
- Start Listening for Connections: Start the server to listen for incoming client connections.
- Scan for Devices: Search for available paired devices.
- Connect to a Device: Select a device and connect.
- Handle Data Reception: Use an event listener to receive data from the client.
import { BluetoothCommunication } from '@yesprasoon/capacitor-bluetooth-communication';
// Initialize the Bluetooth plugin
await BluetoothCommunication.initialize();
// Enable Bluetooth if it's not already enabled
await BluetoothCommunication.enableBluetooth();
// Scan for paired devices
const result = await BluetoothCommunication.scanDevices();
const device = result.devices[0]; // Select the first device (or whichever you prefer)
// Connect to the selected device
await BluetoothCommunication.connect({ address: device.address });
// Listen for incoming data from the server
BluetoothCommunication.addListener('dataReceived', (data) => {
console.log('Received data:', data);
});
This project is licensed under the MIT License. See the LICENSE file for more details.