I've been trying to get a bluetooth plugin for PhoneGap working but I can't seem to figure out where I'm going wrong. Firstly, my test device is a Galaxy S3 (GT-19305T) and the applications were built using the PhoneGap CLI.
The plugin I am attempting to use can be found here with an example here.
I tried the example which didn't seem to actually do anything.
So then I went basic, and tried using the plugins with examples given by PhoneGap. I could quite easily get all of these working. In my example, I am using the basic device information plugin.
Here is some example code:
Javascript:
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
function onDeviceReady() {
var element = document.getElementById('deviceProperties');
element.innerHTML = 'Device Model: ' + device.model + '<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
var btstatus = document.getElementById('status');
btstatus.innerHTML = "Getting bluetooth information";
window.bluetooth.isEnabled(isEnabledSuccess, isEnabledError);
}
function checkBluetoothStatus() {
var btstatus = document.getElementById('status');
btstatus.innerHTML = "Checking bluetooth information";
window.bluetooth.isEnabled(isEnabledSuccess, isEnabledError);
}
function isEnabledSuccess(isEnabled){
var btstatus = document.getElementById('status');
if(isEnabled){
btstatus.innerHTML = "Enabled";
}else{
btstatus.innerHTML = "Disabled";
}
}
function isEnabledError(error){
var btstatus = document.getElementById('status');
btstatus.innerHTML = "Cannot determine Bluetooth status: " + error.message;
}
function enableBluetooth(){
var btstatus = document.getElementById('status');
btstatus.innerHTML = "Attempting to turn bluetooth on";
window.bluetooth.enable(bluetoothTestSucces, bluetoothTestFail);
}
</script>
Html:
<body>
<p id="deviceProperties">Loading device properties...</p>
<br />
<button onclick="enableBluetooth();">Enable Bluetooth</button>
<br />
<button onclick="checkBluetoothStatus();">Check Bluetooth Status</button>
<br />
<p id="status">Loading bluetooth information...</p>
</body>
So basically I am trying to either get the plugin to return the current bluetooth connectivity information, or enable the bluetooth upon clicking the "enable bluetooth" button.
Unfortunately nothing has worked so far and as I stated earlier I am not sure where I am going wrong.
I have tried applying it manually and by using the CLI.
I have recently experimented with the same example and was able to get it working. The main difference however is that I used Cordova CLI instead.
Note: You will need to have installed: Apache ANT, JAVA, Android SDK, GIT Command Line Tool. The first three also need to be set up correctly in your Environment Path.
These are the steps I performed:
npm install -g cordova
npm install -g coffee-script
cd C:\
cordova create bluetooth com.example.bluetooth bluetooth
cd bluetooth
cordova platform add android
cordova plugin add https://github.com/tanelih/phonegap-bluetooth-plugin.git
main.coffee
to main.js
using coffee --compile main.coffee
<script src="cordova.js">
instead of <script src="phonegap.js">
cordova build android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With