Initial experience _android Android Bluetooth technology

Source: Internet
Author: User
Tags uuid

One: Introduction to Bluetooth Kits

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, and the four commonly used classes are as follows:

Bluetoothadapter class

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 create a bluetoothserversocket (as a server) 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 or to obtain the name, address, type, and binding status of the remote Bluetooth device (the information is encapsulated in Bluetoothsocket).

Bluetoothsocket class

Represents a Bluetooth socket interface (similar to a socket in TCP), which is a connection point that the application communicates with other Bluetooth devices through input and output streams.

Blueboothserversocket class

On behalf of the Open service connection to listen for possible connection requests (belonging to the server side), to connect two Bluetooth devices, you must have a device open a service socket as a server. The Blueboothserversocket class returns a bluetoothsocket when a remote device initiates a connection request and is already connected.

Second: The use of common classes

Bluetoothadapter: Bluetooth Adapter

Canceldiscovery () Cancel the search, call this method when we are searching for the device and will no longer continue searching
Disable () Turn off Bluetooth
Enable () Turn on Bluetooth, this method turns on Bluetooth will not pop-up prompts, more times we need to ask the user whether to open, the following two lines of code is also open Bluetooth, but will prompt the user:

Intentenabler = new Intent (bluetoothadapter.action_request_enable);
StartActivity (enabler);
GetAddress () get local Bluetooth address
Getdefaultadapter () Gets the default Bluetoothadapter, which is actually the only way to get Bluetoothadapter
GetName () get local bluetooth name
Getremotedevice (String address) to obtain a remote Bluetooth device from a Bluetooth location
GetState () Gets the current state of the local Bluetooth adapter
Isdiscovering () to determine if the device is currently being looked up, return true
IsEnabled () Determines if Bluetooth is turned on, is turned on to return true, otherwise returns false
Listenusingrfcommwithservicerecord (String name,uuid uuid) by name, UUID creates and returns Bluetoothserversocket, This is the first step to creating the Bluetoothsocket server side
StartDiscovery () Start the search, this is the first step of the search

Bluetoothdevice: Remote Bluetooth device

Createrfcommsockettoservicerecord (UUIDUUID) creates and returns a bluetoothsocket based on the UUID, which is also the way we get Bluetoothdevice
The purpose--Create Bluetoothsocket

This class of other methods, such as getaddress (), GetName (), and so on, with Bluetoothadapter.

Bluetoothsocket: Client

This class has a total of 6 methods
Close () closes connect
() connection isconnected () to
determine whether to connect
getinptustream () get input stream
Getoutputstream () gets the output stream
Getremotedevice () Gets the remote Bluetooth device bluetoothsocket The specified connection

Bluetoothserversocket: Service Side

There are 4 methods in this class
Accept ()
Accept (int timeout)
Close () off
Getchannel () returns the channel bound by this socket

The difference is that the following method specifies the elapsed time, and it should be noted that when the two methods are executed, the thread is blocked and should be run in a new thread until the client's request (or expiration) is received. 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

Third: Data transmission

Bluetooth data Transfer-server-side

1, get bluetoothadapter.
2, the Bluetoothserversocket object is created through the Bluetoothadapter.listenusingrfcommwithservicerecord (Name,uuid UUID) method.
3. Returns a Bluetoothsocket object through the Luetoothserversocket.accept () method. Because the method is blocked, you need to open the thread to handle it.
4, through the Bluetoothsocket.getinputstream () and Bluetoothsocket.getoutputstream () method to obtain read and write Data InputStream and OutputStream objects.
5, through the Inputstream.read () method to read data. The Outputstream.write () method is used to write the data.

Bluetooth data Transfer--client

1, get bluetoothadapter.
2, through Bluetoothadapter.getremotedevice (String address) to obtain the Bluetoothdevice object of the specified addresses.
3, create the Bluetoothsocket object through the Bluetoothdevice.createrfcommsockettoservicerecord (UUID uuid) method.
4, through the Bluetoothsocket.connect () method to connect Bluetooth devices.
5, through the Bluetoothsocket.getinputstream () and Bluetoothsocket.getoutputstream () method to obtain read and write Data InputStream and OutputStream objects.
6, through the Inputstream.read () method to read data. The Outputstream.write () method is used to write the data.

Four: Tip

Uuid

UUID: Globally unique identifier in the format: 8-4-4-4-12
//Two Bluetooth devices are connected using the same UUID

<uses-permissionandroid:name= " Android.permission.BLUETOOTH_ADMIN "/>
<uses-permissionandroid:name=" Android.permission.BLUETOOTH " >

The above is a small set for you to share the first experience of Android Bluetooth Bluetooth technology related content, the next chapter will introduce the Android Bluetooth Bluetooth technology to use the process of detailed, interested friends can click to learn more details.

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.