How to use Bluetooth Low Energy (BLE) Central Plugin for Apache Codova

Asked 2 years ago, Updated 2 years ago, 104 views

BLE CordovaPlugin (https://github.com/don/cordova-plugin-ble-central/blob/master/README.md)
I am trying to develop an iPhone app with Monaca using .
Run Environment: iPhone 5s (iOS7)
If you set up the plug-in and execute the following code in javascript,
javascript:

 document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady()
    {
        console.log("PhoneGap is ready";
    }
    $(function()
    {
            log("bbb";
            scan();
            log("aaaa";
    })

    function log(str)
    {
        $("#log").append(str+"<br/>");
        $("#log").scrollTop($("#log")[0].scrollHeight);
    }

    function scan()
    {
        log(">>scan";
        window.ble.scan([], 5, function(device)
        {
            log(JSON.stringify(device));
        }, function(reason)
        {
            log("ERROR:" + reason);
        });
        log("<<scan";
    }

HTML:

<body>
    <divid="log"></div>
</body>

The results of the run are:

bbbb
>scan

All I could see was

What do you expect?

bbbb
>scan
Device Information
<scan
aaaaaaaaaaaaaaaaaaaa

That's it.

The iPhone console log looks like this:

Apr5 15:09:43 WWTF MonacaApp [957]<Notice>: DiskCookieStorage changing policy from 2 to 0, cookie file: file:///private/var/mobile/containers/Data/Application/3A33816B-D3DC-4EC2-8866-DEA 6Fookies/Cookies/Cookies/Cookies/Cookies/Cookies
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: Multi-tasking-> Device: YES, App: YES
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>—Unlimited access to network resources
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: [CDVTimer] [monaca] 0.181019ms
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: [CDVTimer] [splashscreen] 33.852994ms
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: [CDVTimer] [statusbar] 11.131048ms
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>—Cordova BLE Central Plugin
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: (c) 2014-2015 Don Coleman
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: [CDVTimer] [ble] 1.643002ms
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: [CDVTimer] [TotalPluginStartup] 49.09982ms
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: Status of CoreBluetooth central manager changed 5 State powered up and ready (CBC CentralManagerStatePoweredOn)
Apr 5 15:09:43 WWTF MonacaApp [957] <Warning>: Resetting plugins due to page load.
Apr 5 15:09:44 WWTF MonacaApp [957] <Warning>: Finished load of: file://private/var/mobile/containers/Bundle/Application/5B3DD2D2-53DA-404040-B246-9E70DC9501A7/MonacaApp/ww/index.html

I have confirmed that the iPhone application LightBlue scan has been successfully performed and device information has been retrieved.

Could someone please tell me what's wrong?

javascript ios monaca cordova bluetooth

2022-09-30 20:44

1 Answers

Self-resolved.

The following causes occurred:

  • Must run after PhoneGap initialization is complete
  • You must register button listeners in onDevice Ready
  • $(function(){}) in jQuery may not have finished initializing PhoneGap.
  • Monaca preview feature does not call device ready

Now you can run it with the following code:

<!DOCTYPE HTML>
<html>

<head>
    <metacharset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scale=no">
    <script src="components/loader.js"></script>
    <link rel="stylesheet" href="components/loader.css">
    <link rel="stylesheet" href="css/style.css">
    <script>
        $(document).ready(function()
        {
            // Are running in native app or in a browser?
            window.isphone=false;
            if( document.URL.indexOf("http://")===-1&document.URL.indexOf("https://")===-1)
            {
                window.isphone=true;
            }
            if(window.isphone)
            {
                document.addEventListener("deviceready", onDeviceReady, false);
            }
            else
            {
                onDeviceReady();
            }
        });

        function onDeviceReady()
        {
            $('#scan').on('click', function()
            {
                scan();
            });
        }

        function log(str)
        {
            $("#log").append(str+"<br/>");
            $("#log").scrollTop($("#log")[0].scrollHeight);
        }

        function scan()
        {
            log(">>scan";
            ble.scan([], 5, function(device)
            {
                log("DEVICE:" + JSON.stringify(device)));
            }, function(reason)
            {
                log("ERROR:" + reason);
            });
            log("<<scan";
        }
    </script>
</head>

<body>
    <button id="scan" type="button">Scan</button>
    <divid="log"></div>
</body>


2022-09-30 20:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.