Android 低功耗(BLE)藍牙開發說明

BLE(Bluetooth Low Energy)低功耗藍牙興起的原因

BLE藍牙的興起主要是因為可穿戴設備的流行,由于傳統藍牙不能滿足可穿戴設備的續航要求,因此大部分可穿戴設備采用藍牙4.0技術,即BLE藍牙技術。

BLE的特點

快速搜索、快速連接、超低功耗連接和數據傳輸,但是數據傳輸速率低。

在Android開發中,BLE藍牙一包數據最多20個字節,因此在Android系統下最好不要使用BLE藍牙傳輸大量的數據

BLE藍牙開發流程

1、申請藍牙權限

<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

注意:在Android6.0以上需要打開位置,不然搜索不到藍牙設備

2、獲取藍牙適配器,判斷本地藍牙是否可用,打開藍牙

/*獲取藍牙適配器*/

mBluetoothManager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);

mBluetoothAdapter =mBluetoothManager.getAdapter();

/*** 判斷藍牙是否可用*/

public boolean isBlueAdapterEnable() {

return mBluetoothAdapter !=null &&mBluetoothAdapter.isEnabled();

}

如果本地藍牙未打開,調用已下代碼打開本地藍牙

/*?打開藍牙適配器*/

Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

context.startActivityForResult(intent, 0);

3、建立BluetoothAdapter.LeScanCall類的對象

BluetoothAdapter.LeScanCallback scanCallback =new BluetoothAdapter.LeScanCallback() {

@Override

? ? public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

? ? ? ? //TODO處理搜索到的藍牙設備邏輯

????}

};

4、搜索藍牙設備

mBluetoothAdapter .startLeScan(scanCallback);

5、停止搜索藍牙設備

mBluetoothAdapter.stopLeScan(scanCallback);

6、建立BluetoothGattCallBack類對象

private BluetoothGattCallbackgattCallback =new BluetoothGattCallback() {

????????/* 斷開或連接 狀態發生變化時調用* */

? ? ? ? ????@Override

? ? ? ? ????public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

????????????????super.onConnectionStateChange(gatt, status, newState);

? ? ? ? ? ? ????//連接成功

? ? ? ? ? ? ????if (newState == BluetoothGatt.STATE_CONNECTED) {

????????????????????Log.i(TAG, "連接成功");

? ? ? ? ? ? ? ? ????gatt.discoverServices();

? ? ? ? ? ? ????}else {

???????????????????Log.i(TAG, "連接失敗");

?????????????????connected=false;

? ? ? ? ? ? }

????}

????????/* 發現設備(真正建立連接)*/

? ? ? ? @Override

? ? ? ? public void onServicesDiscovered(BluetoothGatt gatt, int status) {

????????????super.onServicesDiscovered(gatt, status);

? ? ? ? ? ? //直到這里才是真正建立了可通信的連接

????????????connected=ture;?

? ? ? ? ? ? //獲取初始化服務和特征值

? ? ? ? ? ? mBluetoothGatt = gatt;

? ? ? ? ? ? initServiceAndChara(mBluetoothGatt);

? ? ? ? ? ? //訂閱通知

? ? ? ? ? ? mBluetoothGatt.setCharacteristicNotification(mBluetoothGatt.getService(NotifyUUID).getCharacteristic(NOTIFY_DESCRIPTOR), true);

? ? ? ? }

????/* 讀操作的回調*/

? ? ? ? @Override

? ? ? ? public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

????????????super.onCharacteristicRead(gatt, characteristic, status);

? ? ? ? ? ? Log.e(TAG, "onCharacteristicRead()");

? ? ? ? }

????/* 寫操作的回調*/

? ? ? ? @Override

? ? ? ? public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

????????????super.onCharacteristicWrite(gatt, characteristic, status);

? ? ? ? ? ? Log.e(TAG, "onCharacteristicWrite()? status=" + status +",value=" + HexUtil.encodeHexStr(characteristic.getValue()));

? ? ? ? }

????/ * 接收到硬件返回的數據 */

? ? ? ? @Override

? ? ? ? public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

????????????super.onCharacteristicChanged(gatt, characteristic);

? ? ? ? ? ? Log.e(TAG,"onCharacteristicChanged()"+characteristic.getValue());

????}

};

/*初始化mBlueGatt的服務和特征*/

private void initServiceAndChara(BluetoothGatt mBluetoothGatt) {

????List bluetoothGattServices = mBluetoothGatt.getServices();

? ? for (BluetoothGattService bluetoothGattService : bluetoothGattServices) {

????????List characteristics = bluetoothGattService.getCharacteristics();

? ? ? ? for (BluetoothGattCharacteristic characteristic : characteristics) {

????????????int charaProp = characteristic.getProperties();

? ? ? ? ? ? if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) >0) {

????????????????NotifyUUID = bluetoothGattService.getUuid();

? ? ? ? ? ? ? ? NOTIFY_DESCRIPTOR = characteristic.getUuid();

? ? ? ? ? ? }

????????????if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) >0) {

????????????????bleWriteGattCharacteristic = characteristic;

? ? ? ? ? ? }

????????????if ((charaProp & BluetoothGattCharacteristic.PROPERTY_READ)>0){

????????????????bleReadGattCharacteristic=characteristic;

? ? ? ? ? ? }

????????????Log.i("NOTIFY_DESCRIPTOR", "NotifyUUID:" +NotifyUUID +",NOTIFY_DESCRIPTOR" +NOTIFY_DESCRIPTOR);

? ? ? ? }

????}

}

7、讀取藍牙數據

public void readBleDeviceData(){

if (connected &&mBluetoothGatt!=null){

mBluetoothGatt.readCharacteristic(bleReadGattCharacteristic);

? ? }

}

8、向藍牙設備發送數據

bleWriteGattCharacteristic.setValue(byte[] bytes);

mBluetoothGatt.writeCharacteristic(bleWriteGattCharacteristic);

9、斷開藍牙設備連接

mBluetoothGatt.disconnect();?

Android 低功耗(BLE)藍牙開發說明(二)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容