This article describes the android BLE Bluetooth 4.0, that is, API level >= 18, and Bluetooth 4.0 mobile phone can be used, if the phone system version API level < 18, is not used Bluetooth 4.0 OH.
first send the official demo, interested in the past to see: http://developer.android.com/guide/topics/connectivity/bluetooth-le.html. Android system more than 4.3, mobile phone support Bluetooth 4.0, with search, pairing, connection, Discovery service and eigenvalues, disconnection and other functions: http://download.csdn.net/detail/lqw770737185/8116019. first, understand the API and concepts 1.1 Bluetoothgatt inherit Bluetoothprofile, through Bluetoothgatt can connect the device (connect), Discover the service (discoverservices), and return the corresponding property to Bluetoothgattcallback 1.2 bluetoothgattcharacteristic equivalent to a data type that includes a description of value and 0~n value (bluetoothgattdescriptor) 1.3 Bluetoothgattdescriptor Descriptor , description of characteristic, including scope, unit of measurement, etc. 1.4 Bluetoothgattservice A collection of services, characteristic. 1.5 Bluetoothprofile a generic specification for sending and receiving data according to this specification. 1.6 Bluetoothmanager get Bluetoothadapter with Bluetoothmanager Bluetoothmanager Bluetoothmanager = (bluetoothmanager) getsystemservice (context.bluetooth_service);1.7 Bluetoothadapter an Android system has only one bluetoothadapter, which gets through Bluetoothmanager Bluetoothadapter mbluetoothadapter = Bluetoothmanager.getadapter ();1.8 Bluetoothgattcallback The results that are returned after certain operations on the device have been connected on the device. Here must be reminded that after the device has been connected to return, did not return to seriously see if there is no connection on the device. private Bluetoothgattcallback Gattcallback = new Bluetoothgattcallback () {//There are 9 ways to implement it, see what the situation is going to accomplish, and use that to achieve thosePublic void Onconnectionstatechange (Bluetoothgatt gatt, int status, int newstate) {};Public void Oncharacteristicwrite (Bluetoothgatt GATT, bluetoothgattcharacteristic characteristic, int status) {};< /c0>};Bluetoothdevice device = mbluetoothadapter.getremotedevice (address);Bluetoothgatt GATT = Device.connectgatt (this, false, mgattcallback);1.8.1:notification corresponds to oncharacteristicchanged; gatt.setcharacteristicnotification (characteristic, true);1.8.2:readcharacteristic corresponds to Oncharacteristicread; gatt.readcharacteristic (characteristic);1.8.3:writecharacteristic corresponds to Oncharacteristicwrite; gatt.wirtecharacteristic (mcurrentcharacteristic);1.8.4: Connect bluetooth or disconnect bluetooth corresponding onconnectionstatechange; 1.8.5:readdescriptor corresponds to Ondescriptorread; 1.8.6:writedescriptor corresponds to Ondescriptorwrite; Gatt.writedescriptor (descriptor);1.8.7:readremoterssi corresponds to Onreadremoterssi; Gatt.readremoterssi ()1.8.8:executereliablewrite corresponds to onreliablewritecompleted; 1.8.9:discoverservices corresponds to onservicesdiscovered. gatt.discoverservices ()1.9 Bluetoothdevice discover connected devices after scanning to get connected devices second, turn on Bluetooth permissions <uses-permission android:name= "Android.permission.BLUETOOTH"/><uses-permission android:name= "Android.permission.BLUETOOTH_ADMIN"/><uses-feature android:name= "Android.hardware.bluetooth_le" android:required= "true"/>if Android.hardware.bluetooth_le is set to false, it can be installed on an unsupported device to determine if Bluetooth 4.0 is supported for code. if (!getpackagemanager (). Hassystemfeature (Packagemanager.feature_bluetooth_le)) {Toast.maketext (This, "device does not support Bluetooth 4.0", Toast.length_short). Show ();finish ();}third, the start and close operation of Bluetooth 1. Use the system to turn on the Bluetooth dialog box by default if (Mbluetoothadapter = = NULL | |!mbluetoothadapter.isenabled ()) {Intent enablebtintent = new Intent (bluetoothadapter.action_request_enable);Startactivityforresult (enablebtintent, REQUEST_ENABLE_BT);}2, the background to open Bluetooth, do not make any hint, this can also be used to customize the Bluetooth dialog box mbluetoothadapter.enable ();3. Turn off Bluetooth in the background mbluetoothadapter.disable ();Four, scanning equipment, connection equipment, access equipment information, disconnect equipment, self-View the official demo, or see the demo is clearer ah
Android ble Bluetooth 4.0 Summary