TShopping

 找回密碼
 註冊
搜索
查看: 1833|回復: 0

[教學] 如何在Android BLE 發送到 ESP32 超過20個字元的數據?

[複製鏈接]
發表於 2020-4-28 16:38:56 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
上篇 Android BLE ESP32的onCharacteristicChanged沒有回撥及getDescriptor null
接著出現下面問題

在使用Android BLE發送到 ESP32 超過20個字元的數據時,數據會自動被切斷到20字元處
那要怎麼解決呢?那就分段處理就好了

數據拆分為20字節數據包並在發送每個數據包之間短延遲(即使用sleep(200)),就可以輕鬆實現通過BLE發送超過20個字節。
先用一維byte[]的形式獲取數據,並以20字節的塊的形式將其拆分為相同的數組變成二維(byte[][]),然後在到發送數據時,把二維數據拆分為一維數據。
以下是網路上找到的代碼
  1. public void sendData(byte [] data){
  2.     int chunksize = 20; //20 byte chunk
  3.     packetSize = (int) Math.ceil( data.length / (double)chunksize); //make this variable public so we can access it on the other function

  4.     //this is use as header, so peripheral device know ho much packet will be received.
  5.     characteristicData.setValue(packetSize.toString().getBytes());
  6.     mGatt.writeCharacteristic(characteristicData);
  7.     mGatt.executeReliableWrite();

  8.     packets = new byte[packetSize][chunksize];
  9.     packetInteration =0;
  10.     Integer start = 0;
  11.     for(int i = 0; i < packets.length; i++) {
  12.         int end = start+chunksize;
  13.         if(end>data.length){end = data.length;}
  14.         packets[i] = Arrays.copyOfRange(data,start, end);
  15.         start += chunksize;
  16.     }
  17. }
複製代碼

MainActivity.java
  1. //送訊號到藍芽
  2.         sendData.setOnClickListener(new View.OnClickListener(){
  3.             @Override
  4.             public void onClick(View v) {
  5. //String data = TestText.getText().toString();
  6. String data = "aaaaabbbbbcccccdddddeeeeefffffggggghhhhhiiiiijjjjjkkkkk";
  7.                 int chunksize = 20;
  8.                 packetSize = (int) Math.ceil( data.length() / (double) chunksize);
  9.                 Log.e(TAG, "packetSize: "+packetSize);
  10.                 byte[][] packets = new byte[packetSize][chunksize];

  11.                 Integer start = 0;
  12.                 for (int i = 0; i < packets.length; i++) {
  13.                     int end = start + chunksize;
  14.                     if (end > data.length()) {
  15.                         end = data.length();
  16.                     }
  17.                     packets[i] = Arrays.copyOfRange(data.getBytes(), start, end);
  18.                     start += chunksize;
  19.                 }
  20.                 mBluetoothLeService.send(packets);
  21.             }
  22.         });
複製代碼

BluetoothLeService.java
  1. public boolean send(byte[][] packets) {
  2.         List<BluetoothGattService> gattServices = getSupportedGattServices();
  3.         String uuid = null;

  4.         if (mBluetoothGatt == null || gattServices == null) {
  5.             Log.w(TAG, "BluetoothGatt not initialized");
  6.             return false;
  7.         }
  8.         for (BluetoothGattService gattService : gattServices) {
  9.             uuid = gattService.getUuid().toString();
  10.             if (GattAttributes.device.get("target_service").equals(uuid)) {
  11.                 Log.i(TAG, "send, uuid: " + uuid);
  12.                 // Loops through available Characteristics.
  13.                 for (BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
  14.                     uuid = gattCharacteristic.getUuid().toString();
  15.                     Log.e(TAG,"uuid:"+uuid);
  16.                     if (GattAttributes.device.get(GattAttributes.DEVICES_TX).equals(uuid)) {
  17.                         gattCharacteristic = gattService.getCharacteristic(UUID.fromString(GattAttributes.UUID_DEVICES_TX));

  18.                         if (gattCharacteristic == null) {
  19.                             Log.w(TAG, "Send characteristic not found");
  20.                             return false;
  21.                         }
  22.                         byte[] data;
  23.                         Log.w(TAG, "packets.length:" + packets.length);
  24.                         for (int i = 0; i < packets.length; i++) {

  25.                             data = packets[i];
  26.                             Log.w(TAG, "data:" + data.toString());
  27.                             gattCharacteristic.setValue(data);
  28.                             gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
  29.                             try {
  30.                                 sleep(200);
  31.                             } catch (InterruptedException e) {
  32.                             }

  33.                             mBluetoothGatt.writeCharacteristic(gattCharacteristic);
  34.                         }
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.         return false;
  40.     }
複製代碼

再來就會在Arduino視窗下看到

Arduino

Arduino


影片展示



參考文章https://stackoom.com/question/22QEM/%E5%A6%82%E4%BD%95%E5%9C%A8android%E4%B8%AD%E5%8F%91%E9%80%81%E8%B6%85%E8%BF%87-%E4%B8%AA%E5%AD%97%E8%8A%82%E7%9A%84%E6%95%B0%E6%8D%AE

來源
http://www.netyea.com







 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-18 12:16 , Processed in 0.067475 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表