【问题标题】:send data from python to cordova app via Bluetooth通过蓝牙将数据从 python 发送到 cordova 应用程序
【发布时间】:2017-10-04 07:14:48
【问题描述】:

各位开发者您好,

我在建立 Python 与 cordova (Phonegap) 应用程序之间的通信通道时遇到问题。

我有一个 python 脚本正在发送数据,只有当我通过蓝牙通过另一个 python 脚本接收数据时它才起作用。

但我无法在 cordova 应用程序中接收数据。

这是python脚本。

import bluetooth

bd_addr = "01:23:45:67:89:AB"

port = 1

sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))

sock.send("hello!!")

sock.close()

我想制作一个可以在安卓手机中接收数据的cordova脚本。

如果能得到您的帮助,将不胜感激。

谢谢

【问题讨论】:

    标签: android python cordova bluetooth phonegap


    【解决方案1】:

    编辑

    如果你想使用套接字来连接和传输数据,你可以试试这个插件。

    https://www.npmjs.com/package/cordova-plugin-networking-bluetooth

    安装:

    cordova plugin add cordova-plugin
    

    连接示例:

    var uuid = '94f39d29-7d6d-437d-973b-fba39e49d4ee';
    
    networking.bluetooth.connect(device.address, uuid, function (socketId) {
        // Profile implementation here. 
    }, function (errorMessage) {
        console.log('Connection failed: ' + errorMessage);
    });
    

    从套接字接收数据的示例:

    networking.bluetooth.onReceive.addListener(function (receiveInfo) {
          if (receiveInfo.socketId !== socketId) {
              return;
          }
    
          // receiveInfo.data is an ArrayBuffer. 
    });
    

    原创

    我以前用过这个插件:

    https://github.com/don/BluetoothSerial

    安装:

    cordova plugin add cordova-plugin-bluetooth-serial
    

    您的手机必须启动连接,然后您可以使用订阅方法来监听数据。然后将此数据传递给回调函数。

    在从 python 脚本发送的数据中,您需要包含一个分隔符,例如换行符,以便插件知道何时完成读取数据。用法是这样的:

    function connectSuccess(){
    
        //The first argument is the delimiter to stop reading data at
    
        bluetoothSerial.subscribe('\n', function (data) {
            console.log(data);
        }, failure);
    
    }
    
    function failure(e){
    
        console.log('Subscribe failure: ' + e);
    
    }
    
    function connectFailure(e){
    
        console.log('Connect failure: ' + e);
    
    }
    
    bluetoothSerial.connect(macAddress_or_uuid, connectSuccess, connectFailure);
    

    【讨论】:

    • 感谢您的回复。我正在尝试你的方式,请保持联系。
    • 如果我能提供更多帮助,请告诉我。
    • 是的,我试过了,但是在连接 api 之后它给出了错误无法连接。
    • 有谁能帮助我吗??
    • bluetoothSerial.connect( app.macAddress, // 连接到 app.openPort 的设备, // 如果成功则开始监听 app.showError // 如果失败则显示错误 );它显示错误无法连接到设备
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多