BluetoothGattCallback
?是 Android 中用于處理藍牙低功耗(BLE)設備通信的核心回調類。它負責處理與 BLE 設備的連接、服務發現、數據讀寫等操作的結果。以下是對?BluetoothGattCallback
?的詳細解析:
1.?onConnectionStateChange
-
觸發時機:當與 BLE 設備的連接狀態發生變化時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象,表示當前連接的 GATT 客戶端。 -
status
:連接狀態的變化結果,BluetoothGatt.GATT_SUCCESS
?表示成功。 -
newState
:新的連接狀態,可能的值有?BluetoothProfile.STATE_CONNECTED
?或?BluetoothProfile.STATE_DISCONNECTED
。
-
-
常見操作:
-
連接成功后,調用?
gatt.discoverServices()
?開始發現服務。 -
斷開連接后,釋放資源或嘗試重新連接。
-
java
復制
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {if (newState == BluetoothProfile.STATE_CONNECTED) {// 連接成功,開始發現服務gatt.discoverServices();} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {// 斷開連接,釋放資源gatt.close();} }
2.?onServicesDiscovered
-
觸發時機:當發現 BLE 設備的服務完成時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
status
:服務發現的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
獲取服務列表并查找特定的特征(Characteristic)或描述符(Descriptor)。
-
java
復制
@Override public void onServicesDiscovered(BluetoothGatt gatt, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {List<BluetoothGattService> services = gatt.getServices();for (BluetoothGattService service : services) {// 處理每個服務}} }
3.?onCharacteristicRead
-
觸發時機:當讀取特征值完成時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
characteristic
:被讀取的特征對象。 -
status
:讀取操作的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
處理讀取到的特征值。
-
java
復制
@Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {byte[] data = characteristic.getValue();// 處理讀取到的數據} }
4.?onCharacteristicWrite
-
觸發時機:當寫入特征值完成時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
characteristic
:被寫入的特征對象。 -
status
:寫入操作的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
確認寫入操作是否成功。
-
java
復制
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {// 寫入成功} }
5.?onCharacteristicChanged
-
觸發時機:當特征值發生變化時觸發(通常是由于通知或指示)。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
characteristic
:發生變化的特征對象。
-
-
常見操作:
-
處理特征值的變化。
-
java
復制
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {byte[] data = characteristic.getValue();// 處理變化的數據 }
6.?onDescriptorRead
-
觸發時機:當讀取描述符完成時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
descriptor
:被讀取的描述符對象。 -
status
:讀取操作的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
處理讀取到的描述符值。
-
java
復制
@Override public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {byte[] data = descriptor.getValue();// 處理讀取到的描述符數據} }
7.?onDescriptorWrite
-
觸發時機:當寫入描述符完成時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
descriptor
:被寫入的描述符對象。 -
status
:寫入操作的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
確認寫入操作是否成功。
-
java
復制
@Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {// 寫入成功} }
8.?onReadRemoteRssi
-
觸發時機:當讀取遠程設備的 RSSI(信號強度)完成時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
rssi
:讀取到的 RSSI 值。 -
status
:讀取操作的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
處理讀取到的 RSSI 值。
-
java
復制
@Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {// 處理 RSSI 值} }
9.?onMtuChanged
-
觸發時機:當 MTU(最大傳輸單元)大小發生變化時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
mtu
:新的 MTU 大小。 -
status
:MTU 變化的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
處理 MTU 變化后的數據傳輸。
-
java
復制
@Override public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {// 處理 MTU 變化} }
10.?onPhyUpdate
-
觸發時機:當物理層(PHY)更新完成時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
txPhy
:發送端的 PHY。 -
rxPhy
:接收端的 PHY。 -
status
:PHY 更新的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
處理 PHY 更新后的通信。
-
java
復制
@Override public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {// 處理 PHY 更新} }
11.?onPhyRead
-
觸發時機:當讀取物理層(PHY)信息完成時觸發。
-
參數:
-
gatt
:BluetoothGatt
?對象。 -
txPhy
:發送端的 PHY。 -
rxPhy
:接收端的 PHY。 -
status
:讀取操作的結果,BluetoothGatt.GATT_SUCCESS
?表示成功。
-
-
常見操作:
-
處理讀取到的 PHY 信息。
-
java
復制
@Override public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {// 處理讀取到的 PHY 信息} }
總結
BluetoothGattCallback
?是 Android BLE 開發中非常重要的類,它提供了與 BLE 設備交互的各種回調方法。開發者需要根據具體的業務需求,實現這些回調方法來處理連接、服務發現、數據讀寫等操作的結果。