Reputation: 27
I've been working on this for a while and putting together code from multiple sources over the net, however I'm still not getting any results.
Initially I was trying to scan for BLE devices and view them in the overflow menu and connect from there, but couldn't get that to work...
Now I have attempted to use a list view for the devices, but still no luck. I'm v new to this so not entirely sure what links to what. I can initiate a scan using startDiscovery and nothing crashes - but no luck.
I'll try provide as much code as possible, however I don't know where the problem lies. This is a university assignment.
I've tried adding a BroadcastReceiver but that hasn't changed anything I'm meant to be connecting to a SensorTag device and receive some sensor readings via GATT connection - I've already added code for this but I don't think it plays a part in the connection process so I haven't included it.
@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;
}
Upvotes: 0
Views: 342
Reputation: 1858
You need to enable location settings to view nearby BLE devices. You can either request runtime location permission or enable it manually.
Upvotes: 1