Common actions for Android bluetooth

Source: Internet
Author: User

Recently on the Bluetooth operation of Android devices to do some research, the following to do some summary, version is 4.4, the list of solutions are mostly from the network, thanks to the powerful netizens:

The operation of Bluetooth can be divided into regular operation, and unconventional operation. The so-called normal operation, is the interface is prompted, requires the customer permission to do some operations. Unconventional is usually the use of reflection and other means to achieve the purpose of unknowingly connecting Bluetooth.

A. General operation:

1. Get the Bluetooth operating interface:

Bluetoothadapter mbtadapter = Bluetoothadapter.getdefaultadapter ();

Bluetooth related operations are basically through the above class.

2. Turn on the Bluetooth device on this computer:

if (!   Mbtadapter.isenabled ()) {Intent enableintent = new Intent (bluetoothadapter.action_request_enable); Startactivityforresult (enableintent, 0);}

3. Turn on the visibility of Bluetooth:

if (mbtadapter.isenabled ()) { Intent visibleintent = new Intent (bluetoothadapter.action_request_discoverable)  ;  Visibleintent.putextra (bluetoothadapter.extra_discoverable_duration, 300); The later MS is at most startactivity (visibleintent);} 

4. The Bluetooth device is turned on to communicate with other devices, so you need to scan the available devices around:

Register two intent and define receiver to accept callback events for "Discover device" and "done" during Bluetooth device detection
 intentfilter discoveryfilter = new Intentfilter (bluetoothdevice.action_found); 
Intentfilter discoveryfinishedfilter = new Intentfilter (bluetoothadapter.action_discovery_finished);
Broadcastreceiver discoverreceiver = new Broadcastreceiver () {
    @Override
    Public void OnReceive (Context context, Intent Intent) {
        String action = Intent.getaction ();
if (BluetoothDevice.ACTION_FOUND.equals (ACTION))  
{
Bluetoothdevice device = Intent.getparce  Lableextra (Bluetoothdevice.extra_device); Get another device in order to connect Operation

Else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals ACTION)  
{
//do Something
}
}
};
This.registerreceiver (Discoverreceiver, Discoveryfilter);
This.registerreceiver (Discoverreceiver, discoveryfinishedfilter);

/************************************/

Mbtadapter.startdiscovery (); // used to start searching for Bluetooth devices that are visible around you // If you want to stop during the discovery process, you can call the following API if (Mbtadapter.isdiscovering ()) { mbtadapter.canceldiscovery (); return ;}

5. As server side, wait for other devices to connect:

Bluetoothserversocket Mserversocket = Mbtadapter.listenusingrfcommwithservicerecord (PROTOCOL_SCHEME_RFCOMM, Uuid.fromstring ("00001101-0000-1000-8000-00805f9b34fb"));
Bluetoothsocket mclientsocket = mserversocket.accept ();

With the socket object, you can get the stream, you can use the stream's Read,write method to read and write data, read can be in the loop of the independent thread, to ensure continuous acceptance of the data.
InputStream instream = Mclientsocket.getinputstream ();
OutputStream OutStream = Mclientsocket.getoutputstream ();

6. As the client side, you can connect to the server:

Bluetoothdevice Mdevice = Mbtadapter.getremotedevice (Device.getaddress ()); The first thing to do is to get the server's device object, which can be obtained before discover, or through the previously recorded address.

If connect succeeds, it gets a socket connection, and then, like above, it can send and receive messages through the stream.
Mclientsocket = Mdevice.createrfcommsockettoservicerecord (uuid.fromstring ("00001101-0000-1000-8000-00805F9B34FB" ));
Mclientsocket.connect ();

Two. Unconventional operation:

1. Turn on the Bluetooth device on this computer:

This will not pop up the dialog box
If (! Mbtadapter.isenabled ()) {  mbtadapter.enable ();}

2. Turn on the visibility of Bluetooth:


Mbtadapter.isenabled () to make sure the equipment is ready.   
publicvoid setdiscoverabletimeout (int timeout) {

Try {
Method setdiscoverabletimeout= Bluetoothadapter.class. GetMethod ("Setdiscoverabletimeout",int.class); Setdiscoverabletimeout.setaccessible (true); Method Setscanmode=bluetoothadapter.class. GetMethod ("Setscanmode",int.class,int.class); Setscanmode.setaccessible (true);            Setdiscoverabletimeout.invoke (Mbtadapter, timeout);        Setscanmode.invoke (Mbtadapter, bluetoothadapter.scan_mode_connectable_discoverable,timeout); } Catch(Exception e) {e.printstacktrace (); }    }

3. Do bluetooth communication without pairing:

With these two APIs, it's amazing to be able to communicate with a Bluetooth device.
Mserversocket = Mbtadapter.listenusinginsecurerfcommwithservicerecord (Protocol_scheme_rfcomm, UUID.fromString (" 00001101-0000-1000-8000-00805F9B34FB "));
Mclientsocket = Mdevice.createinsecurerfcommsockettoservicerecord (Uuid.fromstring (" 00001101-0000-1000-8000-00805F9B34FB "));

Three. Finally, list the required permissions:

    <uses-permission android:name="android.permission.BLUETOOTH"/>    < Uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Bluetooth Reference page:

Http://stackoverflow.com/questions/5308373/how-to-create-insecure-rfcomm-socket-in-android

Http://stackoverflow.com/questions/5885438/bluetooth-pairing-without-user-confirmation

http://blog.csdn.net/zshq280017423/article/details/7645622

http://blog.csdn.net/menghnhhuan/article/details/7057484

http://blog.csdn.net/eric41050808/article/details/16967189

Http://www.2cto.com/kf/201312/261093.html

WIFI Reference page:

http://blog.csdn.net/ranger1111/article/details/6777153

http://lszdb1983.blog.163.com/blog/static/20426348201209251344/

Format Ah, how also do not,,,

Common actions for Android bluetooth

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.