android-bluetooth-< 14 >

Source: Internet
Author: User

1. bluetooth® (Bluetooth)

    • Bluetooth is currently the most widely used wireless communication protocol
    • Mainly for short-range equipment communication (10M)
    • Commonly used for linking headphones , mouse and mobile communication devices , etc.

2. Bluetooth-related APIs

1.BluetoothAdapter: The class object represents the local Bluetooth adapter

2.BluetoothDevice: Represents a remote Bluetooth device

Note: You will need to use the android3.0 version above

3. Set the relevant permissions

To declare the rights of Bluetooth in the manifest file: <uses-permission android:name= "Android.permission.BLUETOOTH"/>

4. Code steps:

    1. Obtain Bluetoothadapter object;
    2. Determine if the current device has a Bluetooth device;
    3. Determine if the Bluetooth device in the current device is turned on, and if it is not open, it can be started;
    4. Get all paired Bluetooth device objects;
 Public classBluetoothdemoactivityextendsActivity {/**Called when the activity is first created.*/    PrivateButton bt=NULL; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); BT=(Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {//Listen for the "Search paired Bluetooth" button                /*Add permissions in the manifest file first * 1. Create a new Bluetoothadapter Object * 2. Determine if the Bluetooth device exists * 3. Determine if Bluetooth is turned on * 4. Get the surrounding Bluetooth device (bluetoothdriver) **/                //1. Create a local Bluetooth adapterBluetoothadapter adapter=Bluetoothadapter.getdefaultadapter (); //2. Determine if there is a Bluetooth device                if(adapter!=NULL)                {                    //3. Determine if open, not open, start with intent                    if(!adapter.isenabled ()) {Intent Intent=NewIntent (bluetoothadapter.action_request_enable); Startactivityforresult (Intent,0);; }                    //4. Find a paired Bluetooth device (note: Yes paired)Set<bluetoothdevice> ds=adapter.getbondeddevices (); //loop out the Bluetooth device,                    if(Ds.size () >0)                    {                         for(Bluetoothdevice d:ds) System.out.println ("Paired Bluetooth Device:" +d.getaddress ()); }                }                ElseSystem.out.println ("No Bluetooth device on the phone");            }        }); }}

5. Bluetooth visibility (can be detected by other Bluetooth scanning)

Set Visibility: You need to add permissions to the manifest file <uses-permission android:name= "Android.permission.BLUETOOTH_ADMIN"/> Get the Manage Bluetooth permissions.

//Setting the visibility listener (local bluetooth scan is visible)    Private classDiscoverbluetoothImplementsview.onclicklistener{ Public voidOnClick (View v) {/** 1. Create a new "Bluetooth visibility" request intent intent * 2. Initiate a request for the intent join setting visibility duration * 3.startActivity () * */            //1. Set the intent of the requestIntent discoverintent=NewIntent (bluetoothadapter.action_request_discoverable); //2. Set the visibility time (default maximum is 300, so setting more than 300 is reset to)Discoverintent.putextra (bluetoothadapter.extra_discoverable_duration,400 ); //3. Initiating the requeststartactivity (discoverintent); }            }

6. Scan other Bluetooth devices (startdiscovery)

 Public classBluetoothdemo2activityextendsActivity {/**Called when the activity is first created.*/    //get local bluetooth    PrivateBluetoothadapter badapter=Bluetoothadapter.getdefaultadapter (); @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //register Broadcastreceiver, and don't forget the destroy method of activity.//Unregisterreceiver (bluetoothreceiver);Intentfilter intentfilter=NewIntentfilter (Bluetoothdevice.action_found);    Registerreceiver (Bluetoothreceiver,intentfilter); }    //start the "Scan Bluetooth" button listener    Private classScanbluetoothdeviceImplementsView.onclicklistener { Public voidOnClick (View v) {//Start the scan! Badapter.startdiscovery (); }            }    //processing requests for scans    PrivateBroadcastreceiver bluetoothreceiver=NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {//processing the originating request hereString action =intent.getaction (); if(BluetoothDevice.ACTION_FOUND.equals (ACTION)) {//You can remove objects that represent the remote Bluetooth adapter from the intent object you receiveBluetoothdevice device =Intent.getparcelableextra (Bluetoothdevice.extra_device);            System.out.println (Device.getaddress ());  }        }            }; }

Android-bluetooth-< 14;

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.