Android的蓝牙实现基础 [英] android bluetooth implementation basics

查看:139
本文介绍了Android的蓝牙实现基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以用简单的话解释我在android的蓝牙例如需要UUID的。我看过一些关于它的文章,但它仍然没有得到明确的UUID的确切需要。
现在让我来解释一下你的什么,我想发展的情景:
我想开发一个Android应用程序用名为.xyz扩展文件传输数据例如
从我的手机到其他手机通过蓝牙。 IT是不是在所有必要的,接听电话也应该有,我使用的应用。我只是想将数据从我的应用程序转移到其他电话完蛋了。我不在乎什么接收器与数据一样。
我只想范围内连接到设备,并使用我的应用程序传输该文件
现在我应该怎么做呢?哪里UUID的作用来这里?我已阅读,UUID是我的应用程序,服务器和接收器应该意识到这个UUID,形成一个连接。但是,如果接收方不具备我的应用程序?它肯定不会知道我的应用UUID?那么如何将数据传输将成为可能?我只是想用蓝牙就没有一个特定的应用程序。
在这里,有什么我的应用程序应该做的?它是否应创建服务器插座/客户端套接字还是什么?以及为什么。

can anyone in simple words explain me the need of UUID in android bluetooth example. I have read some articles about it but it is still not getting clear the exact need of UUID. And now let me explain you the scenario of what I want to develop: I want to develop an android application to transfer data for example a "FILE with .xyz extension" from my phone to the other phone over bluetooth. IT IS NOT AT ALL NECESSARY THAT THE RECEIVING PHONE SHOULD ALSO HAVE THE APPLICATION THAT I AM USING. I just want to transfer data from my application to other phone and thats it. I don't care what the receiver does with the data. I just want to connect to a device within range and transfer that file using my application Now how should I do this? Where does the the role of UUID come here? I have read that UUID is for my application , and both the server and the receiver should be aware of this UUID to form a connection . But what if the receiver does not have the my application? It will surely not know my Applications UUID ? then how the data transfer will be possible? I simply want to use Bluetooth without concerning a specific application. In here, what my application should be doing? Should it be creating a server socket / a client socket or what? and Why.

在简单的词的解释是AP preciated(一些文章如果可能的话)。我不想有BluetoothChat建议定期答案。如果你不明白的问题,请让我知道,我会努力更加具体和阐述它。
这个问题的基本目标是明确使用蓝牙来自于一个Android手机上运行的应用两个设备(而不是应用程序)的使用UUID和数据传输的。

An explanation in simple words is appreciated(some articles if possible). I dont want regular answers having BluetoothChat suggestions. If you don't understand the question please let me know, I will try to be more specific and elaborate it for you. The basic goal of this question is to clarify the use of UUID and transfer data between two DEVICES (and not an application) using bluetooth from an application running on one android phone.

推荐答案

即使使用蓝牙,您可以创建一个客户端 - 服务器应用程序..有一个的BluetoothSocket
读到这里 http://developer.android.com/reference/android/bluetooth/BluetoothSocket html的

Even with bluetooth you can create a client-server application.. there is a BluetoothSocket read here http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html

现在,让我们说你有两个设备:

Now, lets say you have two devices:


  1. 装置A

  2. 设备B

和假设设备A发送数据到设备B,你没有说,如果设备B也将数据发送到设备A,所以我就描述了第一种情形当发送到B

and assume that device A sending data to device B, You didn't say if device B is also sending data to device A, so I'll just describe the first scenario when A send to B.

因此​​,在这种情况下,由于所有的数据都存储在设备中的,你想将它传送给设备B,这将是比较合理的创建设备A作为BluetoothServer和设备B为BluetoothClient这听服务器。

So in that case, since all the data is stored in device A and you want to send it to device B, it will be more reasonable to create device A as a BluetoothServer and device B as BluetoothClient which listening to the server.

但是...如果你想同时设备交换数据..你可以使他们中的一个作为服务器
并为他们每个人创建2个线程:

But.. If you want both of devices will exchange data.. you can make one of them as a server and for each one of them create 2 threads:


  1. 的线程发送数据

  2. 线程监听数据

所以他们都可以交换数据。

so both of them can exchange data..

另一件事..如果你曾经编写你注意到这是阻塞,直到有它连接到服务器的客户机的接受()方法,常规的客户服务器的..同样是具有蓝牙客户机 - 服务器应用程序。

Another thing.. if you ever programmed a regular client server you noticed the accept() method which is blocking until there is a client which connected to server.. the same is with Bluetooth client-server application.

摘要:


  1. 一台设备将充当服务器 - 所以你需要写一个服务器项目,并安装结果
    它在第一装置上

  1. One device will act as a server - so you'll need to write a server project and install
    it on the first device

二器件将作为一个客户端 - 所以你需要编写一个客户端项目,
第二个设备上安装

Second device will act as a client - so you'll need to write a client project and install it on the second device

不要忘记添加蓝牙权限清单文件这两个结果
项目。

Don't forget to add the bluetooth permission in the manifest file for both
projects.

这两个项目的需要,你在你的问题中提到的相同的UUID。
用简单的话两侧的需要UUID让他们知道每一个与他们是谁
通信
我觉得它更像是一个常规的客户端 - 服务器的端口..
我读的地方,用于RFC通信..你大概知道有结果
一些协议蓝牙像RFC,SDP等。

Both of the projects need the same UUID as you mentioned in your question. in simple words both of the sides need the UUID so they each know with who they're communicate I think it's more like a port in a regular client-server.. I read somewhere that is used for RFC communication.. as you probably know there are
some protocols for Bluetooth like RFC,SDP and etc..

编辑:
   在大部分的手机有一个配对过程,当你想通过发送数据
   bluethooth。所以,如果你不想使用客户端 - 服务器的版本,我想你可以
   做到这一点:

In most of the phones there is a pairing process when you want to send data through bluethooth. so if you don't want to use the client-server version, I think you can do this:


  1. 您的应用程序将搜索设备连接。 (配对过程)

  2. 配对您连接到另一台设备,只发送数据后

编辑2:
  你想从A发送数据到B吧?
  我会解释清楚..

EDIT 2: You want to send data from A to B right? I'll explain more clearly..

当你说客户端应该知道谁是服务器,需要插入端口您说得对,并
  的服务器的IP,这是正确的,并以这种方式工作的。

You're right when you said the client should know who is the server and need insert the port and the IP of the server this is correct and works in this way.

现在,看看..

服务器监听来自客户端的连接,当连接建立的通信
  就开始了。

The server listen to connection from clients, when a connection established the communication begins.


  1. 客户端请求数据

  2. 服务器在处理客户端的请求,并给他的相关资料

  1. Client ask for data
  2. The server processing the client request and send him the related data

因此​​,像任何数据:文件,数据库应存储在服务器端..

So any data like: Files, Databases should be stored on the server side..

现在你的情况,你要要发送的文件位于设备中的,而不是在设备B,
所以,如果一个设备是服务器,他将监听连接。当设备B连接到服务器
(设备A)通信开始..设备B可以请求从设备A文件..
此外,由于设备A为服务器,他甚至可以广播消息..指发送相同的消息
为被连接到他的所有客户端。

Now in your case, the files you want to send are located in device A and not in device B, So if device A is the server he will listen for connections.. when device B connects to the server (device A) the communication begins.. Device B can request for files from Device A.. Also, since device A is the server, he can even broadcast a message.. means send the same message for all clients which are connected to him.

但你想要做的是发送即使设备B没有要求它的文件,对不对?
如果你想要那个设备B也将发送文件设备A我不明白,所以让我们把它结果
进入场景:

But what you're want to do is to send file even if device b didn't ask for it, right? I don't understand if you want that device B will also send file to device A, so lets divide it
into scenarios:

只是设备A发送到B:
 在这种情况下,由于文件位于装置A,装置设备A具有数据,
 设备A为服务器和设备B是客户端。
 因此,当连接建立可以从A发送到B。

just device A send to B: In this case, since the files are located in device A, means device A have the data, device A is the server and device B is the client. So when connection established you can send from A to B.

这两种设备交换数据:
 在这种情况下,两个设备应该倾听对方,但只是之一应该行动
 作为服务器和其他的客户。意味着你需要在安装serverApp
 其中之一,所述clientApp为另一方。
 但他们每个人都可以发送和听其他。因此对于每一个需要创建
 螺纹与发送数据和处理接收到的数据的另一个线程处理

Both devices exchange data: In this case, both devices should listen to each other, but just one of the should act as a server and the other as a client. means that you need to install the serverApp on one of them and the clientApp on the other. But each of them can send and listen to other. so for each of the you need to create thread that handle with the sending data and another thread that handles the receiving data

这篇关于Android的蓝牙实现基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆