capacitor-android-bluetoothmanager
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "capacitor-android-bluetoothmanager", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Access to the Android Bluetooth Manager", | ||
@@ -5,0 +5,0 @@ "main": "dist/plugin.js", |
181
readme.md
# Capacitor-Android-BluetoothManager | ||
**Current version: 0.0.2** | ||
**NPM release: https://www.npmjs.com/package/capacitor-android-bluetoothmanager** | ||
--- | ||
**Only for Android .... in case the name of the plugin is not explicit enough** | ||
@@ -11,2 +17,10 @@ | ||
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 | ||
@@ -22,3 +36,3 @@ | ||
* Do Android Plugin integration | ||
**Now do Android Plugin integration:** | ||
@@ -64,4 +78,169 @@ ```java | ||
// and then use the BluetoothManagerPlugin | ||
``` | ||
## Methods | ||
### initialize() | ||
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 BlutoothAdapter if there is Blutooth Support | ||
* We start listening for BT events. | ||
``` | ||
BluetoothManagerPlugin.initialize() | ||
.then(() => { | ||
console.log('Initialized !') | ||
}) | ||
.catch(() => { | ||
console.log('Dont have permissions to work with Bluetooth') | ||
}) | ||
``` | ||
### hasBluetoothSupport() | ||
Definition: `hasBluetoothSupport(): Promise<{ hwSupport: boolean }>;` | ||
``` | ||
BluetoothManagerPlugin.hasBluetoothSupport() | ||
.then(answer => { | ||
if (answer.hwSupport) { | ||
// YES WE HAVE SUPPORT | ||
} else { | ||
// NO WE DONT HAVE SUPPORT | ||
} | ||
}) | ||
``` | ||
### isBluetoothEnabled() | ||
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)) | ||
}) | ||
``` | ||
### enableBluetooth() | ||
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)) | ||
}) | ||
``` | ||
### disableBluetooth() | ||
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)) | ||
}) | ||
``` | ||
### getName() | ||
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)) | ||
}) | ||
``` | ||
### setDeviceName() | ||
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)) | ||
}) | ||
``` | ||
## Listening to Events | ||
When state changes happen on the BT adapter the Plugin notifies Listeners with `BluetoothManagerPluginEvent` | ||
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() | ||
``` | ||
## |
244
1094936
136
428