无法使用BluetoothLeGatt应用程序从BLE设备读取/写入某些特性

时间:2016-12-01 05:45:15

标签: android bluetooth-lowenergy

我一直在使用BluetoothLeGatt应用程序。我试图从 BLE设备 - TI CC2541 Keyfob 读取特性。我能够读取和写入一些特征,但未能对其他一些特征这样做。 所有这些服务和特征都列在expandableListView中。但是在选择其中一些时,它们的价值并没有显示出来。 任何人都可以帮我解决这个问题。

有没有办法使用特征值句柄

读取值

1 个答案:

答案 0 :(得分:0)

**For Write characterstics Steps :

1. First you have to do indicate
2. After executing indication onDecriptor write sucessfull is executed.Here you have to start write characterstics.**

// for indicate the follwing code is in BluetoothLEService

 public void indicateCharacteristic(UUID serviceUUID, UUID characteristicUuid, boolean isIndicate) {
        try {
            UUID serviceuid = serviceUUID;
            if (serviceuid != null && mBluetoothGatt != null) {
                BluetoothGattService service = mBluetoothGatt.getService(serviceuid);
                UUID characteristicuid = characteristicUuid;
                BluetoothGattCharacteristic characteristic = null;
                if (service != null) {
                    characteristic = service.getCharacteristic(characteristicuid);
                    //Enable local notifications
                    if (mBluetoothGatt != null) {
                        mBluetoothGatt.setCharacteristicNotification(characteristic, isIndicate);
                        ArrayList<BluetoothGattDescriptor> gattdescriptor = (ArrayList<BluetoothGattDescriptor>) characteristic
                                .getDescriptors();

                        for (int k = 0; k < gattdescriptor.size(); k++) {

                            BluetoothGattDescriptor descriptor = gattdescriptor.get(k);
                            if (descriptor.getUuid().toString().equalsIgnoreCase(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG))
                                writeGattDescriptorForIndication(descriptor, isIndicate);
                        }

                    }
                }
            }
        } catch (Exception e) {
            Log.d("device", "not found");
        }
    }

    // Write gatt descriptor
    public void writeGattDescriptorForIndication(BluetoothGattDescriptor d, boolean isIndicate) {
        boolean test;
        if (isIndicate) {
            d.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            d.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        } else {
            d.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
        }
        //  test = mBluetoothGatt.readDescriptor(d);  // return value = true
        // Log.d("test",""+test);
        test = mBluetoothGatt.writeDescriptor(d);
        Log.d("test", "" + test);
    }


@Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        if (characteristic.getUuid().toString().equals(GattAttributes.BATTERY_LEVEL_CHARACTERSTIC_UUID))
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);

    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {

                writeStartCommand();
                Log.d(TAG, "volume Descriptor writing successful");

        } else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
            // this is where the tricky part comes
            if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
                Log.e(TAG, "Bonding required!!!");
            } else {
                // this situation happens when you try to connect for the
                // second time to already bonded device
                // it should never happen, in my opinion
                Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug?");
                // I don't know what to do here
                // This error was found on Nexus 7 with KRT16S build of
                // Andorid 4.4. It does not appear on Samsung S4 with
                // Andorid 4.3.
            }
        } else {
            Log.e(TAG, "Error writing descriptor, status: " + status);
        }

    }
};

public void writeStartCommand() {
    int val = 0x55;
    doWrite(UUID.fromString(GattAttributes.BATTERY_LEVEL_SERVICE_UUID), UUID.fromString(GattAttributes.VOLUME_START_SERVICE_UUID), val);
    waitSometime(100);
}

public synchronized void doWrite(UUID gatservice_uuid, UUID char_uuid, int value) {
    try {
        byte[] value1 = new byte[1];
        value1[0] = (byte) (Integer.valueOf(value) & 0xFF);
        BluetoothGattService mSVC = mBluetoothGatt.getService(gatservice_uuid);
        BluetoothGattCharacteristic mCH = mSVC.getCharacteristic(char_uuid);
        mCH.setValue(value1);
        Log.d("write val", "" + value1);
        // mCH.setValue(value, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
        // mCH.setWriteType(BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE);
        if (mBluetoothGatt.writeCharacteristic(mCH)) {
            waitSometime(100);
            Log.d("sucess", "write characteristics successfully stored-" + char_uuid);
        } else {
            Log.d("fail", "write characteristics failed-" + char_uuid);
           /* if(char_uuid.toString().equalsIgnoreCase(GattAttributes.CALIBRATION_START_COMMAND_UUID)){
                Thread.sleep(100);
                isWriting
                writeStopCommand();
            }else {
                Toast.makeText(CalibrationLeService.this, "Write Characteristics failed"+char_uuid.toString(), Toast.LENGTH_SHORT).show();
            }*/
            //sendBroadcast(new Intent(BluetoothLeForegroundService.ACTION_FINISH));
        }
    } catch (Exception e) {
        Log.d("characteristic id is", "discoverd services not available");
    }
}

public synchronized void waitSometime(int seconds) {
    try {
        Thread.sleep(seconds);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

//然后最后在你的活动中调用

public static String BATTERY_LEVEL_SERVICE_UUID         = "5956efdd-7272-4bfe-937a-f17c70e86b55"; // Volume level service UUID
public static String BATTERY_LEVEL_CHARACTERSTIC_UUID   = "a5bd1e6a-db71-4da5-9b42-a59800e4538b";     
 mBluetoothLeForegroundService.indicateCharacteristic(UUID.fromString(GattAttributes.BATTERY_LEVEL_SERVICE_UUID), UUID.fromString(GattAttributes.BATTERY_LEVEL_CHARACTERSTIC_UUID), true);
相关问题