首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过Android4.3将文本数据发送到CC2541 keyfob?

如何通过Android4.3将文本数据发送到CC2541 keyfob?
EN

Stack Overflow用户
提问于 2013-11-04 08:09:16
回答 2查看 6.2K关注 0票数 2

我正在开发一个应用程序,我必须连接到Android 4.3上的蓝牙设备。

我想通过安卓应用程序更改CC2541 Keyfob的名称。

我的想法是:

1.有一个纯文本,我可以在我的Android应用程序中键入我想要的名称。

2.键入姓名后,按下按钮发送此文本。

3.如果CC2541从Android应用程序接收到此文本,它将更改keyfobdemo.c中以下代码的deviceName[]中的文本:

代码语言:javascript
复制
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'
};

问题如下:

1.如何将文本数据发送到安卓应用程序4.3 ??中的CC2541 keyfob

2.如何在CC2541端接收文本数据??

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

抱歉,我的英语,还有这个问题。

谢谢你的指导。

编辑

我尝试使用0x2A00来获得设备名服务,但当我调用Device_Name函数时,它似乎无法工作。

Name_Service为null.

代码语言:javascript
复制
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));

它显示了以下日志:

代码语言:javascript
复制
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

它读起来很成功,我可以知道设备的名字。

我参考的是蓝牙页面-设备名称,格式是UTF-8字符串.但它是writeCharacteristic错误的。

EN

Stack Overflow用户

发布于 2015-02-06 18:34:27

这是我的解决方案,添加到Vegar的背景信息关贸总协定,简介等。

这是基于simpleBLEPeripheral SDK中的CC2541应用程序和sensortag应用程序。simpleBLEPeripheral特性允许您读取多字节特性,我对其进行了修改以启用写操作。C simpleProfile_WriteAttrCB()方法需要一个额外的case语句:

代码语言:javascript
复制
  case SIMPLEPROFILE_CHAR5_UUID:

    //Validate the value
    // Make sure it's not too long

    if ( len >= SIMPLEPROFILE_CHAR5_LEN )
    {
      status = ATT_ERR_INVALID_VALUE_SIZE;
    }

    //Write the value
    if ( status == SUCCESS )
    {
      uint8 *pCurValue = (uint8 *)pAttr->pValue;
      osal_memcpy(pCurValue+offset, pValue, len);

      notifyApp = SIMPLEPROFILE_CHAR5;    
    }

    break;

在Android端,下面的代码放在DeviceActivity.java中。请原谅这些乱七八糟的代码,这是一个快速的黑客攻击。它接受一个字符串,找到十六进制表示,然后作为特征更新发送给CC2541。

代码语言:javascript
复制
 void writeString() {
    UUID servUuid = SensorTagGatt.UUID_STR_SERV;
    UUID configUuid = SensorTagGatt.UUID_STR_DATA;
    BluetoothGattService serv = mBtGatt.getService(servUuid);
    BluetoothGattCharacteristic config = serv.getCharacteristic(configUuid);

    int OAD_BLOCK_SIZE = 18;
    int OAD_BUFFER_SIZE = OAD_BLOCK_SIZE + 2;
    int GATT_WRITE_TIMEOUT = 300; // Milliseconds

    String msg = new String();
    byte[] mOadBuffer = hexStringToByteArray("e04fd020ea3a6910a2d808002b30309daabbccdd");

    // Send block
    config.setValue(mOadBuffer);
    boolean success = mBtLeService.writeCharacteristic(config);

    if (success) {
        // Update stats
        if (!mBtLeService.waitIdle(GATT_WRITE_TIMEOUT)) {
            success = false;
            msg = "GATT write timeout\n";
        }
    } else {
        msg = "GATT writeCharacteristic failed\n";
    }
    if (!success) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

}

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

关键是确保您的UUID匹配,否则什么都不起作用。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19763777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档