Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using uint8 internally to get better performance #1067

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public class FlutterBluePlusPlugin implements
private final Map<String, BluetoothDevice> mBondingDevices = new ConcurrentHashMap<>();
private final Map<String, Integer> mMtu = new ConcurrentHashMap<>();
private final Map<String, BluetoothGatt> mAutoConnected = new ConcurrentHashMap<>();
private final Map<String, String> mWriteChr = new ConcurrentHashMap<>();
private final Map<String, String> mWriteDesc = new ConcurrentHashMap<>();
private final Map<String, byte[]> mWriteChr = new ConcurrentHashMap<>();
private final Map<String, byte[]> mWriteDesc = new ConcurrentHashMap<>();
private final Map<String, String> mAdvSeen = new ConcurrentHashMap<>();
private final Map<String, Integer> mScanCounts = new ConcurrentHashMap<>();
private HashMap<String, Object> mScanFilters = new HashMap<String, Object>();
Expand Down Expand Up @@ -372,7 +372,7 @@ public void onMethodCall(@NonNull MethodCall call,
break;
}

case "getAdapterName":
case "getAdapterName":
{
ArrayList<String> permissions = new ArrayList<>();

Expand Down Expand Up @@ -580,8 +580,8 @@ public void onMethodCall(@NonNull MethodCall call,
for (int i = 0; i < withMsd.size(); i++) {
HashMap<String, Object> m = (HashMap<String, Object>) withMsd.get(i);
int id = (int) m.get("manufacturer_id");
byte[] mdata = hexToBytes((String) m.get("data"));
byte[] mask = hexToBytes((String) m.get("mask"));
byte[] mdata = (byte[]) m.get("data");
byte[] mask = (byte[]) m.get("mask");
ScanFilter f = null;
if (mask.length == 0) {
f = new ScanFilter.Builder().setManufacturerData(id, mdata).build();
Expand All @@ -594,9 +594,9 @@ public void onMethodCall(@NonNull MethodCall call,
// service data
for (int i = 0; i < withServiceData.size(); i++) {
HashMap<String, Object> m = (HashMap<String, Object>) withServiceData.get(i);
ParcelUuid s = ParcelUuid.fromString(uuid128((String) m.get("service")));
byte[] mdata = hexToBytes((String) m.get("data"));
byte[] mask = hexToBytes((String) m.get("mask"));
ParcelUuid s = ParcelUuid.fromString(uuid128((String) m.get("service")));
byte[] mdata = (byte[]) m.get("data");
byte[] mask = (byte[]) m.get("mask");
ScanFilter f = null;
if (mask.length == 0) {
f = new ScanFilter.Builder().setServiceData(s, mdata).build();
Expand Down Expand Up @@ -894,7 +894,7 @@ public void onMethodCall(@NonNull MethodCall call,
String serviceUuid = (String) data.get("service_uuid");
String characteristicUuid = (String) data.get("characteristic_uuid");
String primaryServiceUuid = (String) data.get("primary_service_uuid");
String value = (String) data.get("value");
byte[] value = (byte[]) data.get("value");
int writeTypeInt = (int) data.get("write_type");
boolean allowLongWrite = ((int) data.get("allow_long_write")) != 0;

Expand Down Expand Up @@ -938,7 +938,7 @@ public void onMethodCall(@NonNull MethodCall call,

// check maximum payload
int maxLen = getMaxPayload(remoteId, writeType, allowLongWrite);
int dataLen = hexToBytes(value).length;
int dataLen = value.length;
if (dataLen > maxLen) {
String a = writeTypeInt == 0 ? "withResponse" : "withoutResponse";
String b = writeTypeInt == 0 ? (allowLongWrite ? ", allowLongWrite" : ", noLongWrite") : "";
Expand All @@ -955,7 +955,7 @@ public void onMethodCall(@NonNull MethodCall call,
// write characteristic
if (Build.VERSION.SDK_INT >= 33) { // Android 13 (August 2022)

int rv = gatt.writeCharacteristic(characteristic, hexToBytes(value), writeType);
int rv = gatt.writeCharacteristic(characteristic, value, writeType);

if (rv != BluetoothStatusCodes.SUCCESS) {
String s = "gatt.writeCharacteristic() returned " + rv + " : " + bluetoothStatusString(rv);
Expand All @@ -965,7 +965,7 @@ public void onMethodCall(@NonNull MethodCall call,

} else {
// set value
if(!characteristic.setValue(hexToBytes(value))) {
if(!characteristic.setValue(value)) {
result.error("writeCharacteristic", "characteristic.setValue() returned false", null);
break;
}
Expand Down Expand Up @@ -1040,7 +1040,7 @@ public void onMethodCall(@NonNull MethodCall call,
String characteristicUuid = (String) data.get("characteristic_uuid");
String descriptorUuid = (String) data.get("descriptor_uuid");
String primaryServiceUuid = (String) data.get("primary_service_uuid");
String value = (String) data.get("value");
byte[] value = (byte[]) data.get("value");

// check connection
BluetoothGatt gatt = mConnectedDevices.get(remoteId);
Expand Down Expand Up @@ -1071,9 +1071,9 @@ public void onMethodCall(@NonNull MethodCall call,

// check mtu
int mtu = mMtu.get(remoteId);
if ((mtu-3) < hexToBytes(value).length) {
if ((mtu-3) < value.length) {
String s = "data longer than mtu allows. dataLength: " +
hexToBytes(value).length + "> max: " + (mtu-3);
value.length + "> max: " + (mtu-3);
result.error("writeDescriptor", s, null);
break;
}
Expand All @@ -1086,7 +1086,7 @@ public void onMethodCall(@NonNull MethodCall call,
// write descriptor
if (Build.VERSION.SDK_INT >= 33) { // Android 13 (August 2022)

int rv = gatt.writeDescriptor(descriptor, hexToBytes(value));
int rv = gatt.writeDescriptor(descriptor, value);
if (rv != BluetoothStatusCodes.SUCCESS) {
String s = "gatt.writeDescriptor() returned " + rv + " : " + bluetoothStatusString(rv);
result.error("writeDescriptor", s, null);
Expand All @@ -1096,7 +1096,7 @@ public void onMethodCall(@NonNull MethodCall call,
} else {

// Set descriptor
if(!descriptor.setValue(hexToBytes(value))){
if(!descriptor.setValue(value)){
result.error("writeDescriptor", "descriptor.setValue() returned false", null);
break;
}
Expand Down Expand Up @@ -1194,7 +1194,7 @@ public void onMethodCall(@NonNull MethodCall call,
// remember the data we are writing
if (primaryServiceUuid == null) {primaryServiceUuid = "";}
String key = remoteId + ":" + serviceUuid + ":" + characteristicUuid + ":" + CCCD + ":" + primaryServiceUuid;
mWriteDesc.put(key, bytesToHex(descriptorValue));
mWriteDesc.put(key, descriptorValue);

// write descriptor
if (Build.VERSION.SDK_INT >= 33) { // Android 13 (August 2022)
Expand Down Expand Up @@ -2299,7 +2299,7 @@ public void onCharacteristicReceived(BluetoothGatt gatt, BluetoothGattCharacteri
response.put("remote_id", gatt.getDevice().getAddress());
response.put("service_uuid", uuidStr(characteristic.getService().getUuid()));
response.put("characteristic_uuid", uuidStr(characteristic.getUuid()));
response.put("value", bytesToHex(value));
response.put("value", value);
response.put("success", status == BluetoothGatt.GATT_SUCCESS ? 1 : 0);
response.put("error_code", status);
response.put("error_string", gattErrorString(status));
Expand Down Expand Up @@ -2357,7 +2357,7 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi

// what data did we write?
String key = remoteId + ":" + serviceUuid + ":" + characteristicUuid + ":" + primaryServiceUuid;
String value = mWriteChr.get(key) != null ? mWriteChr.get(key) : "";
byte[] value = mWriteChr.get(key) != null ? mWriteChr.get(key) : new byte[0];
mWriteChr.remove(key);

// see: BmCharacteristicData
Expand Down Expand Up @@ -2395,7 +2395,7 @@ public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descrip
response.put("service_uuid", uuidStr(descriptor.getCharacteristic().getService().getUuid()));
response.put("characteristic_uuid", uuidStr(descriptor.getCharacteristic().getUuid()));
response.put("descriptor_uuid", uuidStr(descriptor.getUuid()));
response.put("value", bytesToHex(value));
response.put("value", value);
response.put("success", status == BluetoothGatt.GATT_SUCCESS ? 1 : 0);
response.put("error_code", status);
response.put("error_string", gattErrorString(status));
Expand Down Expand Up @@ -2427,7 +2427,7 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri

// what data did we write?
String key = remoteId + ":" + serviceUuid + ":" + characteristicUuid + ":" + descriptorUuid + ":" + primaryServiceUuid;
String value = mWriteDesc.get(key) != null ? mWriteDesc.get(key) : "";
byte[] value = mWriteDesc.get(key) != null ? mWriteDesc.get(key) : new byte[0];
mWriteDesc.remove(key);

// see: BmDescriptorData
Expand Down Expand Up @@ -2567,20 +2567,20 @@ HashMap<String, Object> bmScanAdvertisement(BluetoothDevice device, ScanResult r
Map<ParcelUuid, byte[]> serviceData = adv != null ? adv.getServiceData() : null;

// Manufacturer Specific Data
HashMap<Integer, String> manufDataB = new HashMap<>();
HashMap<Integer, byte[]> manufDataB = new HashMap<>();
if (manufData != null) {
for (Map.Entry<Integer, byte[]> entry : manufData.entrySet()) {
manufDataB.put(entry.getKey(), bytesToHex(entry.getValue()));
manufDataB.put(entry.getKey(), entry.getValue());
}
}

// Service Data
HashMap<String, Object> serviceDataB = new HashMap<>();
HashMap<String, byte[]> serviceDataB = new HashMap<>();
if (serviceData != null) {
for (Map.Entry<ParcelUuid, byte[]> entry : serviceData.entrySet()) {
ParcelUuid key = entry.getKey();
byte[] value = entry.getValue();
serviceDataB.put(uuidStr(key.getUuid()), bytesToHex(value));
serviceDataB.put(uuidStr(key.getUuid()), value);
}
}

Expand Down Expand Up @@ -2806,21 +2806,6 @@ private boolean isAdapterOn()
}
}

private static byte[] hexToBytes(String s) {
if (s == null) {
return new byte[0];
}
int len = s.length();
byte[] data = new byte[len / 2];

for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}

return data;
}

private static String bytesToHex(byte[] bytes) {
if (bytes == null) {
return "";
Expand Down
6 changes: 3 additions & 3 deletions example/lib/screens/device_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';

import '../widgets/service_tile.dart';
import '../utils/extra.dart';
import '../utils/snackbar.dart';
import '../widgets/characteristic_tile.dart';
import '../widgets/descriptor_tile.dart';
import '../utils/snackbar.dart';
import '../utils/extra.dart';
import '../widgets/service_tile.dart';

class DeviceScreen extends StatefulWidget {
final BluetoothDevice device;
Expand Down
3 changes: 1 addition & 2 deletions example/lib/widgets/characteristic_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';

import "../utils/snackbar.dart";

import "descriptor_tile.dart";

class CharacteristicTile extends StatefulWidget {
Expand Down Expand Up @@ -134,7 +133,7 @@ class _CharacteristicTileState extends State<CharacteristicTile> {

Widget buildButtonRow(BuildContext context) {
bool read = widget.characteristic.properties.read;
bool write = widget.characteristic.properties.write;
bool write = widget.characteristic.properties.write || widget.characteristic.properties.writeWithoutResponse;
bool notify = widget.characteristic.properties.notify;
bool indicate = widget.characteristic.properties.indicate;
return Row(
Expand Down
Loading