简体   繁体   English

如何在Android Studio中通过蓝牙配对的2台设备之间发送数据?

[英]How do I go about sending data between 2 devices which are paired via Bluetooth in Android Studio?

I'm still new to Android studio and I'm having a lot of trouble with Bluetooth, I'm currently making an app where I need to send data between devices. 我还是Android Studio的新手,我在蓝牙方面遇到很多麻烦,目前正在开发一个需要在设备之间发送数据的应用程序。 I'm letting the phone's default Bluetooth setup do the pairing but now I need to know how to send the data, I know I need to use input and output streams but I don't know exactly how. 我让手机的默认蓝牙设置进行配对,但是现在我需要知道如何发送数据,我需要使用输入和输出流,但是我不知道该怎么做。

And yes I have searched all over Google, I've followed a lot of Bluetooth tutorials but none of them seem to really explain how to send data from one device to another. 是的,我在Google上搜索了很多东西,我遵循了很多蓝牙教程,但是它们似乎都没有真正解释如何将数据从一台设备发送到另一台设备。

Thanks in advance. 提前致谢。

After you establish a secure/insecure connection via bluetooth the rest is just socket programming simply. 通过蓝牙建立安全/不安全的连接后,剩下的只是简单的套接字编程。 That is lets think about sending a text. 那就是考虑发送文本。 We convert the text to byte and send that by Java OutputStream. 我们将文本转换为字节,然后通过Java OutputStream发送。 In the same manner for the data received we can get it by InputStream. 以相同的方式,我们可以通过InputStream获得接收到的数据。

But remember you need to maintain bunch of code and thread/handler to maintain state and others. 但是请记住,您需要维护大量代码和线程/处理程序以维护状态和其他状态。 Though the basic thing is simply socket programming over Bluetooth socket using the Bluetooth adapter. 尽管基本的事情就是使用蓝牙适配器通过蓝牙套接字进行套接字编程。 Have a look at the below repository in github. 看看下面的github中的存储库。 This creates a chatroom over bluetooth. 这将在蓝牙上创建一个聊天室。 ie it sends and receives string data 即它发送和接收字符串数据

https://github.com/zahansafallwa/Android-bluetooth-chat-with-emoji/tree/master/app/src/main/java/com/zahan/safallwa/donttalk https://github.com/zahansafallwa/Android-bluetooth-chat-with-emoji/tree/master/app/src/main/java/com/zahan/safallwa/donttalk

Specially have a look at the BluetoothChatService class. 特别看看BluetoothChatService类。 That contains codes related to sending data. 其中包含与发送数据有关的代码。 BluetoothChatService 蓝牙聊天服务

Edit: 编辑:

As per your comment lets think that your devices are paired and also connected. 根据您的评论,让我们认为您的设备已配对并且也已连接。 Now you only need to send the text. 现在,您只需要发送文本即可。 Declare a outputstream 声明outputstream

private final OutputStream mmOutStream;

Suppose you have a string. 假设您有一个字符串。 We convert it to byte. 我们将其转换为字节。 Then get our socket outputstream and send data by write() method 然后获取我们的套接字输出流并通过write()方法发送数据

String message="this is test data";
byte[] send = message.getBytes();
mmOutStream = socket.getOutputStream(); // here socket is the bluetooth socket you establish
mmOutStream.write(send);//this is what sends the message

Remember: 记得:

Edited code is for your understanding only. 编辑后的代码仅供您理解。 It is prescribed to use separate thread for sending data. 规定使用单独的线程发送数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM