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:
- Obtain Bluetoothadapter object;
- Determine if the current device has a Bluetooth device;
- Determine if the Bluetooth device in the current device is turned on, and if it is not open, it can be started;
- 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;