我正在开发一种硬件设备,它通过 BLE 向 Android 应用程序发送连续的数据流。 Android 应用程序接收此数据作为 GATT 通知,然后处理此数据并将其保存到数据库中。
项目详细配置如下:
问题
当数据从硬件设备传输到Android手机上运行的BLE数据捕获应用程序时,没有收到所有数据包。它只收到大约 35-45 个数据包,而预期的数据包数量是 50。
更令人惊讶的是,当我们使用BLE数据包嗅探器时,Android手机嗅探和显示的数据(不完整/不正确的数据)之间存在完美匹配。这让我相信硬件在连接到 Android 手机而不是发送所有数据时表现不同。
当我们将相同的硬件与 iOS BLE 数据捕获应用程序一起使用时,可以正确接收数据。
我对Android中BLE数据捕获的这种行为感到困惑和无能为力。 iOS 设备上的应用程序如何能够正确捕获所有数据,而安卓手机上的应用程序根本无法正确捕获数据?
有没有人在安卓上使用BLE app遇到过这种丢包/数据不正确的问题?欢迎任何意见。非常感谢您的提前帮助。
Android 应用程序使用标准 BLE 代码通过 BLE 连接到设备。我使用的 Android 代码如下所示:
import android.annotation.TargetApi;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.nfc.Tag;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
@TargetApi(21)
public class BLETestActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private int REQUEST_ENABLE_BT = 1;
private Handler mHandler;
private static final long SCAN_PERIOD = 10000;
private BluetoothLeScanner mLEScanner;
private ScanSettings settings;
private List<ScanFilter> filters;
private BluetoothGatt mGatt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bletest);
mHandler = new Handler();
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "BLE Not Supported",
Toast.LENGTH_SHORT).show();
finish();
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
@Override
protected void onResume() {
super.onResume();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
if (Build.VERSION.SDK_INT >= 21) {
Log.i("innnnn","spinnnn - " + Build.VERSION.SDK_INT);
mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
filters = new ArrayList<ScanFilter>();
}
scanLeDevice(true);
}
}
@Override
protected void onPause() {
super.onPause();
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
scanLeDevice(false);
}
}
@Override
protected void onDestroy() {
if (mGatt == null) {
return;
}
mGatt.close();
mGatt = null;
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_CANCELED) {
//Bluetooth not enabled.
finish();
return;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mLEScanner.stopScan(mScanCallback);
}
}
}, SCAN_PERIOD);
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mLEScanner.startScan(filters, settings, mScanCallback);
}
} else {
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mLEScanner.stopScan(mScanCallback);
}
}
}
private ScanCallback mScanCallback = null;/* new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i("callbackType", String.valueOf(callbackType));
Log.i("result", result.toString());
BluetoothDevice btDevice = result.getDevice();
connectToDevice(btDevice);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult sr : results) {
Log.i("ScanResult - Results", sr.toString());
}
}
@Override
public void onScanFailed(int errorCode) {
Log.e("Scan Failed", "Error Code: " + errorCode);
}
};*/
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("onLeScan", device.toString());
connectToDevice(device);
}
});
}
};
public void connectToDevice(BluetoothDevice device) {
if (mGatt == null) {
mGatt = device.connectGatt(this, false, gattCallback);
scanLeDevice(false);// will stop after first device detection
}
}
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.i("onConnectionStateChange", "Status: " + status);
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
Log.i("gattCallback", "STATE_CONNECTED");
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.e("gattCallback", "STATE_DISCONNECTED");
break;
default:
Log.e("gattCallback", "STATE_OTHER");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
List<BluetoothGattService> services = gatt.getServices();
// Log.i("onServicesDiscovered", services.toString());
for(int i=0;i<services.size();i++) {
List<BluetoothGattCharacteristic> charList = services.get(i).getCharacteristics();
for (int j = 0; j < charList.size();j++) {
BluetoothGattCharacteristic characteristic = charList.get(j);
if (characteristic.getUuid().compareTo(Constants.HEART_DATA_UUID) == 0) {
Log.i(BLETestActivity.class.getSimpleName(), "Characteristic UUID is " + characteristic.getUuid());
mGatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(Constants.HEART_DATA_DESCRIPTOR_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mGatt.writeDescriptor(descriptor);
}
}
}
}
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
// Retrieve the byte values
// Convert them to hex values
byte[] data = characteristic.getValue();
// The following array contains data in the follwing form:
// 0-1 - running counter counts
// 2-4, 5-7, 8-10 - Sample 1 data for channel 1, channel2 and channel 3
// 11-13, 14-16, 17-19 - Sample 2 data for channel 1, channel 2 and channel 3
// Log.i(TAG," ------ " + Thread.currentThread().getId());
String heartBeatDataInHex = bytesToHex(data);
// An error packet is received after every 17 samples.
// Checking to make sure that this is not an error packet
if (!(heartBeatDataInHex.substring(4, 10).equalsIgnoreCase("FFFFFF") && heartBeatDataInHex.substring(26, 40).equalsIgnoreCase("FFFFFFFFFFFFFF"))) {
Log.i("testdata", heartBeatDataInHex + " ---- " + Thread.currentThread().getId());
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic
characteristic, int status) {
Log.i("onCharacteristicRead", characteristic.toString());
gatt.disconnect();
}
};
private String bytesToHex(byte[] bytes) {
final char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
最佳答案
我遇到过类似的问题! 我进行了一些测试,确定最小连接间隔为 48 毫秒,android api 级别 18。 (connection interval for BLE on Galaxy S3 Android 4.3)。
可能的解决方案:
使连接间隔变慢(小于 48ms)。
从 api 级别 21 到更高级别,您可以更改 CONNECTION_PRIORITY android developer修改间隔连接,但这意味着更多的能量消耗。
关于Android BLE 通知丢包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778872/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:
我们正在开发一个需要推送通知的WP8应用程序。为了测试它,我们使用CURL命令行运行推送通知POST请求,确保它实际连接,使用客户端SSL证书进行身份验证并发送正确的数据。我们确实知道,当我们收到对设备的推送时,这项工作是有效的。这是我们一直用于测试目的的CURL命令:curl--certclient_cert.pem-v-H"Content-Type:text/xml"-H"X-WindowsPhone-Target:Toast"-H"X-NotificationClass:2"-XPOST-d"MytitleMysubtitle"https://db3.notify.live.ne
我在nginx+unicorn后面运行一系列Rails/Sinatra应用程序,零停机部署。我喜欢这个设置,但Unicorn需要一段时间才能完成重新启动,所以我想在完成时发送某种通知。我能在Unicorn文档中找到的唯一回调与workerfork相关,但我认为这些回调对此不起作用。这是我从赏金中寻找的东西:老unicorn主人启动新主人,然后新主人开始它的worker,然后旧主人停止它的worker并让新主人接管。我想在交接完成后执行一些ruby代码。理想情况下,我不想为此实现任何复杂的流程监控。如果这是唯一的方法,那就这样吧。但在走那条路之前,我正在寻找更简单的选择。
我一直试图在这里寻找答案,但我找不到任何有用的东西。我已经对我的Rails应用程序实现了:success和:dangerflash通知。它工作得很好,即:success是绿色的,:danger是红色的,有一个关闭按钮等等,但是自从添加了一些邮件文件后,我的:success现在显示为红色?application.html.erb摘录:×contact_mailer.rbclassContactMailercontacts_controller.rbclassContactsController还有,contact_email.html.erbNewMessagefromHoo
我已更新到Rails2.3.10、Rack1.2.1,现在我的所有即时消息都没有显示。我发现在重定向期间,通知是这样传递的redirect_to(@user,:notice=>"Sorrytherewasanerror")在我看来闪存哈希是空的!map:ActionController::Flash::FlashHash{}但是您可以在Controller中看到该消息。是什么原因?session{:home_zip=>"94108",:session_id=>"xxx",:flash=>{:notice=>"Sorrytherewasanerror"},:user_credential
我正在尝试使用ruby中的seleniumwebdriver从gmail桌面通知中获取数据 最佳答案 开箱即用的想法,用Selenium截屏并用OCR处理图像?https://github.com/suyesh/ocr_space我假设Selenium只允许您与页面数据交互。 关于ruby-如何在seleniumwebdriver-ruby中自动化桌面通知,我们在StackOverflow上找到一个类似的问题: https://stackoverflo
OpenSSL::SSL::SSLError(SSL_connectSYSCALLreturned=5errno=0state=SSLv3readserversessionticketA):为苹果推送通知Houstongem集成库。自上两个月以来,它运行顺利,但现在在应用程序中出现错误。尝试多种解决方案来解决问题。也尝试使用新的证书pem文件,但遇到相同的错误..有时它可以工作请帮忙解决问题。 最佳答案 错误严格来说是您使用了错误的APNS证书。它可能已过期,或者它只是一个旧证书的类型(在2015年12月之前创建)。一年前,Appl
我正在开发一个应用程序,用户可以在其中添加他们的Gmail帐户,我会对他们的电子邮件进行一些分类工作。我想在任何注册帐户收到新电子邮件时收到通知。一个解决方案是通过IMAP不断轮询帐户并保存我获取的最后一封电子邮件日期以检查是否有新邮件,但这有很多开销。知道如何监控Gmail并在收到新电子邮件时收到通知并将其与Rails应用集成吗?例如,是否有扩展程序可以执行此操作并将发布请求发送到我的Rails应用程序? 最佳答案 我很确定IMAP是这里唯一的答案。您可能想看看IDLE是否有效——我读过相互矛盾的答案。如果是这样,它比轮询响应更快
有谁知道任何好的RoR通知插件/gem。我需要以某种方式存储事件并在他/她离线时将它们显示给用户,或者在在线时以一种很好的方式将它们呈现给用户。这类似于Facebook通知,用户收到新消息、评论、点赞等通知。谢谢 最佳答案 有一个名为Mailboxer的gem具有相似的功能。 关于ruby-on-rails-Rails中的通知系统,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6