如何在android系统在BLE发送超过20个字节的数据? [英] How to send more than 20 bytes data over ble in android?

查看:1006
本文介绍了如何在android系统在BLE发送超过20个字节的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送使用简单的循环超过33个字节,是任何人有想法如何通过Android的BLE发送超过20个字节的数据。

 如果(!mConnected)回报;
        的for(int i = 0; I< str.length;我++){
            如果(STR [I] ==海峡[str.length -1]){
                 VAL = STR [I] +\\ n;
            }其他{
                VAL = STR [I] +_;
            }
            的System.out.println(VAL);
            mBluetoothLeService.WriteValue(VAL);
        }


解决方案

通过BLE发送超过20个字节是很容易实现你的数据分成20字节的数据包,并实现在短暂的延迟(即使用睡眠())发送每个数据包之间。

下面是从项目我工作的那个code的一小段发生在字节[] 的形式数据,并将其分成数组同样,(字节[] [] ),在20个字节的块,然后将其发送到由一个传输每个分组彼此方法

  INT块大小= 20;
    字节[] [] =包新的字节[packetsToSend] [块大小]
    INT packetsToSend =(int)的Math.ceil(BYTECOUNT /块大小);    的for(int i = 0; I< packets.length;我++){
        包[I] = Arrays.copyOfRange(源,启动,启动+块大小);
        启动+ =块大小;
    }    sendSplitPackets(数据包);

下面是如何做到这一点其他两个很好的解释:

<一个href=\"http://stackoverflow.com/questions/24135682/android-sending-data-20-bytes-by-ble\">(Stackoverflow) Android的:由BLE 发送数据> 20个字节

(北欧半)处理大数据包通过BLE

I am trying to send more than 33 bytes using simple loops, Is anybody has idea how to send more than 20 bytes data over android ble.

if(!mConnected) return;
        for (int i = 0; i<str.length;i++) {
            if(str[i] == str[str.length -1]){
                 val = str[i]+"\n";
            }else {
                val = str[i] + "_";
            }
            System.out.println(val);
            mBluetoothLeService.WriteValue(val);
        }

解决方案

Sending more than 20 bytes via BLE is easily achievable by splitting your data into 20 byte packets and implementing a short delay (i.e. using sleep()) between sending each packet.

Here's a short snippet of code from a project I'm working on that takes data in the form of byte[] and splits it into an array of the same, ( byte[][] ), in 20 byte chunks, and then sends it to another method that transmits each packet one by one.

    int chunksize = 20;
    byte[][] packets = new byte[packetsToSend][chunksize]; 
    int packetsToSend = (int) Math.ceil( byteCount / chunksize);

    for(int i = 0; i < packets.length; i++) {
        packets[i] = Arrays.copyOfRange(source,start, start + chunksize);
        start += chunksize;
    }

    sendSplitPackets(packets);

Here are two other very good explanations of how to achieve this:

(Stackoverflow) Android: Sending data >20 bytes by BLE

(Nordic Semi) Dealing Large Data Packets Through BLE

这篇关于如何在android系统在BLE发送超过20个字节的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆