Android ble Bluetooth 4.0 Summary 1

Source: Internet
Author: User

Android ble Bluetooth 4.0 Summary 1
This article introduces Android ble Bluetooth 4.0, that is, API level> = 18, which can only be used by mobile phones that support Bluetooth 4.0. If the mobile phone system version API level is less than 18, Bluetooth 4.0 cannot be used. First, send an official demo. If you are interested, visit http://developer.android.com/guide/topics/connectivity/~th-le.html. Android 4.3 or above, mobile phone supports Bluetooth 4.0, with search, pairing, connection, service and feature value discovery, disconnection and other functions,: http://download.csdn.net/detail/lqw770737185/8116019. 1. Understand the api and concept 1.1 BluetoothGatt inherits BluetoothProfile, and can connect devices (connect) through BluetoothGatt to discover services (discoverServices ), return the corresponding attribute to BluetoothGattCallback 1.2 BluetoothGattCharacteristic equivalent to a data type, which includes a value and 0 ~ Description of n values (descrithgattdescriptor) 1.3 descrithgattdescriptor descriptor, description of Characteristic, including a set of 1.4 descrithgattservice and Characteristic services such as range and measurement unit. 1.5 define thprofile a general specification for sending and receiving data according to this specification. 1.6 BluetoothManager obtains the processing thadapter processing thmanager = (processing thmanager) getSystemService (Context. BLUETOOTH_SERVICE); 1.7 BluetoothAdapter an Android system has only one BluetoothAdapter. You can obtain BluetoothAdapter mBluetoothAdapter = BluetoothManager through bluetoothManager. getAdapter (); 1.8 then thgattcallback is connected to the device and the result returned after some operations on the device. Here, you must note that only those connected to the device can be returned. If not, check whether the device is connected. Private synchronized thgattcallback GattCallback = new BluetoothGattCallback () {// There are nine methods to be implemented here. Depending on the situation, the public void onConnectionStateChange (implements thgatt gatt, int status, int newState) {}; public void onCharacteristicWrite (descrithgatt gatt, descrithgattcharacteristic characteristic, int status) {};}; descrithdevice device = mbluw.thadapter. getRemoteDevice (address); descrithgatt gatt = dev Ice. 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 to or disconnect Bluetooth corresponding to onConnectionStateChange; 1.8.5: readDescriptor Corresponds to region; 1.8.6: region corresponds to onDescriptorWrite; gatt. Partition (descriptor); 1.8.7: readRemoteRssi corresponds to onReadRemoteRssi; gatt. readRemoteRssi () 1.8.8: region corresponds to region; 1.8.9: discoverServices corresponds. Gatt. discoverServices () 1.9 discover connected devices after scanning thdevice. Obtain connected devices. 2. Enable Bluetooth <uses-permission android: name = "android. permission. BLUETOOTH "/> <uses-permission android: name =" android. permission. export th_admin "/> <uses-feature android: name =" android. hardware. export th_le "android: required =" true "/> If android. hardware. bluetooth_le is set to false. It can be installed on an unsupported device and used to determine whether Bluetooth 4.0 supports the following code: if (! GetPackageManager (). hasSystemFeature (PackageManager. feature_effecth_le) {Toast. makeText (this, "Bluetooth 4.0 is not supported on the device", Toast. LENGTH_SHORT ). show (); finish () ;}3. Disable Bluetooth startup. 1. Enable the Bluetooth dialog box by default. if (mblustmthadapter = null |! Mblustmthadapter. isEnabled () {Intent enableBtIntent = new Intent (effecthadapter. ACTION_REQUEST_ENABLE); startActivityForResult (enableBtIntent, REQUEST_ENABLE_BT);} 2. Enable Bluetooth in the background without any prompts. You can also use the custom settings to open the Bluetooth dialog box mblustmthadapter. enable (); 3. Disable Bluetooth mblustmthadapter in the background. disable (); 4. Scan the device, connect to the device, obtain device information, disconnect the device, view the official demo, or view the demo clearly.

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.