简体   繁体   English

如何构建Android蓝牙服务器应用程序以处理使用pybluez从Raspberry Pi发送的数据

[英]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.

相关问题 通过蓝牙将树莓派2的字符串变量发送到android应用 - send string variable from raspberry pi 2 to android app over bluetooth 通过蓝牙从Android中的Raspberry Pi接收数据 - Receiving data from raspberry pi in android via bluetooth 如何使用 Android 应用程序通过蓝牙连接到 Raspberry pi - How to connect to Raspberry pi with an android app over bluetooth 如何通过蓝牙连接从Android App在Raspberry Pi上运行脚本 - How to run a script on Raspberry Pi from Android App over Bluetooth Connection 如何将 android 应用程序连接到树莓派,以便它将传感器的所有数据反映到 android 应用程序上 - How to connect android app to raspberry pi so that it will reflect all the data from sensors on to the android app Pybluez客户端到Android蓝牙套接字服务器 - Pybluez client to Android Bluetooth socket server 将数据从我的App android发送到我的Raspberry PI 3 - Send data from my App android to my Raspberry PI 3 通过Wifi将数据从Android App发送到Raspberry Pi - Sending data over Wifi from Android App to Raspberry Pi Raspberry Pi3和android app蓝牙发送消息但不接收 - Raspberry Pi3 and android app bluetooth sending messages but not receiving 如何通过蓝牙将Android应用程序连接到Raspberry Pi,以发送数字文本文件? - How can I connect an Android app to Raspberry Pi, via Bluetooth, in order to send a text file of numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM