Hi in the below code I am using override method to change MTU value able to see log that is printing .After increasing MTU value and then calling sendcommand() still My device is not changing anything via bluetooth.
Can any one help me where I did the issue
Log
`New MTU:256 mtu ,status : 1`
Method:
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
gatt.requestMtu(mtu);
Log.i(TAG, "New MTU: " + mtu + " , Status: " + status + " , Succeed: " + (status == gatt.GATT_SUCCESS));
}
After increasing MTU value to 256 I am calling another method:
public void sendCommands(BluetoothGatt gatt) throws JSONException {
if (bluetoothGatt != null) {
List<BluetoothGattService> gattServices = bluetoothGatt.getServices();
if (2 == BluetoothProfile.STATE_CONNECTED) // BLE Is connected after provision as ghatt is connected.
{
gatt.requestMtu(256);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
for (int i = 0; i < gattServices.size(); i++) {
String serviceUUID = gattServices.get(i).getUuid().toString();
if (serviceUUID.equalsIgnoreCase(SERVICE_GUID)) {
List<BluetoothGattCharacteristic> gattCharacteristics = gattServices.get(i).getCharacteristics();
for (BluetoothGattCharacteristic gattCharacteristic :
gattCharacteristics) {
final String uuid = gattCharacteristic.getUuid().toString();
if (uuid.equalsIgnoreCase(TX_CHAR_GUID)) {
String jsonString = "{\"i\":1,\"v\":1,\"t\":8,\"s\":\"dbri\",\"n\":50}";
byte[] payload = jsonString.getBytes(Charsets.UTF_8);
gattCharacteristic.setValue(payload);
/*System.out.println("Length:"+payload.length);
try {
JSONObject obj = new JSONObject(jsonString);
String byte_one=obj.getString("i");
System.out.println("byte_one:"+byte_one);
byte[] payload1 = byte_one.getBytes();
} catch (Throwable t) {
// Log.e("My App", "Could not parse malformed JSON: \"" + obj + "\"");
}*/
gatt.writeCharacteristic(gattCharacteristic);
}
}
}
}
}
}, 10000);
}
}
}