[英]How to build Android Bluetooth server app handling data sent from Raspberry Pi using pybluez
I was wondering how to build Android app handling data sent from Raspberry pi. 我想知道如何构建处理从Raspberry pi发送的数据的Android应用程序。
I installed pybluez module on Raspberry Pi and send data using following python script. 我在Raspberry Pi上安装了pybluez模块,并使用以下python脚本发送数据。
import bluetooth
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((targetBluetoothMacAddress, port))
#targetBluetoothMacAddress is my phone MacAddress
sock.send("hello!!")
sock.close()
On my phone I did see two devices paired successfully. 在手机上,我确实看到两个设备成功配对。 But can not find a way to get data sent from Raspberry Pi. 但是找不到从Raspberry Pi发送数据的方法。 Is there a way to build an app handling data from sock.send()? 有没有一种方法可以构建一个处理来自sock.send()的数据的应用程序?
You need to consult the Android Bluetooth APIs, but yes you can build an app pretty easily. 您需要查阅Android蓝牙API,但是可以,您可以轻松构建应用。 First what you'll wanna do is get a reference to the BluetoothDevice that represents the Raspberry Pi you wanna connect to. 首先,您想要做的是获得对BluetoothDevice的引用,该蓝牙设备代表您要连接的Raspberry Pi。 You can do this by calling BluetoothAdapter.startDiscovery() or by directly asking the framework for a device with the MAC address of the Raspberry Pi by calling BluetoothAdapter.getRemoteDevice(...) . 您可以通过调用做到这一点BluetoothAdapter.startDiscovery()或通过调用直接询问与树莓派的MAC地址的设备框架BluetoothAdapter.getRemoteDevice(...) 。
Once you get have the device you'll wanna open BluetoothSocket with the device. 拥有设备后,您将要使用该设备打开BluetoothSocket 。 To do this call createRfcommSocketToServiceRecord(UUID) . 为此,请调用createRfcommSocketToServiceRecord(UUID) 。 The UUID argument will more thank likely "0001101-0000-1000-8000-00805F9B34FB" if you're connecting to the Raspberry Pi using SPP. 如果您要使用SPP连接到Raspberry Pi,则UUID参数将更可能是“ 0001101-0000-1000-8000-00805F9B34FB”。 The create method will return to you a BluetoothSocket which you will need to call connect() on. create方法将为您返回一个BluetoothSocket,您需要在其上调用connect() 。 Note, connect is a blocking call and you will need to perform all this work in a worker thread to prevent locking up your UI. 注意,connect是一个阻塞调用,您将需要在辅助线程中执行所有这些工作,以防止锁定UI。 If connect returns successfully you've successfully connected with the device. 如果连接成功返回,则说明您已成功连接设备。 In order to achieve back and forth communication with the device, you'll need to get ahold to it's Input and Output streams by calling the following two methods getInputStream() and getOutputStream() on BluetoothSocket . 为了实现与该设备的来回通信,您需要通过在BluetoothSocket上调用以下两个方法getInputStream()和getOutputStream()来保持其输入和输出流。
Once you have these two streams you can send byte data back and forth between the two devices. 一旦有了这两个流,就可以在两个设备之间来回发送字节数据。 note that reading and writing from the steams are blocking operations so I recommend creating two separate threads for reading and writing data, passing in the streams and a Handler into the thread constructor so you can send back data back to the UI thread. 请注意,从流中进行读取和写入是阻塞操作,因此我建议创建两个用于读取和写入数据的单独线程,将流和Handler传递到线程构造函数中,以便可以将数据发送回UI线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.