Android Bluetooth technology Bluetooth first Experience
Source: Internet
Author: User
<span id="Label3"></p><strong><strong>one: Introduction to Bluetooth package</strong></strong><p><p>The Android platform provides a Android.bluetooth package that implements the Bluetooth API for communication between Bluetooth Devices. There are a total of 8 classes, The four classes commonly used are as follows:</p></p><strong><strong>Bluetoothadapter class</strong></strong><p><p>Represents a local Bluetooth adapter. It is the entry point for all Bluetooth interactions. With it you can find other Bluetooth devices, query the bundled device, instantiate a Bluetooth device with a known Mac address, and establish a bluetoothserversocket (as the server Side) to listen for connections from other Devices.</p></p><strong><strong>Bluetoothdevice class</strong></strong><p><p>Represents a remote Bluetooth device that uses it to request a remote Bluetooth device connection or to obtain the name, address, kind, and binding status of the remote Bluetooth device (the information is encapsulated in bluetoothsocket).</p></p><strong><strong>Bluetoothsocket class</strong></strong><p><p>Represents a Bluetooth socket interface (similar to a socket in tcp), which is the connection point that the application communicates with other Bluetooth devices through the input and output streams.</p></p><strong><strong>Blueboothserversocket class</strong></strong><p><p>The representative opens a service connection to listen for possible connection requests (which belong to the server side), in order to connect two Bluetooth devices must have one device open as a server for a service socket. The Blueboothserversocket class will return a bluetoothsocket when the remote device initiates a connection request and is already connected.</p></p><strong><strong>second: the use of common classes</strong></strong><strong><strong>Bluetoothadapter: Bluetooth Adapter</strong></strong><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs javascript">cancelDiscovery()取消探索,当我们正在搜索设备的时候调用这个方法将不再继续搜索disable()关闭蓝牙enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,以下两行代码同样是打开蓝牙,但会提示用户: <span class="hljs-keyword">new</span> Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(enabler);getAddress()获取本地蓝牙地址getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdaptergetName()获取本地蓝牙名称getRemoteDevice(<span class="hljs-built_in">String</span> address)根据蓝牙地址获取远程蓝牙设备getState()获取本地蓝牙适配器当前状态isDiscovering()判断当前是否正在查找设备,是则返回<span class="hljs-literal">true</span>isEnabled()判断蓝牙是否打开,已打开返回<span class="hljs-literal">true</span>,否则返回<span class="hljs-literal">false</span>listenUsingRfcommWithServiceRecord(<span class="hljs-built_in">String</span> name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步startDiscovery()开始搜索,这是搜索的第一步</code></pre></pre><strong><strong>bluetoothdevice: remote Bluetooth device</strong></strong><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs erlang"><span class="hljs-function"><span class="hljs-title">createRfcommSocketToServiceRecord</span><span class="hljs-params">(<span class="hljs-variable">UUIDuuid</span>)</span>根据UUID创建并返回一个B<span class="hljs-title">luetoothSocket</span>,这个方法也是我们获取B<span class="hljs-title">luetoothDevice</span>的目的——创建B<span class="hljs-title">luetoothSocket</span>这个类其他的方法,如<span class="hljs-title">getAddress</span><span class="hljs-params">()</span>、<span class="hljs-title">getName</span><span class="hljs-params">()</span>等,同B<span class="hljs-title">luetoothAdapter</span>。</span></code></pre></pre><strong><strong>bluetoothsocket: Client</strong></strong><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs scss"><span class="hljs-comment">//这个类一共有6个方法</span><span class="hljs-function">close()</span>关闭<span class="hljs-function">connect()</span>连接<span class="hljs-function">isConnected()</span>判断是否连接<span class="hljs-function">getInptuStream()</span>获取输入流<span class="hljs-function">getOutputStream()</span>获取输出流<span class="hljs-function">getRemoteDevice()</span>获取BluetoothSocket指定连接的远程蓝牙设备</code></pre></pre><strong><strong>bluetoothserversocket: Service Side</strong></strong><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs scss"><span class="hljs-comment">//这个类一共有4个方法</span><span class="hljs-function">accept()</span><span class="hljs-function">accept(int timeout)</span><span class="hljs-function">close()</span>关闭<span class="hljs-function">getChannel()</span>返回这个套接字绑定的通道</code></pre></pre><p><p><strong>The difference between the two is that the latter method specifies the obsolete time, it should be noted that when the two methods are executed, until the Client's request (or after expiration), will block the thread, should be placed in the new thread to Run. It is also important to note that both methods return a bluetoothsocket, and the final connection is the Server-side connection to the Client's two Bluetoothsocket</strong></p></p><strong><strong>three: Data Transmission</strong></strong><strong><strong>bluetooth data Transfer-server side</strong></strong><p><p>1, Get Bluetoothadapter.<br>2. Create the Bluetoothserversocket object through the Bluetoothadapter.listenusingrfcommwithservicerecord (name,uuid UUID) method.<br>3. Returns a Bluetoothsocket object by means of the luetoothserversocket.accept () method. Because the method is in a blocking state, the thread needs to be opened for Processing.<br>4. The InputStream and OutputStream objects for reading and writing data are obtained through the Bluetoothsocket.getinputstream () and Bluetoothsocket.getoutputstream () methods.<br>5, through the Inputstream.read () method to read the Data. Write the data by using the Outputstream.write () method.</p></p><strong><strong>bluetooth Data transfer--client</strong></strong><p><p>1, Get Bluetoothadapter.<br>2. Obtain the Bluetoothdevice object of the specified address through Bluetoothadapter.getremotedevice (String address).<br>3. Create the Bluetoothsocket object through the Bluetoothdevice.createrfcommsockettoservicerecord (uuid uuid) method.<br>4, through the Bluetoothsocket.connect () method to connect the Bluetooth device.<br>5. The InputStream and OutputStream objects for reading and writing data are obtained through the Bluetoothsocket.getinputstream () and Bluetoothsocket.getoutputstream () methods.<br>6, through the Inputstream.read () method to read the Data. Write the data by using the Outputstream.write () method.</p></p><strong><strong>four: Tip</strong></strong><strong><strong>UUID</strong></strong><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-comment">// UUID:全局唯一标识符,格式为:8-4-4-4-12</span><span class="hljs-comment">// 两个蓝牙设备进行连接需要使用同一个UUID</span></code></pre></pre><strong><strong>Bluetooth Use the permissions you need to add</strong></strong><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs applescript"><uses-permissionandroid:<span class="hljs-property">name</span>=<span class="hljs-string">"android.permission.BLUETOOTH_ADMIN"</span> /><uses-permissionandroid:<span class="hljs-property">name</span>=<span class="hljs-string">"android.permission.BLUETOOTH"</span> /></code></pre></pre> <p><p>Android Bluetooth technology Bluetooth first Experience</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service