AndroidデバイスのBluetooth経由で5バイトのフレームを受信する必要があります。私はデータフレームを送信することに問題はありませんが、これを正しく受け取る方法はわかりません。文字列を受け取る必要はありません。バイト値だけです。 誰かがそのようなコードを持っていますか? Androidスタジオ2.2.3でプログラミングしていますAndroidデバイスのAndroidアプリでBluetooth経由で5バイトのデータを受信する方法は?
0
A
答えて
0
bluetooth
接続の設定方法に関するチュートリアルは、オフィシャルドキュメントにあります。 https://developer.android.com/guide/topics/connectivity/bluetooth.html
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ご登録のコールバックに来ますバイト配列として
この回答が役に立ちます。
乾杯アップ投票アップ
ハッピーコーディング
関連する問題
- 1. AndroidでBluetooth経由でデータを受信する方法は?
- 2. AndroidアプリでBluetoothデバイスからデータを受信
- 3. AndroidでBluetooth経由で文字列(バイト)を送信する
- 4. AndroidでBluetooth経由でオーディオを受信する
- 5. AndroidでBluetooth経由でデータを送信する最大パケットサイズは?
- 6. Androidデバイスから同じ値の5バイトをブルートゥース経由で送信する方法
- 7. Android - SMS/MMS経由でvCardを送受信する方法は?
- 8. 別のGPS AndroidをBluetooth経由で使用する(AndroidからAndroidデバイス)
- 9. 1つのAndroidデバイスから別のAndroidデバイスにオーディオをストリーミングする方法Bluetooth経由ですか?
- 10. BlueTooth経由で文字列を送受信する方法
- 11. AndroidアプリケーションでBluetooth受信ファイルを受け入れる方法は?
- 12. AndroidでBluetooth経由でInputStreamとOutputStreamを処理する方法
- 13. シリアルデータをBluetooth経由で不特定のデバイスに送信する方法は?
- 14. Android ReceiveReceive Bluetooth経由のテキスト
- 15. Bluetooth経由のAndroidデバッグ
- 16. TCPソケット経由でバイト[]を送受信
- 17. AndroidスタジオでBluetooth経由でデータを読み書きする
- 18. Androidデバイス、USBポート経由でデバイスと通信する
- 19. AndroidでのBluetoothデバイスの修正方法
- 20. Android経由でOBDIIに接続する方法Bluetooth
- 21. 簡単なBluetoothデータ受信機Android
- 22. 3G経由のゲームセンター(GameKit)経由でデータを送受信する
- 23. Androidでアプリをネットワーク経由でデバイス間で通信する方法を教えてください。
- 24. インターネット経由でAndroidデバイスをデバッグする
- 25. OpenCV MatをPCからAndroid経由でSocket経由で送信する方法
- 26. AndroidでBluetooth経由で文字列メッセージを送信するには?
- 27. Bluetooth経由で他のiPhoneにファイルを送信する方法
- 28. AndroidアプリはUSB経由でデスクトップアプリケーションと通信できますか?
- 29. Bluetoothからのデータ受信Androidデバイスの低エネルギーデバイスが非常に遅い
- 30. AndroidデバイスのBluetooth
1ブルートゥースまたはBLE? – Jayanth
ブルートゥースSPPプロファイル – Juras