A Capacitor plugin for client-to-server communication over Bluetooth Classic on Android.
npm install @yesprasoon/capacitor-bluetooth-communicationThe 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.
---
To install the plugin, run the following command:
``bash`
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:
- BLUETOOTHBLUETOOTH_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:
`typescript
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:
`typescript`
BluetoothCommunication.enableBluetooth()
.then(() => console.log('Bluetooth enabled'))
.catch(error => console.error('Error enabling Bluetooth:', error));
To scan for paired Bluetooth devices:
`typescript`
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:
`typescript`
BluetoothCommunication.startServer()
.then(() => console.log('Server started'))
.catch(error => console.error('Error starting server:', error));
To stop the Bluetooth server:
`typescript`
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:
`typescript`
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:
`typescript`
BluetoothCommunication.disconnect()
.then(() => console.log('Disconnected'))
.catch(error => console.error('Error disconnecting:', error));
To send data to the connected Bluetooth device:
`typescript`
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:
`typescript`
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:
`typescript`
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:
1. Initialize the Plugin: Set up Bluetooth on the device.
2. Enable Bluetooth: Ensure Bluetooth is enabled on the device.
3. Start Listening for Connections: Start the server to listen for incoming client connections.
4. Handle Data Reception: Use an event listener to receive data from the client.
`typescript
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);
});
`
1. Initialize the Plugin: Set up Bluetooth on the device.
2. Enable Bluetooth: Ensure Bluetooth is enabled on the device.
3. Start Listening for Connections: Start the server to listen for incoming client connections.
4. Scan for Devices: Search for available paired devices.
5. Connect to a Device: Select a device and connect.
6. Handle Data Reception: Use an event listener to receive data from the client.
`typescript
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.