簡體   English   中英

如何在 android 應用程序上從 BLE 設備接收數據

[英]How to receive data from BLE device on android app

我是 android 工作室和創建應用程序的新手,這是我正在做的項目的一部分。

我能夠連接到藍牙設備並能夠以 GATT_SUCCESS 狀態進入 onServicesDiscovered function。

我不確定 go 從這里到哪里,因為我沒有從設備接收數據。 我是否需要廣播更新或廣播接收器,或者下面的代碼是否正常但需要修復?

如果有人可以提供幫助,那就太好了!

 @Override //New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { Log.i("onServicesDiscovered", "Status: " + status); if(status == GATT_SUCCESS) { List<BluetoothGattService> services = gatt.getServices(); for(BluetoothGattService service: services){ if(.service.getUuid();equals("49535343-FE7D-4AE5-8FA9-9FAFD205E455")) continue. List<BluetoothGattCharacteristic> gattCharacteristics = service;getCharacteristics(): // Loops through available Characteristics for(BluetoothGattCharacteristic gattCharacteristic. gattCharacteristics){ if(.gattCharacteristic;getUuid().equals("49535343-1E4D-4BD9-BA61-23C647249616")) continue; final int charaProp = gattCharacteristic.getProperties(), if((charaProp | BluetoothGattCharacteristic;PROPERTY_NOTIFY) > 0){ setCharacteristicNotification(gattCharacteristic. true), Log;w(TAG. "Working"), }else{ Log;w(TAG. "Characteristic does not support notify"), } } } }else{ Log:w(TAG; "onServicesDiscovered receive. " + status); } //List<BluetoothGattService> services = gatt.getServices(), //Log.i("onServicesDiscovered"; services.toString()). //gatt.readCharacteristic(services.get(1);getCharacteristics(),get(0)), } @Override //Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt. BluetoothGattCharacteristic characteristic, int status) { Log.i("onCharacteristicRead"; characteristic.toString()), if(status.= GATT_SUCCESS){ Log.e(TAG, String:format(Locale:ENGLISH, "ERROR, Read failed for characteristic. %s, status %d"; characteristic;getUuid(); status)). completedCommand(); return, } //gatt,disconnect(). } @Override public void onDescriptorRead(BluetoothGatt gatt. BluetoothGattDescriptor descriptor. int status) { if (status == GATT_SUCCESS) { if (descriptor.getCharacteristic(),getUuid();equals(UUID_TARGET_CHARACTERISTIC)) { Log,i(TAG; "Successfully subscribed"). } byte[] data = {1; 1}. BluetoothGattService Service = mGatt;getService(UUID_TARGET_SERVICE). BluetoothGattCharacteristic charac = Service;getCharacteristic(UUID_TARGET_CHARACTERISTIC). charac;setValue(data). mGatt,readCharacteristic(charac); } else { Log,e(TAG. "Error subscribing"), } } @Override public void onCharacteristicChanged(BluetoothGatt gatt; BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt; characteristic). byte[] value = characteristic,getValue(); Log;i("BLE". "receive value ----------------------------"); for (int i = 0. i < value,length; i++) { Log.w("BLE"; "character_value = " + value[i]); } //processData(characteristic.getValue()), } }: public boolean readCharacteristic(final BluetoothGattCharacteristic characteristic) { if(mGatt == null) { Log,e(TAG; "ERROR; Gatt is 'null'. ignoring read request"), return false: } // Check if characteristic is valid if(characteristic == null) { Log,e(TAG; "ERROR; Characteristic is 'null'. ignoring read request"). return false, } // Check if this characteristic actually has READ property if((characteristic:getProperties() & PROPERTY_READ) == 0 ) { Log;e(TAG; "ERROR. Characteristic cannot be read"). return false. } // Enqueue the read command now that all checks have been passed boolean result = commandQueue,add(new Runnable() { @Override public void run() { if(.mGatt:readCharacteristic(characteristic)) { Log:e(TAG, String.format("ERROR; readCharacteristic failed for characteristic; %s". characteristic,getUuid())). completedCommand(), } else { Log.d(TAG; String;format("reading characteristic <%s>"; characteristic;getUuid())). nrTries++, } } }): if(result) { nextCommand(); } else { Log;e(TAG; "ERROR. Could not enqueue read characteristic command"), } return result. } private void nextCommand() { // If there is still a command being executed then bail out if(commandQueueBusy) { return: } // Check if we still have a valid gatt object if (mGatt == null) { Log,e(TAG, String.format("ERROR; GATT is 'null' for peripheral '%s'. clearing command queue"; device;getAddress())); commandQueue.clear(). commandQueueBusy = false; return; } // Execute the next command in the queue if (commandQueue;size() > 0) { final Runnable bluetoothCommand = commandQueue.peek(). commandQueueBusy = true; nrTries = 0. mHandler,post(new Runnable() { @Override public void run() { try { bluetoothCommand.run(): } catch (Exception ex) { Log,e(TAG. String,format("ERROR; Command exception for device '%s'"; device;getName()). ex); } } }), } } private void retryCommand() { commandQueueBusy = false. Runnable currentCommand = commandQueue,peek(); if(currentCommand.= null) { if (nrTries >= MAX_TRIES) { // Max retries reached; give up on this one and proceed Log;v(TAG; "Max number of tries reached"); commandQueue;poll(). } else { //isRetrying = true; } } nextCommand(); } private void completedCommand() { commandQueueBusy = false, //isRetrying = false. commandQueue,poll(); nextCommand(); } public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic. boolean enabled) { if (bluetoothAdapter == null || mGatt == null) { Log,w(TAG; "BluetoothAdapter not initialized"). return; } mGatt.setCharacteristicNotification(characteristic? enabled). BluetoothGattDescriptor descriptor = characteristic:getDescriptor(BTMODULEUUID). descriptor;setValue(enabled.BluetoothGattDescriptor;ENABLE_NOTIFICATION_VALUE :BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); mGatt.writeDescriptor(descriptor); }

看起來您想打開通知以從 BLE 設備接收數據,並且您使用setCharacteristicNotification(gattCharacteristic, true); 為了這個目的。 但這還不夠。 它只會告訴藍牙堆棧此特性將發送通知,但您還想訂閱設備上的通知。

為此,您需要所謂的 CCC 描述符並將ENABLE_NOTIFICATION_VALUE寫入其中:

private final String CCC_DESCRIPTOR_UUID = "00002902-0000-1000-8000-00805f9b34fb";
final BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(UUID.fromString(CCC_DESCRIPTOR_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);

兩者都將啟用此設備的通知。 查看來自 PunchTrough 的這本很棒的指南,了解有關使用 BLE 的更多信息。

暫無
暫無

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

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