首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SL4A (Python)和蓝牙

使用SL4A (Python)和蓝牙
EN

Stack Overflow用户
提问于 2012-08-02 11:21:08
回答 1查看 11.5K关注 0票数 3

寻找一些指点,让我开始吧。

在我的左手中有一个运行ICS的SGS2。我已经启动并运行了SL4A,并且安装了Python2.6.2

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

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

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

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

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-22 09:36:30

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

代码语言:javascript
复制
bluetoothAccept
bluetoothActiveConnections
bluetoothConnect
bluetoothDiscoveryCancel
bluetoothDiscoveryStart
bluetoothGetConnectedDeviceName
bluetoothGetLocalAddress
bluetoothGetLocalName
bluetoothGetRemoteDeviceName
bluetoothGetScanMode
bluetoothIsDiscovering
bluetoothMakeDiscoverable
bluetoothRead
bluetoothReadBinary
bluetoothReadLine
bluetoothReadReady
bluetoothSetLocalName
bluetoothStop
bluetoothWrite
bluetoothWriteBinary
checkBluetoothState
toggleBluetoothState

要调用这些命令中的任何一个,您可以执行如下操作

代码语言:javascript
复制
import android
droid = android.Android()
#call your commands with droid.bluetoothcommand
droid.bluetoothDiscoveryStart()
#or
droid.toggleBluetoothState(True)

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

代码语言:javascript
复制
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并向下滚动到蓝牙外观。祝你好运!

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11770594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档