2017-01-19 7 views
0

AndroidデバイスのBluetooth経由で5バイトのフレームを受信する必要があります。私はデータフレームを送信することに問題はありませんが、これを正しく受け取る方法はわかりません。文字列を受け取る必要はありません。バイト値だけです。 誰かがそのようなコードを持っていますか? Androidスタジオ2.2.3でプログラミングしていますAndroidデバイスのAndroidアプリでBluetooth経由で5バイトのデータを受信する方法は?

+0

1ブルートゥースまたはBLE? – Jayanth

+0

ブルートゥースSPPプロファイル – Juras

答えて

1

特性に応じて通知/表示を有効にする必要があります。 コマンドを書き込んだ後。 GATTからのコールバックをバイトとして取得します。

1)スキャンデバイス

2)デバイス

device.connectGatt(mContext, autoConnect,BluetoothGattCallback, BluetoothDevice.TRANSPORT_LE); 

BluetoothGattCallbackとつながる - コールバック

このコールバックでは、複数の継承されたメソッドを持っています。この目的のためにこれを使用してください

周辺機器からバイトを取得するには、このメソッドを継承します。

public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 
    throw new RuntimeException("Stub!"); 
} 

3)周辺機器の要件に応じて、表示/通知を有効にする必要があります。

//有効にするには - あなたの文字をパラメータとして指定します。

 private static final UUID CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID =  UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); 


    public final boolean enableIndications(final BluetoothGattCharacteristic characteristic) { 
    Log.d("CheckData", "enableIndications"); 
    final BluetoothGatt gatt = mBluetoothGatt; 
    if (gatt == null || characteristic == null) 
     return false; 

    // Check characteristic property 
    final int properties = characteristic.getProperties(); 
    if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) == 0) 
     return false; 

    gatt.setCharacteristicNotification(characteristic, true); 
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID); 
    if (descriptor != null) { 
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); 
     return gatt.writeDescriptor(descriptor); 
    } 
    return false; 
} 

//有効にするにはNotifciation - パラメータを文字として入力します。

 protected final boolean enableNotifications(final BluetoothGattCharacteristic characteristic, boolean enable) { 
    final BluetoothGatt gatt = mBluetoothGatt; 
    if (gatt == null || characteristic == null) 
     return false; 

    // Check characteristic property 
    final int properties = characteristic.getProperties(); 
    if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0) 
     return false; 

    gatt.setCharacteristicNotification(characteristic, enable); 
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID); 
    if (descriptor != null) { 
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
     return gatt.writeDescriptor(descriptor); 
    } 
    return false; 
} 

4)あなたの尊重された特性に値を書き込みます。

5)応答 characteristic.getValue()//出力を特定のオフセットから文字列としてcharacteristic.getStringValue(1)//出力バイト

public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 

     characteristic.getStringValue(1) // Output Bytes 
     characteristic.getValue() // Output as Byte Array 
     Log.d("Values", characteristic.getStringValue(1)); 
    } 

BluetoothGattCallbackご登録のコールバックに来ますバイト配列として

この回答が役に立ちます。

乾杯アップ投票アップ

ハッピーコーディング

+0

このコードを試して、フィードバックをお寄せいただきありがとうございます – Juras

+0

シリアルポートプロファイルを使用する必要がありますが、BLEを使用する必要はありません。 – Juras

関連する問題