简体   繁体   English

Cordova BluetoothSerial插件:discoverUnpaired方法不起作用

[英]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. 所以自然地我用谷歌找到了一种方法,并找到并安装了这个插件: BluethoothSerial来使用这种方法: discoverUnpaired这似乎是实现我想要做的完美工具。

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. 但这是行不通的,我想在该应用的html代码中插入在该区域找到的设备列表,我希望用户能够通过单击该设备来连接以连接到设备,但是每次尝试要运行代码,未定义变量devicedevice 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. 功能discoverUnpaired发现未配对的蓝牙设备。 The success callback is called with a list of objects similar to list, or an empty list if no unpaired devices are found. 使用类似于list的对象列表调用成功回调,如果未找到未配对的设备,则使用空列表调用。

And they give some example of list of devices found with their names, classes, mac addresses, etc. 他们还提供了一些带有名称,类,mac地址等的设备列表示例。

So I kinda hoped that you could help me here are the index.html and index.js of the project. 所以我有点希望您可以在这里为我提供项目的index.html和index.js。

<!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. 你需要调用bluetoothSerial.setDeviceDiscoveredListener bluetoothSerial.discoverUnpaired。当discoverUnpaired开始之前,它调用如果已设置设备发现听众。

Example: 例:

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


bluetoothSerial.discoverUnpaired();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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