First of all, to operate Bluetooth, first to add permissions in the Androidmanifest.xml
<uses-permissionandroid:name= "Android.permission.BLUETOOTH_ADMIN"/>
<uses-permissionandroid:name= "Android.permission.BLUETOOTH"/>
then, look at the API, Android all classes on Bluetooth development are under the Android.bluetooth package, with a total of 8 classes
And we need to use only a few:
1.BluetoothAdapter As the name implies, the Bluetooth adapter will continue to operate it until we establish the Bluetoothsocket connection.
Bluetoothadapter There are a number of methods, commonly used in the following:
canceldiscovery () by literal means, is to cancel the discovery, that is, when we are searching the device, call this method will not continue to search
Disable () turn off Bluetooth
enable () turn on Bluetooth, this method to turn on Bluetooth will not pop up the prompt, more time we need to ask the user whether to open, the two lines of code is also open Bluetooth, but will prompt the user:
Intemtenabler=new Intent (bluetoothadapter.action_request_enable);
Startactivityforresult (Enabler,recode);// with startactivity (enabler);
getaddress () get local Bluetooth address
Getdefaultadapter () Get the default bluetoothadapter, in fact, there's only one way to get Bluetoothadapter
GetName () get local bluetooth name
Getremotedevice (String address) get a remote Bluetooth device based on the Bluetooth address
getState () Get the current status of the local Bluetooth adapter (more needed when it feels possible to debug)
isdiscovering () to determine whether a device is currently being located, returns True
isenabled () Determines whether Bluetooth is turned on, turned on returns True, otherwise, returns false
Listenusingrfcommwithservicerecord (String name,uuid UUID) by name, the UUID creates and returns Bluetoothserversocket, which is the first step in creating the Bluetoothsocket server side
startdiscovery () Start the search, this is the first step in the search
2.BluetoothDevice Look at the name to know that this class describes a Bluetooth device
Createrfcommsockettoservicerecord (Uuiduuid) creates and returns a bluetoothsocket based on the UUID
This approach is also the purpose of our acquisition of Bluetoothdevice-creating Bluetoothsocket
Other methods of this class, such as GetAddress (), GetName (), with Bluetoothadapter
3.BluetoothServerSocket If you remove the Bluetooth I believe we must be familiar with, since it is the socket, the method should be similar,
There are only three ways of this class
Two overloaded accept (), accept (inttimeout) The difference is that the following method specifies the obsolete time, it should be noted that when the two methods are executed, until the client's request (or after the 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
Close () This is needless to say, translation--close!
4.BluetoothSocket, as opposed to Bluetoothserversocket, the client
A total of 5 methods, no accident, will be used
Close (), Close
Connect () Connection
Getinptustream () Get input stream
Getoutputstream () get output stream
Getremotedevice () Gets the remote device, which refers to the remote Bluetooth device that gets the bluetoothsocket specified connection.