[英]BLE Reading Constant Data Stream after Enabling Notifications
更新下面
我正在嘗試從連接到arduino設置的HM-10藍牙設備接收恆定的串行數據流。 我正在使用Google Play商店上的BLE掃描儀來查找MAC地址和UUID。 有一個可以讀取,寫入和通知的特征。 在應用程序上啟用通知可顯示恆定的數據流,而讀取則顯示地址。 我不知道啟用通知如何允許訪問數據流。 我已經編寫了使我能夠連續讀取(顯示地址,就像應用程序一樣)的代碼,我想知道如何訪問通知數據。 抱歉,如果我使用的術語錯誤,則我不太了解BLE通知。 這是我的Connect,Runnable和Callback代碼:
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void Connect(View view) throws InterruptedException {
Device = Adapter.getRemoteDevice("3C:A3:08:94:C3:11");
Gatt = Device.connectGatt(this, true, GattCallback);
}
private final BluetoothGattCallback GattCallback = new BluetoothGattCallback() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
}
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Service = Gatt.getService(UUID.fromString("0000FFE0-0000-1000-8000-00805F9B34FB"));
Characteristic = Service.getCharacteristic(UUID.fromString("0000FFE1-0000-1000-8000-00805F9B34FB"));
Gatt.setCharacteristicNotification(Characteristic, true);
thread.start();
} else {
}
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for (byte byteChar : data) {
stringBuilder.append(String.format("%02X ", byteChar));
}
final String strReceived = stringBuilder.toString();
ErrorID.setText(strReceived);
}
}
};
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void run() {
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(uuid);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Gatt.writeDescriptor(descriptor);
while (true) {
Gatt.readCharacteristic(Characteristic);
}
}
抱歉,如果代碼不是很“干凈”,
謝謝!
刪除讀取100%cpu使用率特征的while循環。 准備通知時,只需完成setCharacteristicNotification和描述符寫操作即可。
只需實現onCharacteristicChanged回調,然后將在每次通知時調用該回調。
UPDATE
因此,現在讓textView顯示來自藍牙模塊的數據。 現在的問題是,除非調用Connect方法(我按下connect按鈕),否則這些值將不會更新。 我懷疑這是因為達到了20個字節的限制。 有沒有辦法讓我連續更新值而無需重新連接? 我也在嘗試在顯示時將值保存在文本文件中,但是我的文本文件為空。 這是我更新的代碼:
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void Connect(View view) throws InterruptedException {
Device = Adapter.getRemoteDevice("3C:A3:08:94:C3:11");
Gatt = Device.connectGatt(this, true, GattCallback);
}
private final BluetoothGattCallback GattCallback = new BluetoothGattCallback() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
}
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Service = Gatt.getService(UUID.fromString("0000FFE0-0000-1000-8000-00805F9B34FB"));
Characteristic = Service.getCharacteristic(UUID.fromString("0000FFE1-0000-1000-8000-00805F9B34FB"));
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
descriptor = Characteristic.getDescriptor(uuid);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Gatt.setCharacteristicNotification(Characteristic, true);
Gatt.writeDescriptor(descriptor);
}
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
final String strReceived = characteristic.getStringValue(0);
try {
FileOutputStream fos = new FileOutputStream(myFile);
OutputStreamWriter osw = new OutputStreamWriter(openFileOutput("Test.txt", MODE_APPEND));
osw.write(strReceived);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
ErrorID.setText(strReceived);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for (byte byteChar : data) {
stringBuilder.append(String.format("%02X ", byteChar));
}
final String strReceived = stringBuilder.toString();
ErrorID.setText(strReceived);
}
}
};
您可以增加最大 使用OnServicesDiscovered Callback方法中的以下代碼行來限制數據包的字符數:
mBluetoothGatt.requestMtu(80);
代替“ 80”,輸入您計划接收的最大字節數。
然后,您要在BluetoothGattCallback中包括以下情況並帶有系統日志打印,以確認您已成功更新字符限制:
public void onMtuChanged(BluetoothGatt bluetoothGatt, int mtu, int status){
System.out.println(" +++++++ MTU CHANGE SUCCESSFUL +++++++");
}
至於“通過通知接收串行數據”,我已經不知所措了,因為我一直在努力尋找一個簡單的解釋,以了解如何做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.