get array value from bluetooth low energy fff2 characteristic on Java - android

I need to get value from fff2 characteristic.
Its characteristic only return 01 or 00
How to get its value?
this is example value that I want.
public boolean readCharacteristic(){
//check mBluetoothGatt is available
if (bluetoothGatt == null) {
Log.e(TAG, "lost connection");
return false;
}
BluetoothGattService Service = bluetoothGatt.getService(UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"));
if (Service == null) {
Log.e(TAG, "service not found!");
return false;
}
BluetoothGattCharacteristic charac = Service
.getCharacteristic(UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb"));
if (charac == null) {
Log.e(TAG, "char not found!");
return false;
}
final byte[] data = charac.getValue(); // return null
String s2 = new String(data);
Log.i("READ", s2);
boolean status = true;
return status;
}
thanks..

public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
byte[] data = characteristic.getValue();
}
}
Convert the byte array to Hex format if required.
Hope this is helpful.

Related

onCharacteristicWriteRequest the #Override is red underlined

I work on a Android Things Project with bluetooth and the #Override is red underlined.
#Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
Log.d(TAG, "onCharacteristicwriterequest UUID: " + characteristic.getUuid().toString());
mGattServer.notifyCharacteristicChanged(mBluetoothDevice, characteristic, true);
}
Are you expecting the BluetoothGattCallback method?. If yes, there is no such callback method. This is the callback method available in BluetoothGattCallback
#Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { //A request to Write has completed
if (status == BluetoothGatt.GATT_SUCCESS) { //See if the write was successful
AppLog.logError(TAG, "**ACTION_DATA_WRITTEN**" + characteristic);
broadcastUpdate(BLEConstants.ACTION_DATA_WRITTEN, characteristic); //Go broadcast an intent to say we have have written data
}
}
if you want to write something to the BLE you can use the below method
public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
try {
if (mBluetoothAdapter == null || mBluetoothGatt == null) { //Check that we have access to a Bluetooth radio
return;
}
int test = characteristic.getProperties(); //Get the properties of the characteristic
if ((test & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0 && (test & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) { //Check that the property is writable
return;
}
if (mBluetoothGatt.writeCharacteristic(characteristic)) { //Request the BluetoothGatt to do the Write
AppLog.logInfo(TAG, "****************WRITE CHARACTERISTIC SUCCESSFUL**" + (characteristic.getValue()[0] & 0xFF));//The request was accepted, this does not mean the write completed
/* if(characteristic.getUuid().toString().equalsIgnoreCase(getString(R.string.char_uuid_missed_connection))){
}*/
} else {
AppLog.logInfo(TAG, "writeCharacteristic failed "+ (characteristic.getValue()[0] & 0xFF)); //Write request was not accepted by the BluetoothGatt
}
} catch (Exception e) {
AppLog.logInfo(TAG, e.getMessage());
}
}

Reading value of characteristic after connecting to service Bluetooth Low Energy Android

after the connection is established I want to read the value from the characteristic and save the value in a byte array.
Here is my function for the reading inside my BluetoothLeService:
public byte[] readWhiteAndIntensityCharacteristic() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return null;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString(UuidAdresssService));
if (mCustomService == null) {
Log.w(TAG, "Custom BLE Service not found");
return null;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString(UuidAdresssWhiteAndIntensityCharastic));
byte[] messageByte = mReadCharacteristic.getValue();
if (messageByte != null && messageByte.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(messageByte.length);
for (byte byteChar : messageByte)
stringBuilder.append(String.format("%02X", byteChar));
s = "0x" + stringBuilder.toString();
Log.v("Scan Activity", s);
if (mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false) {
Log.w(TAG, "Failed to read characteristic");
}
}
return messageByte;
}
This function get called inside my DeviceControlActivity:
public void getStartUpValue(){
Log.w(TAG, "Reading completed");}
if(mBluetoothLeService.readWhiteAndIntensityCharacteristic() == null)
{
Log.w(TAG, "FAILED Reading value failed");
}
startValue = mBluetoothLeService.readWhiteAndIntensityCharacteristic();
Log.w(TAG, "Reading completed");
}
I call the getStartUpValue function after the connection is established.
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
updateConnectionState(R.string.connected);
invalidateOptionsMenu();
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
updateConnectionState(R.string.disconnected);
invalidateOptionsMenu();
clearUI();
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the user interface.
mBluetoothLeService.getSupportedGattServices();
getStartUpValue();
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
}
}
};
The reading is failing everytime but sending values to the characteristic is no problem.
How can I fix this problem?
The solution is: mBluetoothGatt.readCharacteristic(mReadCharacteristic)
After the reading is finished a callback will be called.
Thanks to Andrew Vovk

Gatt 133 exception on read characteristic and followed by gatt 22

Problem:
I am getting a GATT exception 133 when trying to read the characteristics.
I made a delay to read but unfortunately it doesn't works followed by the GATT 22 error.
It happens on few devices but i don't know how to solve this problem.
Code:
public void performRead() {
printMessage("\nperform READ started...");
UUID serviceuid = UUID.fromString(UUID_SERVICE);
if (BDA.getmBluetoothGatt() == null)
return;
BluetoothGattService service = BDA.getmBluetoothGatt().getService(
serviceuid);
UUID characteristicuid = UUID.fromString(UUID_CHARACTERISTIC_STATUS);
BluetoothGattCharacteristic characteristic = null;
if (service != null) {
characteristic = service.getCharacteristic(characteristicuid);
}
if (characteristic != null) {
boolean isSuccess = BDA.getmBluetoothGatt().readCharacteristic(characteristic);
printMessage("isSuccess fire = " + isSuccess);
}
}
Callback:
#Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
String intentAction = "";
if (status == BluetoothGatt.GATT_SUCCESS) {
intentAction = MyUtils.ACTION_READ_SUCCESS;
broadcastNotifyUpdate(characteristic,intentAction);
} else {//**status - 133
intentAction = MyUtils.ACTION_GATT_CHARACTERISTIC_ERROR;
broadcastUpdate(intentAction,""+status);
}
}

Bluetooth BLE service returns status = 10 at onCharacteristicRead

I try to read some configurations from a device I'm connected to, but on the callback - onCharacteristicRead the parameter status=10.
I noticed that status == 0 meas BluetoothGatt.GATT_SUCCESS.
WHAT DOES 10 STANDS FOR????
My code is as follows:
public boolean read(byte[] bytes, String action) {
// is copied from android website example
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return false;
}
BluetoothGattService mCC2540_service = mBluetoothGatt.getService(UUID.fromString(mCC2540_SERVICE_UUID));
if (mCC2540_service == null){
Log.e(TAG, "mCC2540_service == null");
return false;
}
BluetoothGattCharacteristic bluetoothGattCharacteristic = mCC2540_service.getCharacteristic(UUID.fromString("0000fffc-0000-1000-8000-00805f9b34fb"));
bluetoothGattCharacteristic.setValue(bytes);
if ((bluetoothGattCharacteristic.getProperties() | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mBluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true);
}
boolean status = mBluetoothGatt.readCharacteristic(bluetoothGattCharacteristic);
Log.d(TAG, "read() command sent");
return status;
}

Working with BLE Android 4.3 how to write characteristics?

I am working on a BLE project (Android application) using Android 4.3 API, i have used sample BLE app it is only reading characteristics in DeviceControlActivity.activity, but i want to write characteristics and send it to BLE chip on clicking a button. How can I write data on chip cc2540 .. Basically i don't know the step by step procedure to write characteristics.
write i can only see the name and id of device with following piece of code in DeviceControlActivity
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
showDialog("reading");
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.writeCharacteristic(characteristic);
showDialog("writing");
//characteristic.setValue(bytes);
//characteristic.setValue("testing");
//characteristic.setWriteType(BluetoothGattCharacteristic.PERMISSION_WRITE);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
byte[] value = {0x00, (byte) (0xB9) , 0x0D, (byte) (0x90), 0x2F};
if(!characteristic.setValue(value))
{
Log.w(TAG, "Couldn't set characteristic's local value");
//return;
}
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
/*if(!writeCharacteristic.writeCharacteristic(characteristic))
{
Log.w(TAG, "Couldn't write characteristic");
}*/
return true;
}
return false;
}
};
The following code is write characteristic using byte[] data:
public boolean writeCharacteristic(){
//check mBluetoothGatt is available
if (mBluetoothGatt == null) {
Log.e(TAG, "lost connection");
return false;
}
BluetoothGattService Service = mBluetoothGatt.getService(your Services);
if (Service == null) {
Log.e(TAG, "service not found!");
return false;
}
BluetoothGattCharacteristic charac = Service
.getCharacteristic(your characteristic);
if (charac == null) {
Log.e(TAG, "char not found!");
return false;
}
byte[] value = new byte[1];
value[0] = (byte) (21 & 0xFF);
charac.setValue(value);
boolean status = mBluetoothGatt.writeCharacteristic(charac);
return status;
}
Please note that the logic OR in:
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0)" in the original post should be a logic AND for the permission check to work. Same for the second charaProp comparison. Otherwise bot statements are true regardless of the actual permission flag.
The following code is write characteristic using string data in utf-8 format:
public void writeCharacteristic(BluetoothGattCharacteristic characteristic,
String data) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
Log.i(TAG, "characteristic " + characteristic.toString());
try {
Log.i(TAG, "data " + URLEncoder.encode(data, "utf-8"));
characteristic.setValue(URLEncoder.encode(data, "utf-8"));
// TODO
mBluetoothGatt.writeCharacteristic(characteristic);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Hope it helps!
public boolean writeCharacteristic(byte value[],int type){
//check mBluetoothGatt is available
if (mBluetoothGatt == null) {
Log.e(TAG, "lost connection");
return false;
}
BluetoothGattService Service = mBluetoothGatt.getService(UUID_SIMPLESERVICE);
if (Service == null) {
Log.e(TAG, "service not found!");
//////////NO service found...........
return false;
}
BluetoothGattCharacteristic charac1 = null;
boolean status1 = false;
if(type==1) {
charac1 = Service.getCharacteristic(UUID_PORT1);
charac1.setValue(value);
status1 = mBluetoothGatt.writeCharacteristic(charac1);
Log.v("________BLESERVICE____", "___WRITE CHARATERISTICS STATUS:_________"+status1);
onReliableWriteCompleted(status1);
}
if (charac1 == null) {
Log.e(TAG, "char not found!");
return false;
}
Log.v("___TYPE___","______________________"+type);
return status1;
}

Categories

Resources