2012-08-02 143 views
3

寻找一些指引让我开始。使用SL4A(Python)和蓝牙

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

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

我希望它们能够很好地播放 - 我想编写一些将继续观看设备并在传输时捕获代码的内容。

我不是Python专家,但我一直在使用它一段时间来在Web服务器上构建简单的I/O作业,所以我可以找到我的方式。

不寻常的是,我遇到了实际的问题 - 我找不到任何'开始使用蓝牙和SL4A'资源来完成建立持续连接和监视输出的第一步。

任何提示?

回答

7

看来你需要的是蓝牙门面。下面是关于蓝牙一些命令,可能会对你有所帮助:

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中,但为了清晰起见,我添加了注释:

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并向下滚动到蓝牙门面。祝你好运!

+0

@Mathew,SL4A BT api是否支持扫描其他BT设备?我没有找到该API,有没有一种方法来包含该API并构建?谢谢, V – vik86 2013-10-28 19:40:52

+0

不幸的是,我不这么认为,我几乎不是专家,尽管如此,让我知道,如果你看到任何东西! – 2013-11-16 04:05:24