簡體   English   中英

為什么使用 BluetoothGatt 將設備與 BLE 連接時會發現這么多特性?

[英]Why there is so many charateristics discovered when connecting device with BLE using BluetoothGatt?

我在 Arduino 和 Android 應用程序之間建立了聯系。 我成功使用了bluetoothGATT連接,但是有一個問題,為什么當我連接到Arduino時它顯示了許多特色服務。 我只是將 Arduino 的特性設置為19b10001-e8f2-537e-4f6c-d104768a1214 所以我認為如果我連接 BLE,它只會顯示19b10001-e8f2-537e-4f6c-d104768a1214 與我的想法相反,它向我展示了其他不同的特征。

這是我在屏幕上的結果。

在此處輸入圖像描述

這是我在 MainActivity.java 的 BluetoothGattCallback 上的代碼

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @SuppressLint("MissingPermission")
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            runOnUiThread(new Runnable() {
                public void run() {
                    logAdapter.add("BluetoothCallBack - newState : " + String.valueOf(newState));
                }
            });
            switch (newState){
                case 0:
                    runOnUiThread(new Runnable() {
                        public void run() {
                            logAdapter.add("device disconnected");
                        }
                    });
                    break;
                case 2:
                    runOnUiThread(new Runnable() {
                        public void run() {
                            logAdapter.add("device connected");
                        }
                    });
                    btGatt.discoverServices();
                    break;
                default:
                    runOnUiThread(new Runnable() {
                        public void run() {
                            logAdapter.add("unknown error : " + String.valueOf(newState));
                        }
                    });
                    break;
            }
            logAdapter.notifyDataSetChanged();
        }

        @Override
        public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
            // this will get called after the client initiates a BluetoothGatt.discoverServices() call
            MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    tvState.setText("device services have been discovered");
                }
            });
            displayGattServices(btGatt.getServices());
        }

    };

     private void displayGattServices(List<BluetoothGattService> gattServices) {
        if (gattServices == null) return;

        // Loops through available GATT Services.
        for (BluetoothGattService gattService : gattServices) {

            final String uuid = gattService.getUuid().toString();
            System.out.println("Service discovered: " + uuid);
            MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    logAdapter.add("Service discovered: "+uuid);
                    logAdapter.notifyDataSetChanged();
                }
            });
            new ArrayList<HashMap<String, String>>();
            List<BluetoothGattCharacteristic> gattCharacteristics =
                    gattService.getCharacteristics();

            // Loops through available Characteristics.
            for (BluetoothGattCharacteristic gattCharacteristic :
                    gattCharacteristics) {

                final String charUuid = gattCharacteristic.getUuid().toString();
                System.out.println("Characteristic discovered for service: " + charUuid);
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        logAdapter.add("Characteristic discovered for service: "+charUuid+"\n");
                        logAdapter.notifyDataSetChanged();
                    }
                });
            }
        }
    }

這是我的 Arduino 代碼

#include <ArduinoBLE.h>
      
enum {
  GESTURE_NONE  = -1,
  GESTURE_UP    = 0,
  GESTURE_DOWN  = 1,
  GESTURE_LEFT  = 2,
  GESTURE_RIGHT = 3
};

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

int gesture = -1;

BLEService gestureService(deviceServiceUuid); 
BLEByteCharacteristic gestureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);


void setup() {
  Serial.begin(9600);
  while (!Serial);  
    
  if (!BLE.begin()) {
    Serial.println("- Starting Bluetooth® Low Energy module failed!");
    while (1);
  }

  BLE.setLocalName("Arduino");
  BLE.setAdvertisedService(gestureService);
  gestureService.addCharacteristic(gestureCharacteristic);
  BLE.addService(gestureService);
  gestureCharacteristic.writeValue(-1);
  BLE.advertise();

  Serial.println("Nano 33 BLE (Peripheral Device)");
  Serial.println(" ");
  Serial.println("- Discovering central device...");
}

void loop() {
  BLEDevice central = BLE.central();
  
  delay(500);
  gesture = 10;

  if (central) {
    Serial.println("* Connected to central device!");
    Serial.print("* Device MAC address: ");
    Serial.println(central.address());
    Serial.println(" ");

    while (central.connected()) {
      if (gestureCharacteristic.written()) {
         gesture = gestureCharacteristic.value();
         
       }
//       gestureCharacteristic.writeValue((byte)gesture);
//       Serial.println("* Writing value to gesture_type characteristic done!");
//       Serial.println(" ");
//       delay(2000);
    }
    
    Serial.println("* Disconnected to central device!");
  }
}

有人能解釋一下為什么發現了很多特征嗎?

默認情況下,您的 Arduino 上可能會通過您正在使用的藍牙堆棧啟用某些服務。 使用通用 BLE 掃描儀應用程序(例如nRF Connect )時,無需手動查找 UUID 即可解碼其用途。 例如:

UUID 為00001801-0000-1000-8000-00805f9b34fb的服務將顯示為通用訪問服務。

您可以在藍牙規范的分配編號部分找到更多預定義的編號。 文檔“16 位 UUID”包含服務的短版本(16 位),包括前面提到的通用訪問服務的 0x1801。

您可以簡單地忽略所有不需要的服務(並且沒有自己創建)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM