Android: Bluetooth 4.0-BLE-Summary = 1.0, android4.0-ble-

Source: Internet
Author: User

Android: Bluetooth 4.0-BLE-Summary = 1.0, android4.0-ble-
Requirement: BLE connection and communication (currently-enable device-search device-connect device) resources: some demos and official documents from the network

:


1-BLE is an API provided by Android4.3 or later-some conceptual things are necessary.
Currently, Android real machines can only accept and control products-that is, the client-server end is currently only a product-and apple seems to be able to use mobile phones as the receiving end or as the sending end-
The basic concept is the central and peripheral -- the mobile phone is the central -- the product is the peripheral -- and the two are connected through BLE --
I understand the information transfer in this way. The GATT protocol uses ATT as the basis. The information contained in the GATT protocol is Server. The server contains many characti... each feature is the fixed information we need.
If you have an improper understanding, you can point it out-after all, I have only a few days of understanding and understanding of this piece-

2-go to the official section below.
APP permissions = permission to be declared to call a bluetooth device =

<span style="font-size:14px;"><uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /></span>

= Sometimes all functions of our APP are implemented based on Bluetooth, so we need to note that this device is only applicable to Bluetooth =

<span style="font-size:14px;"><uses-feature        android:name="android.hardware.bluetooth_le"        android:required="true" /></span>

3-create a new project-TestBlueTooth

Paste MainActivity. java code

Package com. example. testbluetooth; import java. util. arrayList; import android. app. activity; import android. app. listActivity; import android. bluetooth. export thadapter; import android. bluetooth. descrithadapter. leScanCallback; import android. bluetooth. export thdevice; import android. bluetooth. bluetoothManager; import android. content. context; import android. content. intent; import android. content. pm. packageMana Ger; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. listView; import android. widget. textView; import android. widget. toast; public class MainActivity extends ListActivity {priva Te final static String TAG = "BlueToohth_Test"; private LeDeviceListAdapter mLeDeviceListAdapter; private BluetoothAdapter mblustmthadapter; private boolean mScanning; private Handler mHandler; private static final int REQUEST_ENABLE_BT = 1; // Stops scanning after 10 seconds. private static final long SCAN_PERIOD = 10000; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedI NstanceState); getActionBar (). setTitle (R. string. title_devices); mHandler = new Handler (); // determines whether BLELog is supported. I (TAG, "wheter_support_BLE"); if (! GetPackageManager (). hasSystemFeature (PackageManager. feature_effecth_le) {Toast. makeText (this, R. string. ble_not_supported, Toast. LENGTH_SHORT ). show (); finish ();} Log. I (TAG, "get_about_BLE_API"); final descrithmanager = (descrithmanager) getSystemService (Context. descrith_service); mblustmthadapter = descrithmanager. getAdapter (); if (mbluw.thadapter = null) {Toast. makeText (th Is, R. string. error_effecth_not_supported, Toast. LENGTH_SHORT ). show (); finish (); return; }}@ Overrideprotected void onResume () {super. onResume (); // determine the Bluetooth connection // Ensures Bluetooth is enabled on the device. if Bluetooth is notLog. I (TAG, "connect_ble"); if (! Mblustmthadapter. isEnabled () {if (! Mblustmthadapter. isEnabled () {Intent enableBtIntent = new Intent (effecthadapter. ACTION_REQUEST_ENABLE); startActivityForResult (enableBtIntent, REQUEST_ENABLE_BT) ;}// Initializes list view adapter. mLeDeviceListAdapter = new LeDeviceListAdapter (); setListAdapter (mLeDeviceListAdapter); scanLeDevice (true );} /*** perform a real scan on the searched bluetooth device * the device will receive a prompt every time it is scanned and the device is retrieved.-Use the yydatasetchange Method * @ author Admin **/priv Ate class LeDeviceListAdapter extends BaseAdapter {private ArrayList <shortthdevice> mLeDevices; private extends mInflator; public LeDeviceListAdapter () {super (); mLeDevices = new ArrayList <shortthdevice> (); mInflator = MainActivity. this. getLayoutInflater ();} // Add the device to the public void addDevice (effecthdevice device) {if (! MLeDevices. contains (device) {mLeDevices. add (device) ;}/// obtain the device public synchronized thdevice getDevice (int position) {return mLeDevices. get (position);} // clear the data in the list public void clear () {mLeDevices. clear () ;}@ Overridepublic int getCount () {return mLeDevices. size () ;}@ Overridepublic Object getItem (int I) {return mLeDevices. get (I) ;}@ Overridepublic long getItemId (int I) {return I ;}@ Overridepublic View getView (in T I, View view, ViewGroup viewGroup) {ViewHolder viewHolder; // General ListView optimization code. if (view = null) {view = mInflator. inflate (R. layout. listitem_device, null); viewHolder = new ViewHolder (); viewHolder. deviceAddress = (TextView) view. findViewById (R. id. device_address); viewHolder. deviceName = (TextView) view. findViewById (R. id. device_name); view. setTag (viewHolder);} else {viewHolder = (Vi EwHolder) view. getTag () ;}// the corresponding device processes thdevice device = mLeDevices. get (I); final String deviceName = device. getName (); if (deviceName! = Null & deviceName. length ()> 0) viewHolder. deviceName. setText (deviceName); elseviewHolder. deviceName. setText (R. string. unknown_device); viewHolder. deviceAddress. setText (device. getAddress (); return view;} static class ViewHolder {TextView deviceName; TextView deviceAddress ;} /*** scan device ** @ param enable */private void scanLeDevice (final boolean enable) {if (enable) {// Stops scanning after a pre-define D scan period. mHandler. postDelayed (new Runnable () {@ Override public void run () {mScanning = false; mbluw.thadapter. stopLeScan (mLeScanCallback); invalidateOptionsMenu () ;}}, SCAN_PERIOD); mScanning = true; mbluw.thadapter. startLeScan (mLeScanCallback);} else {mScanning = false; mbluw.thadapter. stopLeScan (mLeScanCallback);} invalidateOptionsMenu (); // update menu bar information} // set private Blu for the response of the scanned bluetooth device Descrithadapter. leScanCallback mLeScanCallback = new systthadapter. leScanCallback () {@ Override public void onLeScan (final effecthdevice device, int Arg, byte [] scanRecord) {runOnUiThread (new Runnable () {@ Override public void run () {mLeDeviceListAdapter. addDevice (device); mLeDeviceListAdapter. notifyDataSetChanged () ;}}) ;};@ Override protected void onPause () {super. onPause (); scanLeDevi Ce (false); mLeDeviceListAdapter. clear ();} // scan and pause options @ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); if (! MScanning) {menu. findItem (R. id. menu_stop ). setVisible (false); menu. findItem (R. id. menu_scan ). setVisible (true); menu. findItem (R. id. menu_refresh ). setActionView (null);} else {menu. findItem (R. id. menu_stop ). setVisible (true); menu. findItem (R. id. menu_scan ). setVisible (false); menu. findItem (R. id. menu_refresh ). setActionView (R. layout. actionbar_indeterminate_progress);} return true;} // Click Event @ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case R. id. menu_scan: mLeDeviceListAdapter. clear (); scanLeDevice (true); break; case R. id. menu_stop: scanLeDevice (false); break;} return true;}/* listen to events for each subitem * @ see android. app. listActivity # onListItemClick (android. widget. listView, android. view. view, int, long) * // @ Override protected void onListItemClick (ListView l, View v, int position, long id) {final incluthdevice device = mLeDeviceListAdapter. getDevice (position); if (device = null) return; final Intent intent = new Intent (this, DeviceControlActivity. class); intent. putExtra (DeviceControlActivity. EXTRAS_DEVICE_NAME, device. getName (); intent. putExtra (DeviceControlActivity. EXTRAS_DEVICE_ADDRESS, device. getAddress (); if (mScanning) {mbluw.thadapter. stopLeScan (mLeScanCallback); mScanning = false;} startActivity (intent);} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {// User chose not to enable Bluetooth. if (requestCode = REQUEST_ENABLE_BT & resultCode = Activity. RESULT_CANCELED) {finish (); return;} super. onActivityResult (requestCode, resultCode, data );}}


3.1-post --- listitem_device.xml
</pre><pre name="code" class="java"><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="wrap_content">    <TextView android:id="@+id/device_name"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textSize="24dp"/>    <TextView android:id="@+id/device_address"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textSize="12dp"/></LinearLayout>


To be continued 




How to Set Bluetooth visibility for android40

My website is htct328w Android 4.0
In the settings window, you can click the Bluetooth option to set the visibility (not click to switch)

Bluetooth 40 BLE support

Hi!
Hahaha what rom can I fl my 2 S to 41 million pixels ??
Bluetooth in meters 2 is 4.0
Xiaomi enterprise Platform [Official Certification]

Related Article

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.