Bluetooth for Android

Source: Internet
Author: User

Bluetooth for Android
I. Bluetooth package Introduction

The Android platform provides an android. bluetooth package that enables communication between bluetooth devices. There are a total of 8 classes, and the four frequently used classes are as follows:

BluetoothAdapter class

Represents a local Bluetooth adapter. It is the entry point for all Bluetooth interactions. With this feature, you can find other Bluetooth devices, query the bound devices, instantiate a bluetooth device with a known MAC address, and create a rabbitthserversocket (as the server side) to listen for connections from other devices.

BluetoothDevice class

Represents a remote Bluetooth device that uses it to request a remote Bluetooth device to connect to or obtain the name, address, type, and binding status of the remote Bluetooth device (the information is encapsulated in javasthsocket ).

BluetoothSocket class

Represents a Bluetooth socket interface (similar to a socket in TCP), which is a connection point for applications to communicate with other Bluetooth devices through input and output streams.

BlueboothServerSocket class

Indicates opening a service connection to listen for possible connection requests (belonging to the server). To connect two bluetooth devices, one device must be used as the server to open a service socket. When the remote device initiates a connection request and the connection has been reached, the BlueboothServerSocket class will return a descrithsocket.

Ii. Use of common classes Descrithadapter: Bluetooth adapter
CancelDiscovery () cancels the exploration. When we are searching for a device, this method will no longer be used to search for disable (). disable Bluetooth enable (). If this method is enabled, no prompt will be displayed, in more cases, we need to ask whether the user is enabled. The following two lines of code also enable Bluetooth, but will prompt the user: Intentenabler = new Intent (effecthadapter. ACTION_REQUEST_ENABLE); startActivity (enabler); getAddress () Get the local Bluetooth address getdefaadapter adapter () Get the default descrithadapter. In fact, only this method gets the descrithadaptergetname () get the local Bluetooth name getRemoteDevice (String address) Get the remote Bluetooth device getState () Get the current status of the local Bluetooth adapter isDiscovering () Determine whether the current device is searching, if yes, trueisEnabled () is returned to determine whether Bluetooth is enabled. If yes, true is returned. Otherwise, falselistenUsingRfcommWithServiceRecord (String name, UUID uuid) is returned. Based on the name, UUID is created and BluetoothServerSocket, this is the first step for creating the strongthsocket server. startDiscovery () is used to start searching. This is the first step for searching.
Descrithdevice: remote Bluetooth device
CreateRfcommSocketToServiceRecord (UUIDuuid, the same as descrithadapter.
Ipvthsocket: Client
// This class has six methods close () to close connect () to isConnected () to determine whether to connect to getInptuStream () to get the input stream getOutputStream () to get the output stream getRemoteDevice () gets the specified remote Bluetooth device connected to ipvthsocket.
Descrithserversocket: Server
// This class has four methods: accept () accept (int timeout) close () close getChannel () return the channel bound to this socket

The difference between the two is that the later method specifies the expiration time. It should be noted that when the two methods are executed, the thread will be blocked until the client request is received (or after expiration, it should be run in the new thread. Note that both methods return a thsocket, and the final connection is the connection between the server and the client.

Iii. Data Transmission Bluetooth data transmission-server side

1. Obtain the thadapter.
2. Create a thserversocket object using the descrithadapter. listenUsingRfcommWithServiceRecord (name, UUID uuid) method.
3. Return a thsocket object through the luetoothServerSocket. accept () method. Because this method is in the blocking status, you need to enable the thread for processing.
4. Use the descrithsocket. getInputStream () and descrithsocket. getOutputStream () methods to obtain the InputStream and OutputStream objects for Data Reading and writing.
5. Use the InputStream. read () method to read data. Use the OutputStream. write () method to write data.

Bluetooth data transmission-Client

1. Obtain the thadapter.
2. Use descrithadapter. getRemoteDevice (String address) to obtain the thdevice object of the specified address.
3. Create a thsocket object using the effecthdevice. createRfcommSocketToServiceRecord (UUID uuid) method.
4. connect to the bluetooth device using the thsocket. connect () method.
5. Use the descrithsocket. getInputStream () and descrithsocket. getOutputStream () methods to obtain the InputStream and OutputStream objects for Data Reading and writing.
6. Use the InputStream. read () method to read data. Use the OutputStream. write () method to write data.

4. Tip UUID
// UUID: globally unique identifier, in the format of 8-4-4-4-12 // The same UUID must be used to connect two bluetooth devices.
Permissions to be added for Bluetooth use

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.