ble 20字节 android,Android:通过BLE发送大于20个字节的数据

小编典典

BLE最多允许你传输20个字节。

如果要发送20个以上的字节,则应定义数组byte[]以包含所需的数据包数量。

如果你要发送少于160个字符(160个字节),以下示例可以很好地工作。

p / s:根据需要编辑以下内容。不要完全跟随我。

实际上,当我们使用BLE时,移动端和固件端需要设置密钥(例如0x03...)来定义双方之间的连接门。

这个想法是:

当我们继续传输数据包时,不是最后一个。门是byte[1] = 0x01。

如果我们发送最后一个,则门为byte[1] = 0x00。

数据构造(20字节):

1– Byte 1定义Gate ID:例如。消息门ID byte[0] = 0x03。

2- Byte 2定义recognization:是最后一个数据包0x00还是继续发送数据包0x01。

3- Byte 3(在消除Byte 1&之后,应为18个字节Byte 2)-在此处附加消息内容。

在阅读下面的代码之前,请先了解我的逻辑。

下面是发送带有许多数据包的消息的示例,每个数据包是一个大小为20字节的数组。

private void sendMessage(BluetoothGattCharacteristic characteristic, String CHARACTERS){

byte[] initial_packet = new byte[3];

/**

* Indicate byte

*/

initial_packet[0] = BLE.INITIAL_MESSAGE_PACKET;

if (Long.valueOf(

String.valueOf(CHARACTERS.length() + initial_packet.length))

> BLE.DEFAULT_BYTES_VIA_BLE) {

sendingContinuePacket(characteristic, initial_packet, CHARACTERS);

} else {

sendingLastPacket(characteristic, initial_packet, CHARACTERS);

}

}

private void sendingContinuePacket(BluetoothGattCharacteristic characteristic,

byte[] initial_packet, String CHARACTERS){

/**

* TODO If data length > Default data can sent via BLE : 20 bytes

*/

// Check the data length is large how many times with Default Data (BLE)

int times = Byte.valueOf(String.valueOf(

CHARACTERS.length() / BLE.DEFAULT_BYTES_IN_CONTINUE_PACKET));

Log.i(TAG, "CHARACTERS.length() " + CHARACTERS.length());

Log.i(TAG, "times " + times);

// TODO

// 100 : Success

// 101 : Error

byte[] sending_continue_hex = new byte[BLE.DEFAULT_BYTES_IN_CONTINUE_PACKET];

for (int time = 0; time <= times; time++) {

/**

* Wait second before sending continue packet

*/

try {

Thread.sleep(200);

} catch (InterruptedException e) {

e.printStackTrace();

}

if (time == times) {

Log.i(TAG, "LAST PACKET ");

/**

* If you do not have enough characters to send continue packet,

* This is the last packet that will be sent to the band

*/

/**

* Packet length byte :

*/

/**

* Length of last packet

*/

int character_length = CHARACTERS.length()

- BLE.DEFAULT_BYTES_IN_CONTINUE_PACKET*times;

initial_packet[1] = Byte.valueOf(String.valueOf(character_length

+ BLE.INITIAL_MESSAGE_PACKET_LENGTH));

initial_packet[2] = BLE.SENDING_LAST_PACKET;

Log.i(TAG, "character_length " + character_length);

/**

* Message

*/

// Hex file

byte[] sending_last_hex = new byte[character_length];

// Hex file : Get next bytes

for (int i = 0; i < sending_last_hex.length; i++) {

sending_last_hex[i] =

CHARACTERS.getBytes()[sending_continue_hex.length*time + i];

}

// Merge byte[]

byte[] last_packet =

new byte[character_length + BLE.INITIAL_MESSAGE_PACKET_LENGTH];

System.arraycopy(initial_packet, 0, last_packet,

0, initial_packet.length);

System.arraycopy(sending_last_hex, 0, last_packet,

initial_packet.length, sending_last_hex.length);

// Set value for characteristic

characteristic.setValue(last_packet);

} else {

Log.i(TAG, "CONTINUE PACKET ");

/**

* If you have enough characters to send continue packet,

* This is the continue packet that will be sent to the band

*/

/**

* Packet length byte

*/

int character_length = sending_continue_hex.length;

/**

* TODO Default Length : 20 Bytes

*/

initial_packet[1] = Byte.valueOf(String.valueOf(

character_length + BLE.INITIAL_MESSAGE_PACKET_LENGTH));

/**

* If sent data length > 20 bytes (Default : BLE allow send 20 bytes one time)

* -> set 01 : continue sending next packet

* else or if after sent until data length < 20 bytes

* -> set 00 : last packet

*/

initial_packet[2] = BLE.SENDING_CONTINUE_PACKET;

/**

* Message

*/

// Hex file : Get first 17 bytes

for (int i = 0; i < sending_continue_hex.length; i++) {

Log.i(TAG, "Send stt : "

+ (sending_continue_hex.length*time + i));

// Get next bytes

sending_continue_hex[i] =

CHARACTERS.getBytes()[sending_continue_hex.length*time + i];

}

// Merge byte[]

byte[] sending_continue_packet =

new byte[character_length + BLE.INITIAL_MESSAGE_PACKET_LENGTH];

System.arraycopy(initial_packet, 0, sending_continue_packet,

0, initial_packet.length);

System.arraycopy(sending_continue_hex, 0, sending_continue_packet,

initial_packet.length, sending_continue_hex.length);

// Set value for characteristic

characteristic.setValue(sending_continue_packet);

}

// Write characteristic via BLE

mBluetoothGatt.writeCharacteristic(characteristic);

}

}

public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic,

String data) {

if (mBluetoothAdapter == null || mBluetoothGatt == null) {

Log.w(TAG, "BluetoothAdapter not initialized");

return false;

}

if (ActivityBLEController.IS_FIRST_TIME) {

/**

* In the first time,

* should send the Title

*/

byte[] merge_title = sendTitle(data);

// Set value for characteristic

characteristic.setValue(merge_title);

// Write characteristic via BLE

mBluetoothGatt.writeCharacteristic(characteristic);

// Reset

ActivityBLEController.IS_FIRST_TIME = false;

return true;

} else {

/**

* In the second time,

* should send the Message

*/

if (data.length() <= BLE.LIMIT_CHARACTERS) {

sendMessage(characteristic, data);

// Reset

ActivityBLEController.IS_FIRST_TIME = true;

return true;

} else {

// Typed character

typed_character = data.length();

return false;

}

}

}

2020-03-24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值