├── BluetoothStatus.js ├── package.json ├── LICENSE ├── plugin.xml ├── src ├── ios │ └── BluetoothStatus.m └── android │ └── com │ └── twofivefivekb │ └── android │ └── bluetoothstatus │ └── BluetoothStatus.java └── README.md /BluetoothStatus.js: -------------------------------------------------------------------------------- 1 | var exec = require('cordova/exec'); 2 | 3 | var BluetoothStatus = function() { 4 | }; 5 | 6 | BluetoothStatus.initPlugin = function() { 7 | //wait for device to be ready 8 | document.addEventListener("deviceready", function () { 9 | exec(null, null, "BluetoothStatus", "initPlugin", []); 10 | }, false); 11 | }; 12 | 13 | BluetoothStatus.enableBT = function() { 14 | exec(null, null, "BluetoothStatus", "enableBT", []); 15 | }; 16 | 17 | BluetoothStatus.promptForBT = function() { 18 | exec(null, null, "BluetoothStatus", "promptForBT", []); 19 | }; 20 | 21 | BluetoothStatus.hasBT = false; 22 | BluetoothStatus.hasBTLE = false; 23 | BluetoothStatus.BTenabled = false; 24 | BluetoothStatus.iosAuthorized = true; 25 | 26 | module.exports = BluetoothStatus; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-bluetooth-status", 3 | "version": "1.0.4", 4 | "description": "Cordova Bluetooth status plugin", 5 | "cordova": { 6 | "id": "cordova-plugin-bluetooth-status", 7 | "platforms": [ 8 | "android", 9 | "ios" 10 | ] 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/255kb/cordova-plugin-bluetooth-status.git" 15 | }, 16 | "keywords": [ 17 | "cordova", 18 | "bluetooth", 19 | "ecosystem:cordova", 20 | "cordova-android", 21 | "cordova-ios" 22 | ], 23 | "engines": [ 24 | { 25 | "name": "cordova", 26 | "version": ">=3.0.0" 27 | } 28 | ], 29 | "author": "Guillaume Monnet", 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/255kb/cordova-plugin-bluetooth-status/issues" 33 | }, 34 | "homepage": "https://github.com/255kb/cordova-plugin-bluetooth-status#readme" 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Guillaume Monnet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Bluetooth status 8 | Cordova Bluetooth status plugin 9 | cordova,bluetooth 10 | MIT 11 | Guillaume Monnet 12 | https://github.com/255kb/cordova-plugin-bluetooth-status.git 13 | https://github.com/255kb/cordova-plugin-bluetooth-status/issues 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/ios/BluetoothStatus.m: -------------------------------------------------------------------------------- 1 | #import 2 | #import 3 | #import 4 | 5 | @interface BluetoothStatus : CDVPlugin { 6 | 7 | } 8 | 9 | @property CBCentralManager* bluetoothManager; 10 | 11 | - (void)initPlugin:(CDVInvokedUrlCommand*)command; 12 | - (void)enableBT:(CDVInvokedUrlCommand*)command; 13 | - (void)promptForBT:(CDVInvokedUrlCommand*)command; 14 | 15 | @end 16 | 17 | @implementation BluetoothStatus 18 | 19 | - (void)initPlugin:(CDVInvokedUrlCommand*)command 20 | { 21 | //set bluetooth capable 22 | [[self commandDelegate] evalJs:[NSString stringWithFormat:@"cordova.plugins.BluetoothStatus.hasBT = true;"]]; 23 | //set bluetoothLE capable 24 | [[self commandDelegate] evalJs:[NSString stringWithFormat:@"cordova.plugins.BluetoothStatus.hasBTLE = true;"]]; 25 | 26 | // set the initial state 27 | [self centralManagerDidUpdateState: self.bluetoothManager]; 28 | } 29 | 30 | - (void)enableBT:(CDVInvokedUrlCommand*)command 31 | { 32 | //not available for iOS 33 | } 34 | 35 | - (void)promptForBT:(CDVInvokedUrlCommand*)command 36 | { 37 | //not available for iOS 38 | } 39 | 40 | - (void)pluginInitialize 41 | { 42 | // Create CoreBluetooth manager 43 | self.bluetoothManager = [[CBCentralManager alloc] 44 | initWithDelegate: self 45 | queue: dispatch_get_main_queue() 46 | options: @{CBCentralManagerOptionShowPowerAlertKey: @(NO)}]; 47 | } 48 | 49 | - (void)centralManagerDidUpdateState:(CBCentralManager *)central { 50 | if([central state] == CBCentralManagerStatePoweredOn) { 51 | NSLog(@"Bluetooth was enabled"); 52 | [[self commandDelegate] evalJs:[NSString stringWithFormat:@"cordova.plugins.BluetoothStatus.BTenabled = true;"]]; 53 | [[self commandDelegate] evalJs:[NSString stringWithFormat:@"cordova.fireWindowEvent('BluetoothStatus.enabled');"]]; 54 | } else if([central state] == CBCentralManagerStatePoweredOff) { 55 | NSLog(@"Bluetooth was disabled"); 56 | [[self commandDelegate] evalJs:[NSString stringWithFormat:@"cordova.plugins.BluetoothStatus.BTenabled = false;"]]; 57 | [[self commandDelegate] evalJs:[NSString stringWithFormat:@"cordova.fireWindowEvent('BluetoothStatus.disabled');"]]; 58 | } else if([central state] == CBCentralManagerStateUnsupported) { 59 | NSLog(@"Bluetooth LE is not supported"); 60 | [[self commandDelegate] evalJs:[NSString stringWithFormat:@"cordova.plugins.BluetoothStatus.hasBTLE = false;"]]; 61 | } else if([central state] == CBCentralManagerStateUnauthorized) { 62 | NSLog(@"use of Bluetooth is not authorized"); 63 | [[self commandDelegate] evalJs:[NSString stringWithFormat:@"cordova.plugins.BluetoothStatus.iosAuthorized = false;"]]; 64 | } 65 | } 66 | 67 | 68 | @end 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## /!\ This package is not maintained anymore /!\ 3 | 4 | # Cordova Bluetooth status plugin 5 | 6 | [![NPM](https://nodei.co/npm/cordova-plugin-bluetooth-status.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/cordova-plugin-bluetooth-status/) 7 | 8 | Cordova Bluetooth status is a Cordova plugin allowing to check for Bluetooth adapter status. 9 | The plugin currently offers the following features: 10 | 11 | - check for Bluetooth capability 12 | - check for Bluetooth LE capability 13 | - check if use of Bluetooth is authorized (iOS only) 14 | - monitor/check Bluetooth enabled/disabled state 15 | - force Bluetooth activation (Android only) 16 | - prompt the user to enable the Bluetooth (Android only) 17 | 18 | # Currently supported platforms 19 | 20 | - Android 21 | - iOS 22 | 23 | # Installation 24 | 25 | cordova plugin add cordova-plugin-bluetooth-status 26 | 27 | # Usage 28 | 29 | Initialize the plugin with the `cordova.plugins.BluetoothStatus.initPlugin()` method. Otherwise, the BT adapter state may not be correctly populated until the next time adapter is switched off/on. 30 | 31 | # Documentation 32 | 33 | This plugin exports its methods and properties on `cordova.plugins.BluetoothStatus`. 34 | 35 | ## Properties 36 | 37 | ### BluetoothStatus.hasBT (Boolean variable) 38 | 39 | Variable storing the presence of a Bluetooth device 40 | 41 | ### BluetoothStatus.hasBTLE (Boolean variable) 42 | 43 | Variable storing the presence of a Bluetooth LE device 44 | 45 | ### BluetoothStatus.BTenabled (Boolean variable) 46 | 47 | Variable storing the state of the Bluetooth adapter (enabled or not) 48 | 49 | ### BluetoothStatus.iosAuthorized (Boolean variable) \[iOS only\] 50 | 51 | Variable storing the authorization to use the Bluetooth adapter on iOS 52 | 53 | ## Methods 54 | 55 | ### BluetoothStatus.initPlugin() 56 | 57 | Initialize the plugin and populate the initial state of the Bluetooth adapter 58 | 59 | cordova.plugins.BluetoothStatus.initPlugin(); 60 | 61 | ### BluetoothStatus.enableBT() \[Android only\] 62 | 63 | Activate the Bluetooth without prompting the user: 64 | 65 | cordova.plugins.BluetoothStatus.enableBT(); 66 | 67 | ### BluetoothStatus.promptForBT() \[Android only\] 68 | 69 | Prompt the user to activate the Bluetooth: 70 | 71 | cordova.plugins.BluetoothStatus.promptForBT(); 72 | 73 | ## Events 74 | 75 | These events are fired on the `window` object. 76 | 77 | ### 'BluetoothStatus.enabled' 78 | 79 | This event is triggered when the Bluetooth adapter gets enabled. 80 | 81 | window.addEventListener('BluetoothStatus.enabled', function() { 82 | console.log('Bluetooth has been enabled'); 83 | }); 84 | 85 | ### 'BluetoothStatus.disabled' 86 | 87 | This event is triggered when the Bluetooth adapter gets disabled. 88 | 89 | window.addEventListener('BluetoothStatus.disabled', function() { 90 | console.log('Bluetooth has been disabled'); 91 | }); 92 | 93 | ## Changelog 94 | 95 | ### v1.0.4: 96 | - correct bugs for iOS9+ where initial BT adapter state was not correctly gathered 97 | 98 | ### v1.0.3: 99 | - correct some build errors for older Android versions 100 | -------------------------------------------------------------------------------- /src/android/com/twofivefivekb/android/bluetoothstatus/BluetoothStatus.java: -------------------------------------------------------------------------------- 1 | package com.twofivefivekb.android.bluetoothstatus; 2 | 3 | import org.apache.cordova.CordovaWebView; 4 | import org.apache.cordova.CallbackContext; 5 | import org.apache.cordova.CordovaInterface; 6 | import org.apache.cordova.CordovaArgs; 7 | import org.apache.cordova.CordovaPlugin; 8 | import org.json.JSONArray; 9 | import org.json.JSONException; 10 | 11 | import android.bluetooth.BluetoothAdapter; 12 | import android.bluetooth.BluetoothManager; 13 | import android.content.BroadcastReceiver; 14 | import android.content.Context; 15 | import android.content.Intent; 16 | import android.content.IntentFilter; 17 | import android.content.pm.PackageManager; 18 | import android.util.Log; 19 | 20 | public class BluetoothStatus extends CordovaPlugin { 21 | private static CordovaWebView mwebView; 22 | private static CordovaInterface mcordova; 23 | 24 | private static final String LOG_TAG = "BluetoothStatus"; 25 | private BluetoothManager bluetoothManager; 26 | private BluetoothAdapter bluetoothAdapter; 27 | 28 | @Override 29 | public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 30 | if (action.equals("enableBT")) { 31 | enableBT(); 32 | return true; 33 | } else if (action.equals("promptForBT")) { 34 | promptForBT(); 35 | return true; 36 | } else if(action.equals("initPlugin")) { 37 | initPlugin(); 38 | return true; 39 | } 40 | return false; 41 | } 42 | 43 | @Override 44 | public void onDestroy() { 45 | super.onDestroy(); 46 | 47 | mcordova.getActivity().unregisterReceiver(mReceiver); 48 | } 49 | 50 | @Override 51 | public void initialize(CordovaInterface cordova, CordovaWebView webView) { 52 | super.initialize(cordova, webView); 53 | 54 | mwebView = super.webView; 55 | mcordova = cordova; 56 | 57 | bluetoothManager = (BluetoothManager) webView.getContext().getSystemService(Context.BLUETOOTH_SERVICE); 58 | bluetoothAdapter = bluetoothManager.getAdapter(); 59 | 60 | // Register for broadcasts on BluetoothAdapter state change 61 | IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 62 | mcordova.getActivity().registerReceiver(mReceiver, filter); 63 | } 64 | 65 | private void enableBT() { 66 | //enable bluetooth without prompting 67 | if (bluetoothAdapter == null) { 68 | Log.e(LOG_TAG, "Bluetooth is not supported"); 69 | } else if (!bluetoothAdapter.isEnabled()) { 70 | bluetoothAdapter.enable(); 71 | } 72 | } 73 | 74 | private void promptForBT() { 75 | //prompt user for enabling bluetooth 76 | Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 77 | mcordova.getActivity().startActivity(enableBTIntent); 78 | } 79 | 80 | private void initPlugin() { 81 | //test if B supported 82 | if (bluetoothAdapter == null) { 83 | Log.e(LOG_TAG, "Bluetooth is not supported"); 84 | } else { 85 | Log.e(LOG_TAG, "Bluetooth is supported"); 86 | 87 | sendJS("javascript:cordova.plugins.BluetoothStatus.hasBT = true;"); 88 | 89 | //test if BLE supported 90 | if (!mcordova.getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { 91 | Log.e(LOG_TAG, "BluetoothLE is not supported"); 92 | } else { 93 | Log.e(LOG_TAG, "BluetoothLE is supported"); 94 | sendJS("javascript:cordova.plugins.BluetoothStatus.hasBTLE = true;"); 95 | } 96 | 97 | //test if BT enabled 98 | if (bluetoothAdapter.isEnabled()) { 99 | Log.e(LOG_TAG, "Bluetooth is enabled"); 100 | 101 | sendJS("javascript:cordova.plugins.BluetoothStatus.BTenabled = true;"); 102 | sendJS("javascript:cordova.fireWindowEvent('BluetoothStatus.enabled');"); 103 | } else { 104 | Log.e(LOG_TAG, "Bluetooth is not enabled"); 105 | } 106 | } 107 | } 108 | 109 | private void sendJS(final String js) { 110 | mcordova.getActivity().runOnUiThread(new Runnable() { 111 | @Override 112 | public void run() { 113 | mwebView.loadUrl(js); 114 | } 115 | }); 116 | } 117 | 118 | //broadcast receiver for BT intent changes 119 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 120 | @Override 121 | public void onReceive(Context context, Intent intent) { 122 | final String action = intent.getAction(); 123 | 124 | if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 125 | final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); 126 | switch (state) { 127 | case BluetoothAdapter.STATE_OFF: 128 | Log.e(LOG_TAG, "Bluetooth was disabled"); 129 | 130 | sendJS("javascript:cordova.plugins.BluetoothStatus.BTenabled = false;"); 131 | sendJS("javascript:cordova.fireWindowEvent('BluetoothStatus.disabled');"); 132 | 133 | break; 134 | case BluetoothAdapter.STATE_ON: 135 | Log.e(LOG_TAG, "Bluetooth was enabled"); 136 | 137 | sendJS("javascript:cordova.plugins.BluetoothStatus.BTenabled = true;"); 138 | sendJS("javascript:cordova.fireWindowEvent('BluetoothStatus.enabled');"); 139 | 140 | break; 141 | } 142 | } 143 | } 144 | }; 145 | } --------------------------------------------------------------------------------