如何通过Android 4.3的发送文本数据CC2541遥控器? [英] How to send the text data to CC2541 keyfob via Android 4.3?

查看:422
本文介绍了如何通过Android 4.3的发送文本数据CC2541遥控器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,我必须连接到蓝牙设备上的Andr​​oid 4.3。

和我想通过Android应用程序来更改CC2541密钥卡的名称。

我的想法是:

1.There有我可以输入什么,我想在我的Andr​​oid应用程序名称的纯文本。

2.After我键入名字,我按下按钮发送这个文本。

3,如果CC2541接收来自Android应用程序本书,它会改变DEVICENAME [中keyfobdemo.c以下code的]文本:

 静态UINT8 DEVICENAME [] =
{
//完整名称
0x0B中,//第一个数据结构的长度(11个字节长度除外字节)
为0x09,//广告类型=完成地方名
0x4b,//'K'
0x65,//'E'
0x79的,//'Y'
0x66,//'F'
0x6f,//'O'
0X62,//'B'
0x64,//'D'
0x65,//'E'
0x6d,//'M'
0x6f,//'O'
};

类似如下的问题:

1.How发送文本数据CC2541遥控器在Android应用程序4.3 ??

2.How收到有关CC2541侧文本数据??

3.Did我需要使用任何配置文件??

对不起我的英语,而这些问题。

感谢您的方向。


修改

我已经尝试使用0x2A00来获取设备名称服务,但它看到不工作时,我所说的设备名称的功能。

中的name_service为null。

 私有静态最后的UUID Device_Name_UUID = UUID.fromString(00002a00-0000-1000-8000-00805f9b34fb);
私有静态最后的UUID Write_UUID = UUID.fromString(00001800-0000-1000-8000-00805f9b34fb);    公共无效设备名称(){
        BluetoothGattService的name_service = mBluetoothGatt.getService(Write_UUID);
        如果(的name_service == NULL){
            Log.d(TAG的name_service服务未找到!);
            返回;
        }        BluetoothGattCharacteristic设备名称= Name_Service.getCharacteristic(Device_Name_UUID);
        如果(设备名称== NULL){
            Log.d(TAG,设备名称charateristic没有找到!);
            返回;
        }    }
Log.v(TAGreadCharacteristic(设备名称)=+ mBluetoothGatt.readCharacteristic(设备名称));串I =123;
DeviceName.setValue(ⅰ);
Log.v(TAGwriteCharacteristic(设备名称)=+ mBluetoothGatt.writeCharacteristic(设备名称));

它显示以下日志:

  V / BluetoothLeService(3680):readCharacteristic(设备名称)=真
V / BluetoothLeService(3680):writeCharacteristic(设备名称)= FALSE
D / audio_hw_primary(1752年):找到了的/ dev / SND / pcmC0D0p
W / audio_hw_primary(1752年):out_write()限制睡眠时间45351至23219
W / audio_hw_primary(1752年):out_write()限制睡眠时间34263至23219
W / audio_hw_primary(1752年):out_write()限制睡眠时间33696至23219
D / BtGatt.btif(2646):btif_gattc_upstreams_evt:事件3
I / BtGatt.btif(2646):set_read_value unformat.len = 13
D / BtGatt.GattService(2646):onReadCharacteristic() - 地址= 90:59:AF:0B:8A:AB,状态= 0,长度= 13
D / BluetoothGatt(3680):onCharacteristicRead() - 设备= 90:59:AF:0B:8A:AB = UUID状态00002a00-0000-1000-8000-00805f9b34fb = 0

它读取成功,我可以得到设备的名称。

和我引用<一个href=\"https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.device_name.xml\"相对=nofollow>蓝牙页面,设备名称,格式为UTF-8字符串。
但writeCharacteristic假的。


解决方案

我不知道Android的API BLE,但我可以告诉你这是怎么应该与蓝牙低功耗工作。

的装置名称被存储在总协定服务器(cc2541设备上的本地数据库)。如果您连接到BLE装置,你应该能够做一个探索找出数据库的结构,并找到设备名称ATT句柄。

关贸总协定服务器构建了一个UUID(松散定义属性的类型)的属性手柄(在GATT服务器实例中使用的标识符)和值的属性。根据[1]设备名称的UUID是0x2A00。所以,你可以按类型搜索和查找与此UUID把手。

一旦你拥有它只是一个使用客户端关贸总协定Android的API中发送一个写请求该处理与新价值的事情的UUID

编辑:看着,我认为你应该使用的getService的API(为0x18,0x00)来[2]来获得主要服务(它应该包含设备名称),然后writeCharacteristic [3]更新名称。

从[4]它看起来像code应该是这个样子(未测试):

 公共无效writeCharacteristic(字节[]值){
    BluetoothGattService gap_service = mBluetoothGatt.getService(
            UUID.fromString(00001800-0000-1000-8000-00805F9B34FB));
    如果(gap_service == NULL){
        的System.out.println(gap_service空);
        返回;
    }
    BluetoothGattCharacteristic的dev_name = gap_service.getCharacteristic(
            UUID.fromString(00002A00-0000-1000-8000-00805F9B34FB));
    如果(的dev_name == NULL){
        的System.out.println(的dev_name空);
        返回;
    }
    dev_name.setValue(值);
    布尔状态= mBluetoothGatt.writeCharacteristic(的dev_name);
    的System.out.println(写状态:+状态);
}


  • [1] bluetooth.org

  • [2] <一个href=\"http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#getService%28java.util.UUID%29\"相对=nofollow>的getService

  • [3] <一个href=\"http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#writeCharacteristic%28android.bluetooth.BluetoothGattCharacteristic%29\"相对=nofollow> writeCharacteristic

  • [4] <一个href=\"https://devzone.nordicsemi.com/index.php/writing-to-a-characteristic-using-the-android-4-3-api\"相对=nofollow> devzone.nordicsemi.com

I am developing an application where I have to connect to Bluetooth device on Android 4.3.

And I want to change the name of CC2541 Keyfob via the Android application.

My ideas is:

1.There has a Plain Text that I can type the name what I want in my Android application.

2.After I type the name, I push the button to send this text.

3.If the CC2541 receive this text from Android application , it will change the text in the deviceName[] of the following code in keyfobdemo.c:

static uint8 deviceName[] =
{
// complete name
0x0b, // length of first data structure (11 bytes excluding length byte)
0x09, // AD Type = Complete local name
0x4b, // 'K'
0x65, // 'e'
0x79, // 'y'
0x66, // 'f'
0x6f, // 'o'
0x62, // 'b'
0x64, // 'd'
0x65, // 'e'
0x6d, // 'm'
0x6f, // 'o'
};

The question like the following:

1.How to send the text data to CC2541 keyfob in Android application 4.3 ??

2.How to receive the text data on CC2541 side ??

3.Did I need to use any profile ??

Sorry about my English, and these question.

Thanks for your direction.


Edit

I have trying to use 0x2A00 to get the Device Name service , but it seen not working when I call the Device_Name function.

The Name_Service is null.

private static final UUID Device_Name_UUID = UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb");
private static final UUID Write_UUID = UUID.fromString("00001800-0000-1000-8000-00805f9b34fb");





    public void Device_Name(){
        BluetoothGattService Name_Service = mBluetoothGatt.getService(Write_UUID );
        if(Name_Service == null) {
            Log.d(TAG, "Name_Service service not found!");
            return;
        }

        BluetoothGattCharacteristic DeviceName = Name_Service.getCharacteristic(Device_Name_UUID);
        if(DeviceName == null) {
            Log.d(TAG, "DeviceName charateristic not found!");
            return;
        }



    }


Log.v(TAG, "readCharacteristic(DeviceName) = " + mBluetoothGatt.readCharacteristic(DeviceName));

String i = "123";       
DeviceName.setValue(i);
Log.v(TAG, "writeCharacteristic(DeviceName) = " + mBluetoothGatt.writeCharacteristic(DeviceName));

it show the following Log:

V/BluetoothLeService( 3680): readCharacteristic(DeviceName) = true
V/BluetoothLeService( 3680): writeCharacteristic(DeviceName) = false
D/audio_hw_primary( 1752): found out /dev/snd/pcmC0D0p
W/audio_hw_primary( 1752): out_write() limiting sleep time 45351 to 23219
W/audio_hw_primary( 1752): out_write() limiting sleep time 34263 to 23219
W/audio_hw_primary( 1752): out_write() limiting sleep time 33696 to 23219
D/BtGatt.btif( 2646): btif_gattc_upstreams_evt: Event 3
I/BtGatt.btif( 2646): set_read_value unformat.len = 13 
D/BtGatt.GattService( 2646): onReadCharacteristic() - address=90:59:AF:0B:8A:AB, status=0, length=13
D/BluetoothGatt( 3680): onCharacteristicRead() - Device=90:59:AF:0B:8A:AB UUID=00002a00-0000-1000-8000-00805f9b34fb Status=0

it read successful,and I can get the name of device.

And I reference the Bluetooth Page-Device Name , the format is UTF-8 String. But it writeCharacteristic false.

解决方案

I don't know about the Android BLE API, but I can tell you how this is supposed to work with Bluetooth Low Energy.

The device name is stored in the GATT server (a local database on the cc2541 device). If you connect to the BLE device you should be able to do a discover to figure out the structure of the database and find the ATT handle for the device name.

The GATT server is built up of attributes with a UUID (loosely defining the type of attribute) an attribute handle (the identifier used in this instance of the GATT server) and a value. According to [1] the UUID for the device name is 0x2A00. So you can search by type and find the handle with this UUID.

Once you have the UUID it's just a matter of using the GATT client in the Android API to send a write request to this handle with the new value

Edit: Looking at the API I think you should use getService(0x18, 0x00) [2] to get the primary service (which should contain the device name) and then writeCharacteristic[3] to update the name.

From [4] it looks like the code should look something like this (not tested):

public void writeCharacteristic(byte[] value) {
    BluetoothGattService gap_service = mBluetoothGatt.getService(
            UUID.fromString("00001800-0000-1000-8000-00805F9B34FB"));
    if (gap_service == null) {
        System.out.println("gap_service null";);
        return;
    }
    BluetoothGattCharacteristic dev_name = gap_service.getCharacteristic(
            UUID.fromString("00002A00-0000-1000-8000-00805F9B34FB"));
    if (dev_name == null) {
        System.out.println("dev_name null";);
        return;
    }
    dev_name.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(dev_name);
    System.out.println("Write Status: " + status);
}

这篇关于如何通过Android 4.3的发送文本数据CC2541遥控器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆