Android Bluetooth Development

Source: Internet
Author: User

In viewAndroidThere are few examples of Bluetooth development, and there are also few examples of Bluetooth development, so the information is not comprehensive.

Android supports Bluetooth development only from sdk 2.0, and simulators are not supported. Testing requires at least two mobile phones, which restricts the development of many technical personnel.

First, add permissions to AndroidManifest. xml to perform Bluetooth operations.

 
 
  1. <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" /> 
  2. <uses-permissionandroid:nameuses-permissionandroid:name="android.permission.BLUETOOTH" /> 

Next, let's look at the api. All Android bluetooth development classes are under the android. bluetooth package. For example, there are only eight classes.

We only need to use the following:

1. Bluetooth adapter, as its name implies, must be operated continuously until we establish a thsocket connection.

There are many methods in BluetoothAdapter, which are commonly used as follows:

CancelDiscovery () is literally called to cancel the discovery, that is, when we are searching for a device, calling this method will not continue searching.

Disable () disable Bluetooth

Enable, however, you will be prompted:

 
 
  1. Intemtenabler = new Intent (effecthadapter. ACTION_REQUEST_ENABLE );
  2. StartActivityForResult (enabler, reCode); // same as startActivity (enabler );

GetAddress () Get the local Bluetooth address

GetDefaultAdapter () obtains the default thadapter. In fact, only this method can be used to obtain the thadapter.

GetName () Get the local Bluetooth name

GetRemoteDevice (String address) obtains the remote Bluetooth device based on the Bluetooth address.

GetState () gets the current status of the local Bluetooth adapter and feels it may be needed for debugging)

IsDiscovering () determines whether the device is currently being searched. true is returned.

IsEnabled () determines whether the Bluetooth is enabled. If enabled, true is returned. Otherwise, false is returned.

ListenUsingRfcommWithServiceRecord (String name, UUID uuid) based on the name, UUID is created and returns BluetoothServerSocket, which is the first step to create the BluetoothSocket server.

StartDiscovery () to start searching. This is the first step of searching.

2. You can see the name of the thdevice. This class describes a bluetooth device.

CreateRfcommSocketToServiceRecord (UUIDuuid) is created based on UUID and returns a effecthsocket

This method is also the purpose of getting ththdevice -- to create a thsocket

Other methods of this class, such as getAddress () and getName (), are the same as those of descrithadapter.

3. I believe everyone will be familiar with the Bluetooth removal of javasthserversocket. Since it is a Socket, the methods should be similar,

This class has only three methods.

The difference between the two overloaded accept () and accept (inttimeout) is that later Methods specify the expiration time. Note that when you execute these two methods, the thread will be blocked until the client request is received or expires. 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.

Close () is not required. Translate -- close!

4. ipvthsocket, which is opposite to ipvthserversocket and is a client

A total of five methods will be used without accident

Close (), close

Connect () Connection

GetInptuStream () gets the input stream

GetOutputStream () gets the output stream

GetRemoteDevice () gets the remote device. This indicates the remote Bluetooth device that is connected to the specified initthsocket.

Build an Android Development Environment

VPN network settings and usage for Android mobile phones

Android development basics and SDK Installation Guide

Android library so file and skia function call

Android uidesign: Build the application parameter setting Screen

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.