- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个连接到 BLE 设备并读取我需要检查的特定 GATT 特性和服务的 Android 应用程序。我使用 Android Dev 站点中的 BluetoothLeGATT 示例作为引用。我可以毫无问题地连接到预定义地址并读取 GATT 属性更新。
我接下来要做的是能够同时连接到两个 BLE 设备。然而,这似乎是一个挑战。
我所做的基本上是复制连接到单个 BLE 设备所需的代码。我有 2 个 BluetoothLeServices、2 个用于 GattCharacteristics 和 Gatt 服务数据的 ArrayLists,以及 2 个服务连接和 2 个用于 GattCallbacks 的广播接收器。
但是,在我的 GattCallback 函数中,我得到了相同的消息——就好像它们连接到同一个区域一样。这是我的代码:
public class MainActivity extends AppCompatActivity {
/*
UUIDs
Dog Block - 20:CD:39:87:DC:AA
Cat Block - 20:CD:39:87:DF:82
*/
private final String TAG = this.getClass().getSimpleName();
private BluetoothAdapter mBluetoothAdapter;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
private static final long SCAN_PERIOD = 10000;
private ArrayList<String> addressID = new ArrayList<>();
private ArrayList<BluetoothDevice> deviceList = new ArrayList<>();
private boolean mScanning = false;
private boolean mConnected = false;
private BluetoothLeService mBluetoothLeService;
private BluetoothLeService mBluetoothLeService1;
private ArrayList<ArrayList<BluetoothGattCharacteristic>> mGattCharacteristics =
new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
private ArrayList<ArrayList<BluetoothGattCharacteristic>> mGattCharacteristics1 =
new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
private final String LIST_NAME = "NAME";
private final String LIST_UUID = "UUID";
ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
= new ArrayList<ArrayList<HashMap<String, String>>>();
ArrayList<HashMap<String, String>> gattServiceData1 = new ArrayList<HashMap<String, String>>();
ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData1
= new ArrayList<ArrayList<HashMap<String, String>>>();
private BluetoothGattCharacteristic mNotifyCharacteristic;
private BluetoothGattCharacteristic mNotifyCharacteristic1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler = new Handler();
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
addressID.add("20:CD:39:87:DC:AA");
addressID.add("20:CD:39:87:DF:82");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume");
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
scanLeDevice(true);
if (mBluetoothLeService != null) {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
scanLeDevice(false);
unregisterReceiver(mGattUpdateReceiver);
unregisterReceiver(mGattUpdateReceiver1);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
mBluetoothLeService = null;
mBluetoothLeService1 = null;
}
private void scanLeDevice(final boolean enable) {
if (enable) {
Log.e(TAG, "scanLeDevice true");
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
Log.e(TAG, "scanLeDevice false");
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
// Device scan callback.
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() {
deviceList.add(device);
Log.e(TAG, "deviceList count = " + deviceList.size());
if(deviceList.size() >= 2){
checkDevices();
}
}
});
}
};
private void checkDevices() {
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
Intent gattServiceIntent1 = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent1, mServiceConnection1, BIND_AUTO_CREATE);
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
registerReceiver(mGattUpdateReceiver1, makeGattUpdateIntentFilter());
}
//TODO -- connect functions here
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
return intentFilter;
}
// Code to manage Service lifecycle.
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
Log.e(TAG, "connecting to " + deviceList.get(0).getAddress());
mBluetoothLeService.connect("20:CD:39:87:DC:AA");
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
private final ServiceConnection mServiceConnection1 = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService1 = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService1.initialize()) {
Log.e(TAG, "1Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up initialization.
Log.e(TAG, "1connecting to " + deviceList.get(1).getAddress());
mBluetoothLeService1.connect("20:CD:39:87:DF:82");
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
Log.e(TAG, "connected");
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
Log.e(TAG, "disconnected");
mConnected = false;
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the user interface.
Log.e(TAG, "gatt services discovered");
displayGattServices(mBluetoothLeService.getSupportedGattServices());
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
Log.e(TAG, "data available");
String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
Log.e(TAG, "data is = " + data);
}
}
};
private final BroadcastReceiver mGattUpdateReceiver1 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
Log.e(TAG, "1connected");
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
Log.e(TAG, "1disconnected");
mConnected = false;
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the user interface.
Log.e(TAG, "1gatt services discovered");
displayGattServices1(mBluetoothLeService1.getSupportedGattServices());
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
Log.e(TAG, "1data available");
String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
Log.e(TAG, "1data is = " + data);
}
}
};
private void displayGattServices(List<BluetoothGattService> gattServices) {
if (gattServices == null) return;
Log.e(TAG, "display gatt services not null.");
String uuid = null;
String unknownServiceString = getResources().getString(R.string.unknown_service);
String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
// Loops through available GATT Services.
for (BluetoothGattService gattService : gattServices) {
HashMap<String, String> currentServiceData = new HashMap<String, String>();
uuid = gattService.getUuid().toString();
currentServiceData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));
currentServiceData.put(LIST_UUID, uuid);
gattServiceData.add(currentServiceData);
ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
new ArrayList<HashMap<String, String>>();
List<BluetoothGattCharacteristic> gattCharacteristics =
gattService.getCharacteristics();
ArrayList<BluetoothGattCharacteristic> charas =
new ArrayList<BluetoothGattCharacteristic>();
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
charas.add(gattCharacteristic);
HashMap<String, String> currentCharaData = new HashMap<String, String>();
uuid = gattCharacteristic.getUuid().toString();
currentCharaData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
currentCharaData.put(LIST_UUID, uuid);
gattCharacteristicGroupData.add(currentCharaData);
if(uuid.equals(SampleGattAttributes.DOG_CHARACTERISTIC_CONFIG)){
Log.e(TAG, "uuid characteristic detected");
final int charaProp = gattCharacteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
Log.e(TAG, "gatt characteristics read!");
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(gattCharacteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
Log.e(TAG, "gatt characteristics notify!");
mNotifyCharacteristic = gattCharacteristic;
mBluetoothLeService.setCharacteristicNotification(
gattCharacteristic, true);
}
}
}
mGattCharacteristics.add(charas);
gattCharacteristicData.add(gattCharacteristicGroupData);
}
}
private void displayGattServices1(List<BluetoothGattService> gattServices) {
if (gattServices == null) return;
Log.e(TAG, "1display gatt services not null.");
String uuid = null;
String unknownServiceString = getResources().getString(R.string.unknown_service);
String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
mGattCharacteristics1 = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
// Loops through available GATT Services.
for (BluetoothGattService gattService : gattServices) {
HashMap<String, String> currentServiceData = new HashMap<String, String>();
uuid = gattService.getUuid().toString();
currentServiceData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));
currentServiceData.put(LIST_UUID, uuid);
gattServiceData1.add(currentServiceData);
ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
new ArrayList<HashMap<String, String>>();
List<BluetoothGattCharacteristic> gattCharacteristics =
gattService.getCharacteristics();
ArrayList<BluetoothGattCharacteristic> charas =
new ArrayList<BluetoothGattCharacteristic>();
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
charas.add(gattCharacteristic);
HashMap<String, String> currentCharaData = new HashMap<String, String>();
uuid = gattCharacteristic.getUuid().toString();
currentCharaData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
currentCharaData.put(LIST_UUID, uuid);
gattCharacteristicGroupData.add(currentCharaData);
if (uuid.equals(SampleGattAttributes.DOG_CHARACTERISTIC_CONFIG)) {
Log.e(TAG, "1uuid characteristic detected");
final int charaProp = gattCharacteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
Log.e(TAG, "1gatt characteristics read!");
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic1 != null) {
mBluetoothLeService1.setCharacteristicNotification(
mNotifyCharacteristic1, false);
mNotifyCharacteristic1 = null;
}
mBluetoothLeService1.readCharacteristic(gattCharacteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
Log.e(TAG, "1gatt characteristics notify!");
mNotifyCharacteristic1 = gattCharacteristic;
mBluetoothLeService1.setCharacteristicNotification(
gattCharacteristic, true);
}
}
}
mGattCharacteristics1.add(charas);
gattCharacteristicData1.add(gattCharacteristicGroupData);
}
}
}
我所做的是,一旦获得我想要连接的 2 个地址,我就会初始化所有必要的连接、服务和广播接收器。但是,我收到的 bluetoothLeGatt 消息是一样的。根据它连接到 Dog 或 Cat block ,我会得到以下几行:
data = dog
1data = dog
来自 LogCat。似乎它们连接到同一台设备。
我检查了我的代码,我什至硬编码了地址,但无济于事。
最佳答案
我已经与多台设备建立了连接并且工作正常。我还制作了一项用于扫描的服务和一项用于每次 ble 通信的服务。
请确保不要在通信部分使用绑定(bind)服务,因为断开连接可能是个问题(在我的情况下)。
对于扫描部分,我制作了一个字符串列表,其中包含 Mac 地址。当我在我的扫描仪中找到一个设备时,我通过 broadcastreceiver 将该设备发送到我的主要 Activity ,然后我将它传输到它的服务。所以每个连接都在自己的服务中运行,有自己的广播接收器和过滤器设置。
为确保这不是您的广播接收器的问题,请在您立即显示输出的每个服务中创建一个控制台日志。我怀疑您的设备与同一服务器有两个连接。
关于Android BLE - 连接到多个设备似乎失败,两个连接的 GATT 响应是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35406168/
我正在开发一个使用 BLE Android 到 iOS 的聊天应用程序,现在我正在使用下面两个库作为引用 https://github.com/izumin5210/Bletia https://gi
我正在从 HCI 套接字接收 EVT_LE_ADVERTISING_REPORT。我想区分 BLE 信标和普通 BLE 设备(解析设备名称等)我有点困惑是否所有 BLE 设备都会发出信标或者它们是不同
我正在从 HCI 套接字接收 EVT_LE_ADVERTISING_REPORT。我想区分 BLE 信标和普通 BLE 设备(解析设备名称等)我有点困惑是否所有 BLE 设备都会发出信标或者它们是不同
我在我的 iOS 应用程序项目中使用核心蓝牙框架。我有以下关于蓝牙低功耗的问题 - iOS 中单个中央设备是否可以连接多个外围设备? 多个中央设备可以与单个外围设备连接吗? 单个 iOS 设备可以同时
我正在创建一个 iOS 和一个 Android 应用程序,它们从蓝牙传感器读取一些数据并将它们保存在数据库中。 即使应用程序终止,我也想提供保存传感器数据的能力。 仅供引用。当应用程序在后台时,我已经
我正在使用核心蓝牙框架并扫描一些设备,例如 micromax A250、micromax A116、samsung grand neo、HTC 610 和 ipod 5s,然后我无法扫描 samsun
这个任务的目的是通过BLE设备连接iPhone的BLE,访问iphone的ANCS。请注意,iPhone 中没有安装应用程序来打开 iPhone 的 BLE。那么,如果我们从 iPhone 设置中启用
我在为我的 BLE 设备开发安卓软件时遇到了问题。我的软件可以找到我的设备和 GATT 服务,但在我的服务中找不到任何特征。 我检查了 android-sdk-4.4.2 源码,找到了一些代码。 ht
有什么方法可以从 BleExplr、LightBlue 等通用 BLE 扫描应用程序中隐藏 BLE 设备? 最佳答案 您可以配置外围设备以使用服务请求。在这种模式下,中央为它们提供服务和外设扫描 -
我有 3 个组件。 Activity1 有连接和断开 BLE 连接的按钮 Activity2 需要从 BLE 设备获取数据。 Service 所有连接逻辑(如 getRemoteDevice()、co
我有一个 BLE 设备,它在通过相当标准的用户界面(点击 UITableView 中显示的设备条目)选择后连接到 iOS 设备。 连接非常简单 - 一些内部处理,然后调用 CBCentralManag
我正在尝试使用 Meteor 和这个 Cordova 插件 - https://github.com/don/cordova-plugin-ble-central - 使用 meteor add co
我坚持在 Android Lollipop 智能手机和 BLE 设备(带有 BLE 模块的 TI 实验板)之间实现连接。我使用以下调用进行连接: device.connectGatt(context,
我正在使用 react-native-ble-plx 在我的应用程序中实现蓝牙 蓝牙扫描工作正常,但它在 android 中包含重复项,而在 iOS 中工作正常,因为 allowDuplicates
我正在尝试在我的 Windows 笔记本电脑上设置一个基于 Nodejs 的演示,并使用额外的 BLE 适配器将我的笔记本电脑连接到另一个 BLE 设备 (Anki Overdrive)。我在互联网上
我正在研究基于 Android 的 BLE 接近感应功能,需要一些信息。目前我看到没有适用于 android 的 BLE 信标制造商。到目前为止,我为 iPhone 找到了 2 个。1) http:/
我正在使用 cordova 和 BLE 插件开发一个应用程序。我想通过 BLE 根据硬编码的已知 device.name 自动连接到 ESP32,而无需用户按下连接按钮。 我的想法是: 在设备准备就绪
我正在尝试实现从/向蓝牙设备接收和发送消息的模块。 我一直在寻找可以通过蓝牙搜索、连接和发送消息的 NuGet 包,但找不到适用于 Linux 的任何东西。 我正在使用 .NET Core 2.1 和
我正在尝试寻找一种方法来了解如何在 iOS 中获取 BLE mac??? 这将适用于所有 BLE,不会存在制造商依赖性。我们正在寻找扫描时间的解决方案。我可以在扫描时间内区分 BLE 吗? 如果获取M
我已阅读技术规范并试图了解为什么 BLE 4.2 比 BLE 4.1 更快? 我们能否发送大于 20 字节的数据包或者连接间隔是否更快? 我想了解是什么让 BLE 4.2 更快。 最佳答案 与早期相比
我是一名优秀的程序员,十分优秀!