android蓝牙python,android – 使用SL4A(Python)和蓝牙

该博客提供了一种使用SL4A(Android Scripting Environment)和Python通过蓝牙与RFID阅读器建立连接的方法。作者分享了如何启动蓝牙连接、接收和发送数据的步骤,并提供了相关的Python命令示例,帮助初学者入门蓝牙通信与RFID数据捕获。
摘要由CSDN通过智能技术生成

寻找一些让我入门的建议.

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

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

我希望他们玩得很好 – 我想编写一些能够继续观看设备并在传输时捕获代码的脚本.

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

不同寻常的是,我遇到了实际问题 – 我找不到任何“开始使用蓝牙和SL4A”资源来克服建立持久连接和监控输出的第一步.

有小费吗?

解决方法:

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

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()

标签:android,python,bluetooth,sl4a

来源: https://codeday.me/bug/20190725/1537943.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值