问题描述
我正在使用的是Android 4.3的API一个BLE项目(Android应用程序),我用的样品BLE的应用程序是只读特性DeviceControlActivity.activity,但我想写的特点,并将其发送到BLE芯片上单击按钮。我怎么能写在芯片CC2540数据..基本上我不知道一步程序步骤写特性。
写,我只能看到设备的名称和id与下面这段code在DeviceControlActivity
私人最终ExpandableListView.OnChildClickListener servicesListClickListner =
新ExpandableListView.OnChildClickListener(){
@覆盖
公共布尔onChildClick(ExpandableListView父,视图V,INT groupPosition,
INT childPosition,长ID){
如果(mGattCharacteristics!= NULL){
最后BluetoothGattCharacteristic特征=
mGattCharacteristics.get(groupPosition)获得(childPosition);
最终诠释charaProp = characteristic.getProperties();
如果((charaProp | BluetoothGattCharacteristic.PROPERTY_READ)大于0){
//如果有一个活动的通知上的特性,明确
//它首先因此它不更新用户接口上的数据字段。
如果(mNotifyCharacteristic!= NULL){
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic,假);
mNotifyCharacteristic = NULL;
}
mBluetoothLeService.readCharacteristic(特性);
的ShowDialog(读书);
}
如果((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE)大于0){
//如果有一个活动的通知上的特性,明确
//它首先因此它不更新用户接口上的数据字段。
如果(mNotifyCharacteristic!= NULL){
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic,假);
mNotifyCharacteristic = NULL;
}
mBluetoothLeService.writeCharacteristic(特性);
的ShowDialog(写);
//characteristic.setValue(bytes);
//characteristic.setValue("testing);
//characteristic.setWriteType(BluetoothGattCharacteristic.PERMISSION_WRITE);
}
如果((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY)大于0){
mNotifyCharacteristic =特性;
mBluetoothLeService.setCharacteristicNotification(
特性,真);
}
byte []的值= {0×00,(字节)(0xB9)值,0X0D,(字节)(0×90),值为0x2F};
如果(!characteristic.setValue(值))
{
Log.w(TAG,无法设置特色的本地值);
//返回;
}
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
/*if(!writeCharacteristic.writeCharacteristic(characteristic))
{
Log.w(TAG,无法写入特性);
} * /
返回true;
}
返回false;
}
};
以下code是写特性使用字节[]
数据:
公共布尔writeCharacteristic(){
//检查mBluetoothGatt可用
如果(mBluetoothGatt == NULL){
Log.e(TAG,失去联系);
返回false;
}
BluetoothGattService服务= mBluetoothGatt.getService(您服务);
如果(服务== NULL){
Log.e(TAG的服务没有找到!);
返回false;
}
BluetoothGattCharacteristic charac =服务
.getCharacteristic(你的特性);
如果(charac == NULL){
Log.e(TAG,CHAR没有找到!);
返回false;
}
byte []的值=新的字节[1];
值[0] =(字节)(21安培;为0xFF);
charac.setValue(值);
布尔值状态= mBluetoothGatt.writeCharacteristic(charac);
返回状态;
}
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;
}
这篇关于工作与BLE的Android 4.3怎么写的特点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!