Reputation: 15
I am using "randdusing/cordova-plugin-bluetoothle" plugin in PhoneGap/Cordova App to create a Bluetooth application. I am using a simple example which is given on GitHub page of the plugin. but I am not getting any device listed but getting a message Scanning for devices (will continue to scan until you select a device)...
Below is the Code I am using
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
bluetoothle.initialize({
request: true,
statusReceiver: false
}, initializeSuccess, handleError);
}
function initializeSuccess(result) {
if (result.status === "enabled") {
log("Bluetooth is enabled.");
log(result);
} else {
document.getElementById("start-scan").disabled = true;
log("Bluetooth is not enabled:", "status");
log(result, "status");
}
}
function handleError(error) {
var msg;
if (error.error && error.message) {
var errorItems = [];
if (error.service) {
errorItems.push("service: " + (uuids[error.service] || error.service));
}
if (error.characteristic) {
errorItems.push("characteristic: " + (uuids[error.characteristic] || error.characteristic));
}
msg = "Error on " + error.error + ": " + error.message + (errorItems.length && (" (" + errorItems.join(", ") + ")"));
} else {
msg = error;
}
log(msg, "error");
if (error.error === "read" && error.service && error.characteristic) {
reportValue(error.service, error.characteristic, "Error: " + error.message);
}
}
var foundDevices = [];
function startScan() {
log("Starting scan for devices...", "status");
document.getElementById("devices").innerHTML = "";
document.getElementById("services").innerHTML = "";
document.getElementById("output").innerHTML = "";
if (window.cordova.platformId === "windows") {
bluetoothle.retrieveConnected(retrieveConnectedSuccess, handleError, {});
} else {
bluetoothle.startScan(startScanSuccess, handleError, {
services: []
});
}
}
function startScanSuccess(result) {
log("startScanSuccess(" + result.status + ")");
if (result.status === "scanStarted") {
log("Scanning for devices (will continue to scan until you select a device)...", "status");
} else if (result.status === "scanResult") {
if (!foundDevices.some(function (device) {
return device.address === result.address;
})) {
log('FOUND DEVICE:');
log(result);
foundDevices.push(result);
addDevice(result.name, result.address);
}
}
}
function retrieveConnectedSuccess(result) {
log("retrieveConnectedSuccess()");
log(result);
result.forEach(function (device) {
addDevice(device.name, device.address);
});
}
function addDevice(name, address) {
var button = document.createElement("button");
button.style.width = "100%";
button.style.padding = "10px";
button.style.fontSize = "16px";
button.textContent = name + ": " + address;
button.addEventListener("click", function () {
document.getElementById("services").innerHTML = "";
connect(address);
});
document.getElementById("devices").appendChild(button);
}
function log(msg, level) {
level = level || "log";
if (typeof msg === "object") {
msg = JSON.stringify(msg, null, " ");
}
console.log(msg);
if (level === "status" || level === "error") {
var msgDiv = document.createElement("div");
msgDiv.textContent = msg;
if (level === "error") {
msgDiv.style.color = "red";
}
msgDiv.style.padding = "5px 0";
msgDiv.style.borderBottom = "rgb(192,192,192) solid 1px";
document.getElementById("output").appendChild(msgDiv);
}
}
This is my first Bluetooth LE Project in Cordova. Please help me and suggest any other plugin better than this with good documentation.
Thanks.
Upvotes: 0
Views: 885
Reputation: 76
If you are testing your app on Android 6+ you need to request permission first in order to use the Bluetooth functionality of the device. Otherwise your app will silently fail and you won't see any devices discovered. You can use the built-in method of the Bluetooth LE plugin for this
bluetoothle.requestPermission().then(success, fail)
Or you can add cordova-plugin-android-permissions, should you need to request other permissions as well.
Upvotes: 1