Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
bluetooth-serial
Advanced tools
Use classic bluetooth functionality.
How this plugin is different from it's source:
For things to work, you need to add certain permissions into your AndroidManifest. Refer to this for a detailed description: https://developer.android.com/guide/topics/connectivity/bluetooth/permissions
The plugin requests/checks permissions in a granular fashion; a call that only requires the CONNECT permission will only check/request that permission. As opposed to every call that requires any permission, requesting all of them.
npm install bluetooth-serial
npx cap sync
echo(...)
connect(...)
connectInsecure(...)
disconnect()
read()
write(...)
available()
isEnabled()
isConnected()
clear()
enable()
settings()
list()
discoverUnpaired()
cancelDiscovery()
checkPermissions()
requestPermissions(...)
addListener('discoverUnpaired', ...)
addListener('connectionChange', ...)
removeAllListeners()
echo(options: { value: string; }) => Promise<{ value: string; }>
Param | Type |
---|---|
options | { value: string; } |
Returns: Promise<{ value: string; }>
connect(options: connectionOptions) => Promise<void>
Creates a secure connection (https://developer.android.com/reference/android/bluetooth/BluetoothDevice#createRfcommSocketToServiceRecord(java.util.UUID)) to the bluetooth device with the given address. The plugin only retains one connection at a time; upon connecting to a device, while there is already an existing connection, the previous device is disconnected. If there is already a running connect call that hasn't resolved, and a new one starts, the original will reject with "Connection interrupted". Requires CONNECT permission on Android API >= 30
Param | Type |
---|---|
options | connectionOptions |
connectInsecure(options: connectionOptions) => Promise<void>
Creates an insecure connection (https://developer.android.com/reference/android/bluetooth/BluetoothDevice#createInsecureRfcommSocketToServiceRecord(java.util.UUID)) to the bluetooth device with the given address. The plugin only retains one connection at a time; upon connecting to a device, while there is already an existing connection, the previous device is disconnected. If there is already a running connect call that hasn't resolved, and a new one starts, the original will reject with "Connection interrupted". Requires CONNECT permission on Android API >= 30
Param | Type |
---|---|
options | connectionOptions |
disconnect() => Promise<void>
Disconnects from the currently connected device. This may be called while there is no connected device; in that case, the method will resolve with void.
read() => Promise<{ data: string; }>
Returns data emitted from the currently connected device.
Returns: Promise<{ data: string; }>
write(options: { data: string; }) => Promise<void>
Writes data to the currently connected device.
Param | Type |
---|---|
options | { data: string; } |
available() => Promise<{ available: number; }>
Returns the length of the data that can be read by calling read().
Returns: Promise<{ available: number; }>
isEnabled() => Promise<{ isEnabled: boolean; }>
Returns true or false depending on whether bluetooth is enabled.
Returns: Promise<{ isEnabled: boolean; }>
isConnected() => Promise<{ isConnected: boolean; }>
Returns true or false depending on whether the plugin is currently connected to a device.
Returns: Promise<{ isConnected: boolean; }>
clear() => Promise<void>
Clears the data readable by calling read().
enable() => Promise<{ isEnabled: boolean; }>
Displays the native prompt for enabling bluetooth. Returns true or false depending on whether the user enabled bluetooth. Requires CONNECT permission on Android API >= 30
Returns: Promise<{ isEnabled: boolean; }>
settings() => Promise<void>
Opens the native bluetooth settings activity. Resolves immediately upon being called.
list() => Promise<devices>
Returns a list of bonded devices. This includes devices that were previously paired with the user's device Requires CONNECT permission on Android API >= 30
Returns: Promise<devices>
discoverUnpaired() => Promise<devices>
Begins the discovery of nearby devices and resolves with them once discovery is finished. There may only be one discovery process at a time. If another call starts while there is a discovery in progress, the original call will resolve with "Discovery cancelled".
On Android API >= 30 requires SCAN, CONNECT and FINE_LOCATION permissions. You can declare in your manifest that scanning for devices is not used to derive the user's location. In that case, you may also add the following into your capacitor.config.ts to indicate that the plugin should not require FINE_LOCATION:
BluetoothSerial: { neverScanForLocation: true, }
In that case, only SCAN and CONNECT are required.
On Android 10 and 11, only FINE_LOCATION is required.
On lower versions, only COARSE_LOCATION is required.
The versions of Android that require location permissions, also require location services to be enabled. So this plugin will reject with "Location services not enabled" if the device requires location for scanning, but it is disabled.
https://developer.android.com/guide/topics/connectivity/bluetooth/permissions
Returns: Promise<devices>
cancelDiscovery() => Promise<void>
Cancels current unpaired devices discovery, if there is one in progress. If there is no discovery in progress, resolves with void. Be sure to note that calling this will reject any existing discoverUnpaired() call which hasn't resolved yet. Requires SCAN permission on Android API >= 30
checkPermissions() => Promise<PermissionStatus[]>
Takes into account the fact that SCAN and CONNECT permissions only exist on Android 11+; those permissions will always resolve as GRANTED on devices below Android 11.
Returns: Promise<PermissionStatus[]>
requestPermissions(options: { permissions: permissions[]; }) => Promise<PermissionStatus[]>
Takes into account the fact that SCAN and CONNECT permissions only exist on Android 11+; those permissions will always resolve as GRANTED on devices below Android 11.
Param | Type |
---|---|
options | { permissions: permissions[]; } |
Returns: Promise<PermissionStatus[]>
addListener(event: 'discoverUnpaired', listenerFunc: (event: devices) => any) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
event | 'discoverUnpaired' |
listenerFunc | (event: devices) => any |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener(event: 'connectionChange', listenerFunc: (event: { state: ConnectionState; }) => any) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
event | 'connectionChange' |
listenerFunc | (event: { state: ConnectionState; }) => any |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
removeAllListeners() => Promise<void>
Prop | Type |
---|---|
address | string |
name | string |
deviceClass | number |
Prop | Type |
---|---|
remove | () => Promise<void> |
{ address: string }
{ devices: BluetoothDevice[] }
{ [permission in permissions]?: PermissionState }
'coarseLocation' | 'fineLocation' | 'scan' | 'connect'
'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'
Members |
---|
NONE |
CONNECTING |
CONNECTED |
FAQs
Use native bluetooth functionality
The npm package bluetooth-serial receives a total of 2 weekly downloads. As such, bluetooth-serial popularity was classified as not popular.
We found that bluetooth-serial demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.