簡體   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