上篇 Android BLE ESP32的onCharacteristicChanged沒有回撥及getDescriptor null
接著出現下面問題
在使用Android BLE發送到 ESP32 超過20個字元的數據時,數據會自動被切斷到20字元處
那要怎麼解決呢?那就分段處理就好了
數據拆分為20字節數據包並在發送每個數據包之間短延遲(即使用sleep(200)),就可以輕鬆實現通過BLE發送超過20個字節。 先用一維byte[]的形式獲取數據,並以20字節的塊的形式將其拆分為相同的數組變成二維(byte[][]),然後在到發送數據時,把二維數據拆分為一維數據。 以下是網路上找到的代碼 - public void sendData(byte [] data){
- int chunksize = 20; //20 byte chunk
- packetSize = (int) Math.ceil( data.length / (double)chunksize); //make this variable public so we can access it on the other function
- //this is use as header, so peripheral device know ho much packet will be received.
- characteristicData.setValue(packetSize.toString().getBytes());
- mGatt.writeCharacteristic(characteristicData);
- mGatt.executeReliableWrite();
- packets = new byte[packetSize][chunksize];
- packetInteration =0;
- Integer start = 0;
- for(int i = 0; i < packets.length; i++) {
- int end = start+chunksize;
- if(end>data.length){end = data.length;}
- packets[i] = Arrays.copyOfRange(data,start, end);
- start += chunksize;
- }
- }
複製代碼
MainActivity.java - //送訊號到藍芽
- sendData.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- //String data = TestText.getText().toString();
- String data = "aaaaabbbbbcccccdddddeeeeefffffggggghhhhhiiiiijjjjjkkkkk";
- int chunksize = 20;
- packetSize = (int) Math.ceil( data.length() / (double) chunksize);
- Log.e(TAG, "packetSize: "+packetSize);
- byte[][] packets = new byte[packetSize][chunksize];
- Integer start = 0;
- for (int i = 0; i < packets.length; i++) {
- int end = start + chunksize;
- if (end > data.length()) {
- end = data.length();
- }
- packets[i] = Arrays.copyOfRange(data.getBytes(), start, end);
- start += chunksize;
- }
- mBluetoothLeService.send(packets);
- }
- });
複製代碼
BluetoothLeService.java- public boolean send(byte[][] packets) {
- List<BluetoothGattService> gattServices = getSupportedGattServices();
- String uuid = null;
- if (mBluetoothGatt == null || gattServices == null) {
- Log.w(TAG, "BluetoothGatt not initialized");
- return false;
- }
- for (BluetoothGattService gattService : gattServices) {
- uuid = gattService.getUuid().toString();
- if (GattAttributes.device.get("target_service").equals(uuid)) {
- Log.i(TAG, "send, uuid: " + uuid);
- // Loops through available Characteristics.
- for (BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
- uuid = gattCharacteristic.getUuid().toString();
- Log.e(TAG,"uuid:"+uuid);
- if (GattAttributes.device.get(GattAttributes.DEVICES_TX).equals(uuid)) {
- gattCharacteristic = gattService.getCharacteristic(UUID.fromString(GattAttributes.UUID_DEVICES_TX));
- if (gattCharacteristic == null) {
- Log.w(TAG, "Send characteristic not found");
- return false;
- }
- byte[] data;
- Log.w(TAG, "packets.length:" + packets.length);
- for (int i = 0; i < packets.length; i++) {
- data = packets[i];
- Log.w(TAG, "data:" + data.toString());
- gattCharacteristic.setValue(data);
- gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
- try {
- sleep(200);
- } catch (InterruptedException e) {
- }
- mBluetoothGatt.writeCharacteristic(gattCharacteristic);
- }
- }
- }
- }
- }
- return false;
- }
複製代碼
再來就會在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
|