我有一个类“ DeviceServicesBackground”来连接低功耗蓝牙设备。 连接在构造函数中启动。 为了连接多个BLE设备,我创建了“ DeviceServicesBackground”的多个实例。 我可以在“ BroadcastReceiver”类型的“ gattUpdateReceiver”中连接多个设备并从中接收数据。
我的问题是我无法识别哪个设备向我发送了数据。每个“ BroadcastReceiver”实例都可以从随机BLE设备接收数据。 请咨询如何从不同设备中分离数据?
DeviceServicesBackground的代码:
package com.sample.hrv;
import android.app.Activity;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import java.util.List;
import com.sample.hrv.R;
import com.sample.hrv.adapters.BleServicesAdapter;
import com.sample.hrv.adapters.BleServicesAdapter.OnServiceItemClickListener;
import com.sample.hrv.demo.DemoHeartRateSensorActivity;
import com.sample.hrv.demo.DemoSensorActivity;
import com.sample.hrv.sensor.BleHeartRateSensor;
import com.sample.hrv.sensor.BleSensor;
import com.sample.hrv.sensor.BleSensors;
public class DeviceServicesBackground {
private final static String TAG = DeviceServicesActivity.class.getSimpleName();
public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";
private TextView connectionState;
private TextView dataField;
private TextView heartRateField;
private TextView intervalField;
private Button demoButton;
private ExpandableListView gattServicesList;
private BleServicesAdapter gattServiceAdapter;
private String deviceName;
private String deviceAddress;
private BleService bleService;
private boolean isConnected = false;
private BleSensor<?> activeSensor;
private BleSensor<?> heartRateSensor;
private OnServiceItemClickListener serviceListener;
private static DeviceScanActivity mainActivity = null;
// Code to manage Service lifecycle.
private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
bleService = ((BleService.LocalBinder) service).getService();
if (!bleService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
//finish();
}
// Automatically connects to the device upon successful start-up initialization.
bleService.connect(deviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
bleService = null;
}
};
// Handles various events fired by the Service.
// ACTION_GATT_CONNECTED: connected to a GATT server.
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
// ACTION_DATA_AVAILABLE: received data from the device. This can be a result of read
// or notification operations.
private final BroadcastReceiver gattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BleService.ACTION_GATT_CONNECTED.equals(action)) {
isConnected = true;
updateConnectionState(R.string.connected);
// invalidateOptionsMenu();
} else if (BleService.ACTION_GATT_DISCONNECTED.equals(action)) {
isConnected = false;
updateConnectionState(R.string.disconnected);
// invalidateOptionsMenu();
clearUI();
} else if (BleService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the user interface.
displayGattServices(bleService.getSupportedGattServices());
enableHeartRateSensor();
} else if (BleService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(intent.getStringExtra(BleService.EXTRA_SERVICE_UUID), intent.getStringExtra(BleService.EXTRA_TEXT));
}
}
};
// If a given GATT characteristic is selected, check for supported features. This sample
// demonstrates 'Read' and 'Notify' features. See
// http://d.android.com/reference/android/bluetooth/BluetoothGatt.html for the complete
// list of supported characteristic features.
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (gattServiceAdapter == null)
return false;
final BluetoothGattCharacteristic characteristic = gattServiceAdapter.getChild(groupPosition, childPosition);
final BleSensor<?> sensor = BleSensors.getSensor(characteristic.getService().getUuid().toString());
if (activeSensor != null)
bleService.enableSensor(activeSensor, false);
if (sensor == null) {
bleService.readCharacteristic(characteristic);
return true;
}
if (sensor == activeSensor)
return true;
activeSensor = sensor;
bleService.enableSensor(sensor, true);
return true;
}
};
private final BleServicesAdapter.OnServiceItemClickListener demoClickListener = new BleServicesAdapter.OnServiceItemClickListener() {
@Override
public void onDemoClick(BluetoothGattService service) {
Log.d(TAG, "onDemoClick: service" +service.getUuid().toString());
final BleSensor<?> sensor = BleSensors.getSensor(service.getUuid().toString());
if (sensor == null)
return;
final Class<? extends DemoSensorActivity> demoClass;
if (sensor instanceof BleHeartRateSensor)
demoClass = DemoHeartRateSensorActivity.class;
else
return;
}
@Override
public void onServiceEnabled(BluetoothGattService service, boolean enabled) {
if (gattServiceAdapter == null)
return;
final BleSensor<?> sensor = BleSensors.getSensor(service.getUuid().toString());
if (sensor == null)
return;
if (sensor == activeSensor)
return;
if (activeSensor != null)
bleService.enableSensor(activeSensor, false);
activeSensor = sensor;
bleService.enableSensor(sensor, true);
}
@Override
public void onServiceUpdated(BluetoothGattService service) {
final BleSensor<?> sensor = BleSensors.getSensor(service.getUuid().toString());
if (sensor == null)
return;
bleService.updateSensor(sensor);
}
};
private void clearUI() {
gattServicesList.setAdapter((SimpleExpandableListAdapter) null);
dataField.setText(R.string.no_data);
heartRateField.setText(R.string.no_data);
intervalField.setText(R.string.no_data);
}
public void setServiceListener(OnServiceItemClickListener listener) {
this.serviceListener = listener;
}
public DeviceServicesBackground( DeviceScanActivity activity, String deviceName, String deviceAddress) {
mainActivity = activity;
this.deviceName = deviceName;
this.deviceAddress = deviceAddress;
// Sets up UI references.
final Intent gattServiceIntent = new Intent(activity, BleService.class);
activity.bindService(gattServiceIntent, serviceConnection, activity.BIND_AUTO_CREATE);
activity.registerReceiver(gattUpdateReceiver, makeGattUpdateIntentFilter());
if (bleService != null) {
final boolean result = bleService.connect(deviceAddress);
Log.d(TAG, "Connect request result=" + result);
}
}
private void updateConnectionState(final int resourceId) {
}
private void displayData(String uuid, String data) {
if (data != null) {
mainActivity.displayData(uuid,deviceName + "### "+ data);
// if (uuid.equals(BleHeartRateSensor.getServiceUUIDString())) {
// heartRateField.setText(data);
// } else {
// dataField.setText(data);
// }
}
}
private boolean enableHeartRateSensor() {
if (gattServiceAdapter == null)
return false;
final BluetoothGattCharacteristic characteristic = gattServiceAdapter
.getHeartRateCharacteristic();
Log.d(TAG,"characteristic: " + characteristic);
final BleSensor<?> sensor = BleSensors.getSensor(characteristic
.getService()
.getUuid()
.toString());
if (heartRateSensor != null)
bleService.enableSensor(heartRateSensor, false);
if (sensor == null) {
bleService.readCharacteristic(characteristic);
return true;
}
if (sensor == heartRateSensor)
return true;
heartRateSensor = sensor;
bleService.enableSensor(sensor, true);
this.setServiceListener(demoClickListener);
return true;
}
private void displayGattServices(List<BluetoothGattService> gattServices) {
if (gattServices == null)
return;
gattServiceAdapter = new BleServicesAdapter(mainActivity, gattServices);
gattServiceAdapter.setServiceListener(demoClickListener);
//gattServicesList.setAdapter(gattServiceAdapter);
}
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BleService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BleService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BleService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BleService.ACTION_DATA_AVAILABLE);
return intentFilter;
}
}