2016-06-02 5 views
0

私はBLE113モジュールにデータを送信するアンドロイドアプリを持っています。どのタイプのタイプが「ユーザー」かというGATT特性を通じてデータを受け取ります。私は文字列としてデータを取得することができます。私が24などの整数を送ると、文字列 '24'として受け取られます。とにかくこの文字列番号を整数型に変換できますか?BGScript:文字列を整数に変換する

これはgatt.xmlのものです。

<characteristic uuid="xxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxx" id="configuration"> 
    <description>Config Register</description> 
    <properties read="true" write="true"/> 
    <value type="user" /> 
</characteristic> 

これはAndroid側のスニペットで、整数値 '1'を書きます。

String str = "1"; 
    try { 
     byte[] value = str.getBytes("UTF-8"); 
     chara.setValue(value); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 
. 
. 
. 
boolean status = mBluetoothGatt.writeCharacteristic(chara); 

BGScript側でデータを整数 '1'として受信したいとします。私は変換に何か悪いことをしていますか?整数を送るのを助けてください。

GATT特性のタイプ 'USER'で何かをしなければなりませんか? 'hex'または 'utf-8'に変更すると、問題は解決されますか? charaは型であると仮定すると、あなたのAndroidプロジェクトで

<characteristic uuid="xxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxx" id="configuration"> 
    <description>Config Register</description> 
    <properties read="true" write="true"/> 
    <value type="hex">00</value> 
</characteristic> 

、:そのようなgatt.xmlファイルで

+0

使用しているサンプルコードを追加してください。送信時と受信時の両方簡単な型変換の問題にぶち当たっているかもしれません。 – SJoshi

+0

こんにちは、私はスニペットで質問を編集しました。ありがとうTed –

答えて

0

、「進」に「ユーザー」からの値の種類を変更し、それにいくつかの固定長を与え、

:あなたのBGScript(私は仮定し、あなたがそう言っていなかった)で、 attributes_value()イベントでは、あなたが好きなの整数を保存することができ、その後

int newConfigRegValue = 1; 
chara.setValue(newConfigRegValue, BluetoothGattCharacteristic.FORMAT_UINT8, 0); 
boolean status = mBluetoothGatt.writeCharacteristic(chara); 

BluetoothGattCharacteristic、あなたのようなもので特性への書き込み

dim configRegister 
... 
event attributes_value(connection, reason, handle, offset, value_len, value_data) 
... 
    if handle = configuration then 
     configRegister = value_data(0:value_len)  
    end if 
... 
end 

-Ted

-Begin編集 -

あなたもそれを行うことができます。

あなたgatt.xmlにおける特性は次のようになります。読み出し要求に来るとき

<characteristic uuid="xxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxx" id="configuration"> 
    <description>Config Register</description> 
    <properties read="true" write="true"/> 
    <value type="user">0</value> 
</characteristic> 

その後、あなたのBGScriptファイルには、値を提供する必要があります。これは、attributes_user_read_request()イベントで行われます。 。同様に:

event attributes_user_read_request(connection, handle, offset, maxsize) 
    if handle = configuration then 
     # Do something to retreive the configRegister value 
     # If you need to read from external EEPROM or something, save the 'connection' value so you can use it in the callback event 
     call attributes_user_read_response(connection, 0, 1, configRegister(0:1)) 
    end if 
end 
+0

を参照してください。これは私を助けます。何か方法があれば、私はこれをタイプ 'ユーザー'で行うことができますか? –

+0

答えを更新しました。編集されたセクションを参照してください。 – Ted

関連する問題