我已经研究了一段时间,并通过网络将来自多个来源的代码放在一起,但是我仍然没有得到任何结果。
最初我试图扫描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;
}
答案 0 :(得分:1)
您需要启用位置设置才能查看附近的BLE设备。您可以请求运行时位置权限,也可以手动启用它。