繁体   English   中英

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

[英]Cordova BluetoothSerial plugin : discoverUnpaired method not working

我正在尝试编写一个使用该设备的蓝牙硬件扫描该区域并显示可用于该区域连接的蓝牙设备列表的移动跨平台应用程序。

所以自然地我用谷歌找到了一种方法,并找到并安装了这个插件: BluethoothSerial来使用这种方法: discoverUnpaired这似乎是实现我想要做的完美工具。

因此,他们使用一些代码作为示例:

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

我结束了这个:

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);
            }
        );
    }
);

}

我知道这很丑。 但这是行不通的,我想在该应用的html代码中插入在该区域找到的设备列表,我希望用户能够通过单击该设备来连接以连接到设备,但是每次尝试要运行代码,未定义变量devicedevice 这一点并不令我感到惊讶,因为它们似乎无处不在,但在插件的文档中却说:

功能discoverUnpaired发现未配对的蓝牙设备。 使用类似于list的对象列表调用成功回调,如果未找到未配对的设备,则使用空列表调用。

他们还提供了一些带有名称,类,mac地址等的设备列表示例。

所以我有点希望您可以在这里为我提供项目的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');
  }

很抱歉缩进不好,堆栈溢出有点毁了它。 感谢大伙们 !

你需要调用bluetoothSerial.setDeviceDiscoveredListener bluetoothSerial.discoverUnpaired。当discoverUnpaired开始之前,它调用如果已设置设备发现听众。

例:

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