Android ble 藍芽4 0 總結

2021-09-08 17:35:05 字數 2733 閱讀 5452

本文介紹android ble 藍芽4.0,也就是說api level >= 18,且支援藍芽4.0的手機才可以使用,如果手機系統版本api level < 18,也是用不了藍芽4.0的哦。

一、了解api及概念

1.1 bluetoothgatt

繼承bluetoothprofile,通過bluetoothgatt可以連線裝置(connect),發現服務(discoverservices),並把相應地屬性返回到bluetoothgattcallback 

1.2 bluetoothgattcharacteristic

相當於乙個資料型別,它包括乙個value和0~n個value的描述(bluetoothgattdescriptor)

1.3 bluetoothgattdescriptor

描述符,對characteristic的描述,包括範圍、計量單位等

1.4 bluetoothgattservice

服務,characteristic的集合。

1.5 bluetoothprofile

乙個通用的規範,按照這個規範來收發資料。

1.6 bluetoothmanager

通過bluetoothmanager來獲取bluetoothadapter

bluetoothmanager bluetoothmanager = (bluetoothmanager) getsystemservice(context.bluetooth_service);

1.7 bluetoothadapter

乙個android

系統只有乙個bluetoothadapter ,通過bluetoothmanager 獲取

bluetoothadapter mbluetoothadapter = bluetoothmanager.getadapter();

1.8 bluetoothgattcallback

已經連線上裝置,對裝置的某些操作後返回的結果。這裡必須提醒下,已經連線上裝置後的才可以返回,沒有返回的認真看看有沒有連線上裝置。

private bluetoothgattcallback gattcallback = new bluetoothgattcallback() ;

public void oncharacteristicwrite(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic, int status){};

};bluetoothdevice device = mbluetoothadapter.getremotedevice(address);

bluetoothgatt gatt = device.connectgatt(this, false, mgattcallback);

1.8.1:notification對應oncharacteristicchanged;

gatt.setcharacteristicnotification(characteristic, true);

1.8.2:readcharacteristic對應oncharacteristicread;

gatt.readcharacteristic(characteristic);

1.8.3: writecharacteristic對應oncharacteristicwrite;

gatt.wirtecharacteristic(mcurrentcharacteristic);

1.8.4:連線藍芽或者斷開藍芽 對應 onconnectionstatechange;

1.8.5: readdescriptor對應ondescriptorread;

1.8.6:writedescriptor對應ondescriptorwrite;

gatt.writedescriptor(descriptor);

1.8.7:readremoterssi對應onreadremoterssi;

gatt.readremoterssi()

1.8.8:executereliablewrite對應onreliablewritecompleted;

1.8.9:discoverservices對應onservicesdiscovered。

gatt.discoverservices()

1.9 bluetoothdevice

掃瞄後發現可連線的裝置,獲取已經連線的裝置

二、開啟藍芽許可權

如果 android.hardware.bluetooth_le設定為false,可以安裝在不支援的裝置上使用,判斷是否支援藍芽4.0用以下**就可以了

if (!getpackagemanager().hassystemfeature(packagemanager.feature_bluetooth_le))

三、對藍芽的啟動關閉操作

1、利用系統預設開啟藍芽對話方塊

if (mbluetoothadapter == null || !mbluetoothadapter.isenabled())

2、後台開啟藍芽,不做任何提示,這個也可以用來自定義開啟藍芽對話方塊啦

mbluetoothadapter.enable();

3、後台關閉藍芽

mbluetoothadapter.disable();

四、掃瞄裝置,連線裝置,獲取裝置資訊 ,斷開連線裝置,自行檢視官方demo,還是看demo比較清晰啊

Android ble 藍芽4 0 總結一

本文介紹 android ble 藍芽4.0,也就是說api level 18,且支援藍芽4.0的手機才可以使用,如果手機 系統版本api level 18,也是用不了藍芽4.0的哦。首先發一下官方的demo,有興趣的可以過去看看 系統4.3以上,手機支援藍芽4.0,具有搜尋,配對,連線,發現服務及...

Android BLE藍芽使用 一

一 是否支援ble藍芽private boolean issupportbluetooth boolean istoast return false mainactivity context finish final bluetoothmanager bluetoothmanager bluetoo...

藍芽4 0 概述

v4.0 版本 藍芽4.0包括三個子規範,即傳統藍芽技術 高速藍芽技術和新的藍芽低功耗技術。藍芽4.0的改進之處主要體現在三個方面,電池續航時間 節能和裝置種類上。擁有低成本,跨廠商互操作性,3毫秒低延遲 100公尺以上超長距離 aes 128加密等諸多特色,藍芽4.0的有效傳輸距離有很大提公升。3...