【问题标题】:Using SL4A (Python) and bluetooth使用 SL4A (Python) 和蓝牙
【发布时间】:2012-07-31 00:27:17
【问题描述】:

寻找一些指导让我开始。

我的左手有一个运行 ICS 的 SGS2。我已经启动并运行了 SL4A 并安装了 Python 2.6.2

我的右手有一个通用的中国蓝牙 RFID 阅读器。它工作,它读取标签(它有一个显示器),它与手机配对。

我希望它们玩得很好 - 我想编写一些脚本来持续监视设备并在传输代码时捕获代码。

我不是 Python 专家,但我已经使用它在 Web 服务器上构建简单的 I/O 作业有一段时间了,所以我可以找到自己的方法。

不过,不同寻常的是,我在处理这个问题时遇到了真正的问题 - 我找不到任何“蓝牙和 SL4A 入门”资源来完成建立持久连接和监控输出的第一步。

有什么建议吗?

【问题讨论】:

    标签: android python bluetooth sl4a


    【解决方案1】:

    看来您需要的是蓝牙外观。以下是一些与蓝牙相关的命令,可能对您有所帮助:

    bluetoothAccept
    bluetoothActiveConnections
    bluetoothConnect
    bluetoothDiscoveryCancel
    bluetoothDiscoveryStart
    bluetoothGetConnectedDeviceName
    bluetoothGetLocalAddress
    bluetoothGetLocalName
    bluetoothGetRemoteDeviceName
    bluetoothGetScanMode
    bluetoothIsDiscovering
    bluetoothMakeDiscoverable
    bluetoothRead
    bluetoothReadBinary
    bluetoothReadLine
    bluetoothReadReady
    bluetoothSetLocalName
    bluetoothStop
    bluetoothWrite
    bluetoothWriteBinary
    checkBluetoothState
    toggleBluetoothState
    


    要调用这些命令中的任何一个,您需要执行类似的操作

    import android
    droid = android.Android()
    #call your commands with droid.bluetoothcommand
    droid.bluetoothDiscoveryStart()
    #or
    droid.toggleBluetoothState(True)
    


    这是一些蓝牙功能的示例,它包含在 SL4A 中,但为了清楚起见,我添加了 cmets:

    import android #for bluetooth functions
    import time #for waiting
    
    #get everything setup
    droid = android.Android()
    
    #turn on bluetooth
    droid.toggleBluetoothState(True)
    
    #ask user
    droid.dialogCreateAlert('Be a server?')
    droid.dialogSetPositiveButtonText('Yes')
    droid.dialogSetNegativeButtonText('No')
    droid.dialogShow()
    
    #get user response to question
    result = droid.dialogGetResponse()
    
    #if the result is 'Yes' ('positive') then is_server is set to True
    is_server = result.result['which'] == 'positive'
    
    if is_server:
      #so if is_server is true make the device discoverable and accept the next connection
      droid.bluetoothMakeDiscoverable()
      droid.bluetoothAccept()
    else:
      #attempts to connect to a device over bluetooth, the logic being that if the phone
      #is not receiving a connection then the user is attempting to connect to something
      droid.bluetoothConnect()
    
    
    if is_server:
      result = droid.getInput('Chat', 'Enter a message').result #Gets a message to send 
      #via bluetooth
      if result is None:
        droid.exit() #exit if nothing is in the message
      droid.bluetoothWrite(result + '\n') #otherwise write the message
    
    while True: #receives a message
      message = droid.bluetoothReadLine().result
      droid.dialogCreateAlert('Chat Received', message)
      droid.dialogSetPositiveButtonText('Ok')
      droid.dialogShow()
      droid.dialogGetResponse()
      result = droid.getInput('Chat', 'Enter a message').result
      if result is None:
        break
      droid.bluetoothWrite(result + '\n')
    
    droid.exit()
    


    最后,要查看蓝牙命令的完整列表,请查看http://code.google.com/p/android-scripting/wiki/ApiReference 并向下滚动到蓝牙外观。祝你好运!

    【讨论】:

    • @Mathew,SL4A BT api 是否支持扫描其他 BT 设备?我没有找到那个 API,有没有办法包含那个 API 并构建?谢谢,V
    • 不幸的是,我不这么认为,虽然我不是专家,所以如果你看到任何东西,请告诉我!
    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 2021-03-12
    • 1970-01-01
    • 2016-07-10
    • 2017-11-06
    • 2013-10-25
    • 2013-05-26
    • 2014-02-18
    相关资源
    最近更新 更多