标签:android style blog ar io os sp java on
BLE:全称为Bluetooth Low Energy。蓝牙规范4.0最重要的一个特性就是低功耗。BLE使得蓝牙设备可通过一粒纽扣电池供电以维持续工作数年之久。很明显,BLE使得蓝牙设备在钟表、远程控制、医疗保健及运动感应器等市场具有极光明的应用场景。
Google从Android 4.3开始添加了对蓝牙4.0的支持。本文一个demo为入口分析 BLE 搜索的流程。
package com.dy.ble; import android.annotation.SuppressLint; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private static final String TAG = "BLE"; private Button scanBtn; private BluetoothAdapter bluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(!bluetoothAdapter.isEnabled()){ bluetoothAdapter.enable(); } scanBtn = (Button) this.findViewById(R.id.btn_scan); scanBtn.setOnClickListener(new OnClickListener(){ @SuppressLint("NewApi") @Override public void onClick(View arg0) { if(bluetoothAdapter.isEnabled()){ bluetoothAdapter.startLeScan(callback); } } }); } @SuppressLint("NewApi") private BluetoothAdapter.LeScanCallback callback = new BluetoothAdapter.LeScanCallback(){ @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { Log.d(TAG, "onLeScan device = " + device + ",rssi = " + rssi + "scanRecord = " + scanRecord); } }; }
点击按钮就会开始扫描,扫描到设备时,就会触发onLeScan这个回调方法,并且可以从参数中获取扫描到的蓝牙设备信息。下面分析BluetoothAdapter中的startLeScan方法。
public boolean startLeScan(LeScanCallback callback) { return startLeScan(null, callback); }
public boolean startLeScan(UUID[] serviceUuids, LeScanCallback callback) { if (DBG) Log.d(TAG, "startLeScan(): " + serviceUuids); if (callback == null) { if (DBG) Log.e(TAG, "startLeScan: null callback"); return false; } synchronized(mLeScanClients) { if (mLeScanClients.containsKey(callback)) { if (DBG) Log.e(TAG, "LE Scan has already started"); return false; } try { IBluetoothGatt iGatt = mManagerService.getBluetoothGatt(); if (iGatt == null) { if (DBG) Log.e("BluetoothAdapterReceiver", "iGatt == null"); // BLE is not supported return false; } UUID uuid = UUID.randomUUID(); GattCallbackWrapper wrapper = new GattCallbackWrapper(this, callback, serviceUuids); iGatt.registerClient(new ParcelUuid(uuid), wrapper); if (wrapper.scanStarted()) { if (DBG) Log.e("BluetoothAdapterReceiver", "wrapper.scanStarted()==true"); mLeScanClients.put(callback, wrapper); return true; } } catch (RemoteException e) { Log.e(TAG,"",e); } } return false; }
Android4.4(MT8685)源码蓝牙解析--BLE搜索
标签:android style blog ar io os sp java on
原文地址:http://blog.csdn.net/u013467735/article/details/41962075