为什么我无法查看和连接到蓝牙 LE 设备? (Android)
Why can't I view and connect to a Bluetooth LE device? (Android)
我已经为此工作了一段时间,并通过网络将来自多个来源的代码放在一起,但我仍然没有得到任何结果。
最初我试图扫描 BLE 设备并在溢出菜单中查看它们并从那里连接,但无法正常工作...
现在我尝试对设备使用列表视图,但仍然没有成功。我对此很陌生,所以不完全确定什么链接到什么。我可以使用 startDiscovery 启动扫描并且没有崩溃 - 但运气不好。
我会尝试提供尽可能多的代码,但我不知道问题出在哪里。这是大学作业。
我试过添加 BroadcastReceiver,但没有任何改变
我打算连接到 SensorTag 设备并通过 GATT 连接接收一些传感器读数 - 我已经为此添加了代码,但我认为它不会在连接过程中发挥作用,所以我没有包括它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
vHandler = new Handler();
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); // access bluetooth manager
vBluetoothAdapter = manager.getAdapter();
vTags = new ArrayList < BluetoothDevice > (); // store bluetooth devices
// Initialise list view adapter
ListView vListView = (ListView) findViewById(R.id.list);
vLeDeviceListAdapter = new LeDeviceListAdapter();
vListView.setAdapter(vLeDeviceListAdapter);
IntentFilter vfilter = new IntentFilter();
vfilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
vfilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
vfilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
vfilter.addAction(BluetoothDevice.ACTION_FOUND);
}
//Device Scan Callback
private BluetoothAdapter.LeScanCallback vLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
vLeDeviceListAdapter.addDevice(device);
vLeDeviceListAdapter.notifyDataSetChanged();
}
});
Log.i(TAG, "New LE Device: " + device.getName() + " @ " + rssi);
}
};
private void scanLeDevice(final boolean enable) {
int SCAN_PERIOD = 10000;
if (enable) {
// Stops scanning after a pre-defined scan period.
vHandler.postDelayed(new Runnable() {
@Override
public void run() {
vScanning = false;
stopScan();
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
vScanning = true;
startScan();
} else {
vScanning = false;
stopScan();
}
invalidateOptionsMenu();
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList < BluetoothDevice > vLeDevices;
private LayoutInflater vInflator;
LeDeviceListAdapter() {
super();
vLeDevices = new ArrayList < > ();
vInflator = Activity_Scan.this.getLayoutInflater();
}
void addDevice(BluetoothDevice device) {
if (!vLeDevices.contains(device)) {
vLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return vLeDevices.get(position);
}
void clear() {
vLeDevices.clear();
}
@Override
public int getCount() {
return vLeDevices.size();
}
@Override
public Object getItem(int i) {
return vLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = vInflator.inflate(R.layout.listitem_device, viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = vLeDevices.get(i);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
}
您需要启用位置设置才能查看附近的 BLE 设备。您可以请求运行时位置权限或手动启用它。
我已经为此工作了一段时间,并通过网络将来自多个来源的代码放在一起,但我仍然没有得到任何结果。
最初我试图扫描 BLE 设备并在溢出菜单中查看它们并从那里连接,但无法正常工作...
现在我尝试对设备使用列表视图,但仍然没有成功。我对此很陌生,所以不完全确定什么链接到什么。我可以使用 startDiscovery 启动扫描并且没有崩溃 - 但运气不好。
我会尝试提供尽可能多的代码,但我不知道问题出在哪里。这是大学作业。
我试过添加 BroadcastReceiver,但没有任何改变 我打算连接到 SensorTag 设备并通过 GATT 连接接收一些传感器读数 - 我已经为此添加了代码,但我认为它不会在连接过程中发挥作用,所以我没有包括它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
vHandler = new Handler();
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); // access bluetooth manager
vBluetoothAdapter = manager.getAdapter();
vTags = new ArrayList < BluetoothDevice > (); // store bluetooth devices
// Initialise list view adapter
ListView vListView = (ListView) findViewById(R.id.list);
vLeDeviceListAdapter = new LeDeviceListAdapter();
vListView.setAdapter(vLeDeviceListAdapter);
IntentFilter vfilter = new IntentFilter();
vfilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
vfilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
vfilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
vfilter.addAction(BluetoothDevice.ACTION_FOUND);
}
//Device Scan Callback
private BluetoothAdapter.LeScanCallback vLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
vLeDeviceListAdapter.addDevice(device);
vLeDeviceListAdapter.notifyDataSetChanged();
}
});
Log.i(TAG, "New LE Device: " + device.getName() + " @ " + rssi);
}
};
private void scanLeDevice(final boolean enable) {
int SCAN_PERIOD = 10000;
if (enable) {
// Stops scanning after a pre-defined scan period.
vHandler.postDelayed(new Runnable() {
@Override
public void run() {
vScanning = false;
stopScan();
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
vScanning = true;
startScan();
} else {
vScanning = false;
stopScan();
}
invalidateOptionsMenu();
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList < BluetoothDevice > vLeDevices;
private LayoutInflater vInflator;
LeDeviceListAdapter() {
super();
vLeDevices = new ArrayList < > ();
vInflator = Activity_Scan.this.getLayoutInflater();
}
void addDevice(BluetoothDevice device) {
if (!vLeDevices.contains(device)) {
vLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return vLeDevices.get(position);
}
void clear() {
vLeDevices.clear();
}
@Override
public int getCount() {
return vLeDevices.size();
}
@Override
public Object getItem(int i) {
return vLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = vInflator.inflate(R.layout.listitem_device, viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = vLeDevices.get(i);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
}
您需要启用位置设置才能查看附近的 BLE 设备。您可以请求运行时位置权限或手动启用它。