1

I am trying to implement a bluetooth plugin using Onsenui in Monaca IDE. I keep getting an error message saying: bluetoothSerial not found.

I want to create a service that requires the Bluetooth Serial plugin. Then simply call this to do a .isenabled() call. Any help would be great.

    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>
condo1234
  • 3,285
  • 6
  • 25
  • 34

2 Answers2

0

After installing the plugin with Monaca IDE and doing the custom Android build, I was able to get it to work using the following code:

ons.ready(function(){
        bluetoothSerial.isConnected(
            function() {
                alert("Bluetooth is connected");
            },
            function() {
                alert("Bluetooth is not connected");
            }
        );      
    });

The big thing to note, is you need to check for ons.ready, then access your variable.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Munsterlander
  • 1,356
  • 1
  • 16
  • 29
  • Thanks that would be helpful! – condo1234 Jun 28 '16 at 10:01
  • I would love to know how to turn the example I posted above into Onsenui code (below my answer)... I think Bluetooth connectivity with mobile apps is pretty popular so plenty of people would find this useful! – condo1234 Jun 28 '16 at 10:11
  • You should just edit your original post with the code you want us to work on. Posting an answer that is not an answer is not the best way to share the information as it won't come up if searched. – Munsterlander Jun 28 '16 at 21:17
  • Since we are discussing this outside, for others here is the full discussion: https://community.onsen.io/topic/517/onsenui-bluetooth-tutorial – Munsterlander Jun 28 '16 at 21:35
0

How to turn this code to use for 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
        }
    };
condo1234
  • 3,285
  • 6
  • 25
  • 34