I've got a Bluetooth server that uses bleno and returns a list of available Wifi networks to the client. The code for readCharacteristic
looks basically like this:
class ReadCharacteristic extends bleno.Characteristic {
constructor(uuid, name, action) {
super({
uuid: uuid,
properties: ["read"],
value: null,
descriptors: [
new bleno.Descriptor({
uuid: "2901",
value: name
})
]
});
this.actionFunction = action;
}
onReadRequest(offset, callback) {
console.log("Offset: " + offset);
if(offset === 0) {
const result = this.actionFunction();
result.then(value => {
this.actionFunctionResult = value;
const data = new Buffer.from(value).slice(0,bleno.mtu);
console.log("onReadRequest: " + data.toString('utf-8'));
callback(this.RESULT_SUCCESS, data);
}, err => {
console.log("onReadRequest error: " + err);
callback(this.RESULT_UNLIKELY_ERROR);
}).catch( err => {
console.log("onReadRequest error: " + err);
callback(this.RESULT_UNLIKELY_ERROR);
});
}
else {
let data = new Buffer.from(this.actionFunctionResult);
if(offset > data.length) {
callback(this.RESULT_INVALID_OFFSET, null);
}
data = data.slice(offset+1, offset+bleno.mtu);
console.log(data.toString('utf-8'));
callback(this.RESULT_SUCCESS, data);
}
}
}
(I've tried data = data.slice(offset+1, offset+bleno.mtu);
and like this data = data.slice(offset+1);
)
The client is an Android app that reads this Characteristic.
The Android part for reading looks like this:
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.requestMtu(256);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disconnected from GATT server.");
mFancyShowCaseView.show();
gatt.close();
scanForBluetoothDevices();
}
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
if (status != BluetoothGatt.GATT_SUCCESS) {
Log.e(TAG, "Can't set mtu to: " + mtu);
} else {
Log.i(TAG, "Connected to GATT server. MTU: " + mtu);
Log.i(TAG, "Attempting to start service discovery:" +
mWifiProvisioningService.discoverServices());
}
}
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "ACTION_GATT_SERVICES_DISCOVERED");
BluetoothGattService wifiProvisioningService = gatt.getService(WIFI_PROVISIONING_SERVICE_UUID);
BluetoothGattCharacteristic currentConnectedWifiCharacteristic = wifiProvisioningService.getCharacteristic(WIFI_ID_UUID);
BluetoothGattCharacteristic availableWifiCharacteristic = wifiProvisioningService.getCharacteristic(WIFI_SCAN_UUID);
// Only read the first characteristic and add the 2nd one to a list as we have to wait
// for the read return before we read the 2nd one.
if (!gatt.readCharacteristic(currentConnectedWifiCharacteristic)) {
Log.e(TAG, "Error while reading current connected wifi name.");
}
readCharacteristics.add(availableWifiCharacteristic);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
// Result of a characteristic read operation
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
UUID characteristicUUID = characteristic.getUuid();
if (WIFI_ID_UUID.equals(characteristicUUID)) {
Log.d(TAG, "HEUREKA we found the current wifi name: " + new String(characteristic.getValue()));
final String currentWifiName = new String(characteristic.getValue());
runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView) findViewById(R.id.currentWifiTxt)).setText(currentWifiName);
findViewById(R.id.currentWifiTxtProgress).setVisibility(View.GONE);
}
});
} else if (WIFI_SCAN_UUID.equals(characteristicUUID)) {
Log.d(TAG, "HEUREKA we found the wifi list: " + new String(characteristic.getValue()));
List<String> wifiListArrayList = new ArrayList<>();
try {
JSONObject wifiListRoot = new JSONObject(characteristic.getStringValue(0));
JSONArray wifiListJson = wifiListRoot.getJSONArray("list");
for (int i = 0; i < wifiListJson.length(); i++) {
wifiListArrayList.add(wifiListJson.get(i).toString());
}
} catch (JSONException e) {
Log.e(TAG, e.toString());
return;
}
final String[] wifiList = new String[wifiListArrayList.size()];
wifiListArrayList.toArray(wifiList);
runOnUiThread(new Runnable() {
@Override
public void run() {
((ListView) findViewById(R.id.availableWifiList)).setAdapter(new ArrayAdapter<String>(mContext, R.layout.wifi_name_list_item, wifiList));
findViewById(R.id.currentWifiTxtProgress).setVisibility(View.GONE);
}
});
} else {
Log.i(TAG, "Unexpected Gatt vale: " + new String(characteristic.getValue()));
}
if (readCharacteristics.size() > 0) {
BluetoothGattCharacteristic readCharacteristic = readCharacteristics.get(0);
if (!gatt.readCharacteristic(readCharacteristic)) {
Log.e(TAG, "Error while writing descriptor for connected wifi");
}
readCharacteristics.remove(readCharacteristic);
}
}
}
The MTU is adjusted to 256 bytes. Which I reflected on the server when reading the list. The call itself works fine and returns the list but if the list contains more then 600 bytes only 600 bytes are available on Android. I'm somehow certain that the JS server sends all the data but for some reason the Android client only receives or caches 600 bytes which does not seem correct.
I've found this post: Android BLE - Peripheral | onCharacteristicRead return wrong value or part of it (but repeated)
and this: Android BLE - How is large characteristic value read in chunks (using an offset)?
But both didn't solve my issue. I'm aware that I need to wait for one read to return before I start the next read and that I need to wait till MTU is written before I continue to read data. To the best of my knowledge this is reflected in the source you see above. I'm kind of lost here.
Any idea is highly apprechiated.
Thanks a lot