我正在尝试使用Monaca IDE中的Onsenui实现蓝牙插件。我一直收到一条错误消息:bluetoothSerial not found。
我想创建一个需要JsFiddle Demo
插件的服务。然后只需调用它来执行.isenabled()调用。任何帮助都会很棒。
app.service('iBeaconService', function() {
var bluetoothSerial = new cordova.plugins.bluetoothSerial;
return {
sendMessage: function(message) {
// interact with bluetoothSerial
}
};
});
app.controller('InfoPageCtrl', ['$scope', 'iBeaconService', function($scope, iBeaconService) {
bluetoothSerial.isEnabled(
function() {
console.log("Bluetooth is enabled");
},
function() {
console.log("Bluetooth is *not* enabled");
}
);
}]);
app.controller('AppController', function($scope) {
$scope.load = function(page) {
$scope.mySplitterContent.load(page)
}
$scope.open = function() {
$scope.mySplitterSide.open();
}
});
<ons-list ng-controller="InfoPageCtrl">
<ons-list-item class="list-item-container" >
<ons-row>
<ons-col width="110px">
<img src="{{beacon.icon}}" class="info-page-img">
</ons-col>
<ons-col>
<div class="info-page-description">
<p style="text-decoration: underline;">UUID</p>
{{beaconUuid}}
</div>
</ons-col>
</ons-row>
</ons-list-item>
</ons-list>
答案 0 :(得分:0)
好的,为延迟道歉,但它花了我一些研究。在使用Monaca IDE安装插件并进行自定义Android构建之后,我能够使用以下代码使其工作:
ons.ready(function(){
bluetoothSerial.isConnected(
function() {
alert("Bluetooth is connected");
},
function() {
alert("Bluetooth is not connected");
}
);
});
如果需要,我可以发布整个Monaca IDE项目进行导入。它将包含许多来自其他线程的其他代码,我帮助了人们。需要注意的重点是,您需要检查ons.ready
,然后访问您的变量。
祝你好运,希望这可以帮助您完成Arduino项目!
答案 1 :(得分:0)
如何将此代码用于Onsenui。
var app = {
initialize: function() {
this.bindEvents();
this.showMainPage();
},
bindEvents: function() {
var TOUCH_START = 'touchstart';
if (window.navigator.msPointerEnabled) { // windows phone
TOUCH_START = 'MSPointerDown';
}
document.addEventListener('deviceready', this.onDeviceReady, false);
refreshButton.addEventListener(TOUCH_START, this.refreshDeviceList, false);
sendButton.addEventListener(TOUCH_START, this.sendData, false);
disconnectButton.addEventListener(TOUCH_START, this.disconnect, false);
deviceList.addEventListener('touchstart', this.connect, false);
},
onDeviceReady: function() {
app.refreshDeviceList();
},
refreshDeviceList: function() {
bluetoothSerial.list(app.onDeviceList, app.onError);
},
onDeviceList: function(devices) {
var option;
// remove existing devices
deviceList.innerHTML = "";
app.setStatus("");
devices.forEach(function(device) {
var listItem = document.createElement('li'),
html = '<b>' + device.name + '</b><br/>' + device.id;
listItem.innerHTML = html;
if (cordova.platformId === 'windowsphone') {
// This is a temporary hack until I get the list tap working
var button = document.createElement('button');
button.innerHTML = "Connect";
button.addEventListener('click', app.connect, false);
button.dataset = {};
button.dataset.deviceId = device.id;
listItem.appendChild(button);
} else {
listItem.dataset.deviceId = device.id;
}
deviceList.appendChild(listItem);
});
if (devices.length === 0) {
option = document.createElement('option');
option.innerHTML = "No Bluetooth Devices";
deviceList.appendChild(option);
if (cordova.platformId === "ios") { // BLE
app.setStatus("No Bluetooth Peripherals Discovered.");
} else { // Android or Windows Phone
app.setStatus("Please Pair a Bluetooth Device.");
}
} else {
app.setStatus("Found " + devices.length + " device" + (devices.length === 1 ? "." : "s."));
}
},
connect: function(e) {
var onConnect = function() {
// subscribe for incoming data
bluetoothSerial.subscribe('\n', app.onData, app.onError);
resultDiv.innerHTML = "";
app.setStatus("Connected");
app.showDetailPage();
};
var deviceId = e.target.dataset.deviceId;
if (!deviceId) { // try the parent
deviceId = e.target.parentNode.dataset.deviceId;
}
bluetoothSerial.connect(deviceId, onConnect, app.onError);
},
onData: function(data) { // data received from Arduino
console.log(data);
resultDiv.innerHTML = resultDiv.innerHTML + "Received: " + data + "<br/>";
resultDiv.scrollTop = resultDiv.scrollHeight;
},
sendData: function(event) { // send data to Arduino
var success = function() {
console.log("success");
resultDiv.innerHTML = resultDiv.innerHTML + "Sent: " + messageInput.value + "<br/>";
resultDiv.scrollTop = resultDiv.scrollHeight;
};
var failure = function() {
alert("Failed writing data to Bluetooth peripheral");
};
var data = messageInput.value;
bluetoothSerial.write(data, success, failure);
},
disconnect: function(event) {
bluetoothSerial.disconnect(app.showMainPage, app.onError);
},
showMainPage: function() {
mainPage.style.display = "";
detailPage.style.display = "none";
},
showDetailPage: function() {
mainPage.style.display = "none";
detailPage.style.display = "";
},
setStatus: function(message) {
console.log(message);
window.clearTimeout(app.statusTimeout);
statusDiv.innerHTML = message;
statusDiv.className = 'fadein';
// automatically clear the status with a timer
app.statusTimeout = setTimeout(function () {
statusDiv.className = 'fadeout';
}, 5000);
},
onError: function(reason) {
alert("ERROR: " + reason); // real apps should use notification.alert
}
};