简体   繁体   中英

Cordova BluetoothSerial plugin : discoverUnpaired method not working

I'm trying to code a mobile crossplatform app that uses the Bluetooth hardware of the device to scan the area and display the list of bluetooth devices available for connection in the area.

So naturally I used google to find a way to do it and found and installed this plugin : BluethoothSerial to use this method : discoverUnpaired which seemed to be the perfect tool to achieve what I wanted to do.

So using a bit of code they gave as an example :

bluetoothSerial.discoverUnpaired(function(devices) {
    devices.forEach(function(device) {
       console.log(device.id);
    })
}, failure);

I ended up with this :

function reshButt() {
alert('reshButt');
bluetoothSerial.discoverUnpaired(
    function(devices) {
        var currentNode;
        devices.forEach(
            function(device){
                currentNode = document.createElement('div');
                currentNode.id = device.id;
                document.getElementById(device.id).innerHTML = '<div id="'+device.name+'" onclick="bluetoothSerial.connect('+device.address+', alert("Connecting to '+device.name+'"), alert("Impossible to connect to '+device.name+'"));">'+device.name+'</div></br>';
                document.getElementById("devices_list").appendChild(currentNode);
            }
        );
    }
);

}

It's ugly I know. But it doesn't work, I wanted to insert in the html code of the app the list of devices found in the area, and I wanted the user to be able to connect to connect to a device by clicking on it but everytime I try to run the code the variables devices and device are undefined. It kind of doesn't surprise me as they seem to get out of nowhere but in the documentation of the plugin it says :

Function discoverUnpaired discovers unpaired Bluetooth devices. The success callback is called with a list of objects similar to list, or an empty list if no unpaired devices are found.

And they give some example of list of devices found with their names, classes, mac addresses, etc.

So I kinda hoped that you could help me here are the index.html and index.js of the project.

<!DOCTYPE html>

<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <title>Bluetooth transmission</title>
</head>

<body>
    <center>
        <h1>Bluetooth transmission</h1>

        <button id="button-research" onclick="reshButt()">Research</button>

        <h2>Devices detected:</h2>

        <div id="devices_list">
        </div>

        <p>Click on the device you want to connect to.</p>

        <script type="text/javascript"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
    </center>
</body>

document.addEventListener('deviceready', onDeviceReady, onError);

function onDeviceReady() {
    alert('deviceReady');
    bluetoothSerial.enable();
}


//_____BlueTooth_____
function reshButt() {
    alert('deviceReady');
    bluetoothSerial.discoverUnpaired(
        function(devices) {
            var currentNode;
            devices.forEach(
                function(device){
                    currentNode = document.createElement('div');
                    currentNode.id = device.id;
                    document.getElementById(device.id).innerHTML = '<div id="'+device.name+'" onclick="bluetoothSerial.connect('+device.address+', alert("Connecting to '+device.name+'"), alert("Impossible to connect to '+device.name+'"));">'+device.name+'</div></br>';
           document.getElementById("devices_list").appendChild(currentNode);
                }
            );
        }
     );
  }
  function onError() {
      alert('Error while looking for BlueTooth devices');
  }

Sorry for bad indentation, Stack overflow kinda ruined it. Thank you guys !

You need to call bluetoothSerial.setDeviceDiscoveredListener before bluetoothSerial.discoverUnpaired .When discoverUnpaired starts, it calls the device discovered listener if it has been set.

Example:

bluetoothSerial.setDeviceDiscoveredListener(function (device) {
                        console.log('Found: ' + device.name); 
});


bluetoothSerial.discoverUnpaired();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM