- Notifications
You must be signed in to change notification settings - Fork0
jbaysolutions/capacitor-android-bluetoothmanager
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Current version: 0.0.5
NPM release:https://www.npmjs.com/package/capacitor-android-bluetoothmanager
Only for Android .... in case the name of the plugin is not explicit enough
Capacitor plugin to allow access to the underlying Bluetooth Manager, if one exists
This is a work in progress for an internal project
If you want to use the official version that is published on npm :
yarn add capacitor-android-hotspot
If you want to use a clone of this repository:
Git clone this code
Build it using :
yarn build
Add the plugin to your Capacitor Android Project
yarn add file:/path/to/capacitor-android-bluetoothmanager
Now do Android Plugin integration:
packagecom.example.android.project;importandroid.os.Bundle;importcom.getcapacitor.BridgeActivity;importcom.getcapacitor.Plugin;importjava.util.ArrayList;// ----------------- IMPORT THE PLUGIN ----------------------importcom.jbaysolutions.capacitor.bluetooth.capacitorandroidbluetoothmanager.BluetoothManagerPlugin;publicclassMainActivityextendsBridgeActivity {@OverridepublicvoidonCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);// Initializes the Bridgethis.init(savedInstanceState,newArrayList<Class<?extendsPlugin>>() {{// Additional plugins you've installed go here// Ex: add(TotallyAwesomePlugin.class);// -------------- ADD THE PLUGIN -------------add(BluetoothManagerPlugin.class); }}); }}
- Use it on your Capacitor Project:
// Import bitsimport{Plugins}from'@capacitor/core'import'capacitor-android-bluetoothmanager'const{ BluetoothManagerPlugin}=Plugins// and then use the BluetoothManagerPlugin
Definition:initialize(): Promise<void>;
This is the first method you must call on your code. What this does :
- If you don't have the permissions, a request to the user is made
- We try to get a BluetoothAdapter if there is Blurtooth Support
- We start listening for BT events.
BluetoothManagerPlugin.initialize() .then(() => { console.log('Initialized !') }) .catch(() => { console.log('Dont have permissions to work with Bluetooth') })
Definition:hasBluetoothSupport(): Promise<{ hwSupport: boolean }>;
BluetoothManagerPlugin.hasBluetoothSupport() .then(answer => { if (answer.hwSupport) { // YES WE HAVE SUPPORT } else { // NO WE DONT HAVE SUPPORT } })
Definition:isBluetoothEnabled(): Promise<{ enabled: boolean }>;
Error can be thrown if the adaptor is not inited, which can happen in one of two conditions :
- initialize() was not called
- the device doesn't have BT support. IF initialize was called, you can use
hasBluetoothSupport
to check this.
BluetoothManagerPlugin.isBluetoothEnabled() .then(result => { console.log('Is bluetooth enabled? ' + result.enabled); }) .catch(error => { console.log('Error : ' + JSON.stringify(error)) })
Definition:enableBluetooth(): Promise<void>;
THIS METHOD IS ASYNC!
Error can be thrown if :
- Was not initialized
- Already Enabled
- Already busy disabling or enabling BT
BluetoothManagerPlugin.enableBluetooth() .then(()) => { console.log('Called, and no error happened'); }) .catch(error => { console.log('Error : ' + JSON.stringify(error)) })
Definition:disableBluetooth(): Promise<void>;
THIS METHOD IS ASYNC!
Error can be thrown if :
- Was not initialized
- Already Disabled
- Already busy disabling or enabling BT
BluetoothManagerPlugin.disableBluetooth() .then(()) => { console.log('Called, and no error happened'); }) .catch(error => { console.log('Error : ' + JSON.stringify(error)) })
Definition:getName(): Promise<{ name: string }>;
BluetoothManagerPlugin.getName() .then((result) => { console.log(' The name is : ' + result.name) }) .catch(error => { console.log(' Hw support failure : ' + JSON.stringify(error)) })
Definition:setDeviceName(deviceName: string): Promise<void>;
Errors can be thrown if :
- BT adapter was not inited.
- BT adapter is in a state that does not allow for this change
- the new provided name is null .... you can't set name to null, ok?
BluetoothManagerPlugin.setDeviceName({ deviceName: 'NEW_NAME_HERE' }) .then(() => { console.log(' Name Changed !') }) .catch(error => { console.log(' Failed for Reason: ' + JSON.stringify(error)) })
When state changes happen on the BT adapter the Plugin notifies Listeners withBluetoothManagerPluginEvent
To listen to these events you must add a listener to it:
var bluetoothEventListener = Plugins.BluetoothManagerPlugin.addListener( 'BluetoothManagerPluginEvent', (result) => { console.log('BluetoothManagerPluginEvent : ' + JSON.stringify(result)) if (result.eventType === 'STATE_TURNING_ON') { } else if (result.eventType === 'STATE_TURNING_OFF') { } else if (result.eventType === 'STATE_ON') { } else if (result.eventType === 'STATE_OFF') { } } )
Remember to unregister it when you leave and don't need to be listening anymore:
this.bluetoothEventListener.remove()
About
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.