Java 使用(BluetoothGattCharacteristic)characteristic.getValue()中的数据创建、写入和保存csv文件
我是Andriod dev的新手,我正在尝试构建一个应用程序,该应用程序可以连接到一个Ble设备,该设备可用于创建CSV文件并存储具有notify属性的任何特征的值。我已经实现了onCharacteristicChanged(..),并且能够在订阅该特性后接收原始字节 但是,我不知道如何创建CSV文件并写入这些原始字节,直到我取消订阅该特性或断开与设备的连接。请帮忙 编辑: 我正在尝试使用CSVWriter编写它,每次收到onCharacteristicChanged回调时,我都会调用以下方法:Java 使用(BluetoothGattCharacteristic)characteristic.getValue()中的数据创建、写入和保存csv文件,java,android,android-studio,bluetooth,bluetooth-lowenergy,Java,Android,Android Studio,Bluetooth,Bluetooth Lowenergy,我是Andriod dev的新手,我正在尝试构建一个应用程序,该应用程序可以连接到一个Ble设备,该设备可用于创建CSV文件并存储具有notify属性的任何特征的值。我已经实现了onCharacteristicChanged(..),并且能够在订阅该特性后接收原始字节 但是,我不知道如何创建CSV文件并写入这些原始字节,直到我取消订阅该特性或断开与设备的连接。请帮忙 编辑: 我正在尝试使用CSVWriter编写它,每次收到onCharacteristicChanged回调时,我都会调用以下方法:
private void broadcastUpdate(final BluetoothGattCharacteristic characteristic, byte[] value){
if(isCharacteristicNotifiable(characteristic)){
String value_str = bytesToHex(value);
if(characteristic.getUuid().toString() == TX_CHARACTERISTIC){
value_str = bytestoformat(characteristic.getValue());
}
String[] line = value_str.split(" ");
Log.i("broadcastUpdate", value_str);
try {
CSVWriter writer = new CSVWriter(new FileWriter(csv, true));
writer.writeNext(line);
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
每次特性更改得到的字节数为2字节或4字节。因此,CSV文件应该在一行中打印4个十六进制值,当只有2个字节的数据时,将2个值留空
但是,如果第一次通知提供了2字节的数据,那么我的整个文件对于每次更改只显示2字节的数据(即使在第一次通知之后收到了4字节的数据)。如果第一个通知有4个字节的数据,那么我得到了我想要的。可以像其他任何文件一样创建CSV文件。首先打开要写入的文件:
String header = "";
File file = new File("/storage/emulated/0/CharNotifyData.csv");
if (!file.exists()) {
file.createNewFile();
header = "Characteristic;Value\n";
}
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(header);
这样写下你的价值观:
bw.write(characteristicName + ";" + value + "\n");
private void broadcastUpdate(final BluetoothGattCharacteristic characteristic, byte[] value){
if(isCharacteristicNotifiable(characteristic)){
String value_str = bytesToHex(value);
if(characteristic.getUuid().toString() == TX_CHARACTERISTIC){
value_str = bytestoformat(characteristic.getValue());
}
String[] line = value_str.split(" ");
String[] fullLine = new String[4];
for(int i = 0; i < 4; ++i){
if(i < line.length){
fullLine[i] = line[i];
}else{
fullLine[i] = "";
}
}
Log.i("broadcastUpdate", value_str);
try {
CSVWriter writer = new CSVWriter(new FileWriter(csv, true));
writer.writeNext(line);
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
不要忘记使用以下选项关闭文件:
bw.close();
编辑:
尝试区分这两种可能的状态。有一种方式可能是这样的:
bw.write(characteristicName + ";" + value + "\n");
private void broadcastUpdate(final BluetoothGattCharacteristic characteristic, byte[] value){
if(isCharacteristicNotifiable(characteristic)){
String value_str = bytesToHex(value);
if(characteristic.getUuid().toString() == TX_CHARACTERISTIC){
value_str = bytestoformat(characteristic.getValue());
}
String[] line = value_str.split(" ");
String[] fullLine = new String[4];
for(int i = 0; i < 4; ++i){
if(i < line.length){
fullLine[i] = line[i];
}else{
fullLine[i] = "";
}
}
Log.i("broadcastUpdate", value_str);
try {
CSVWriter writer = new CSVWriter(new FileWriter(csv, true));
writer.writeNext(line);
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
私有无效广播更新(最终BluetoothGattCharacteristic特征,字节[]值){
if(特征应报告(特征)){
字符串值\u str=bytesToHex(值);
if(characteristic.getUuid().toString()==TX\u characteristic){
value_str=bytestoformat(characteristic.getValue());
}
String[]line=value_str.split(“”);
String[]fullLine=新字符串[4];
对于(int i=0;i<4;++i){
如果(i<线路长度){
全行[i]=行[i];
}否则{
全文[i]=“”;
}
}
Log.i(“广播更新”,值_str);
试一试{
CSVWriter writer=新CSVWriter(新文件编写器(csv,true));
writer.writeNext(行);
writer.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
@Prasenjit也可以看到我的!