Goal: Build an Android app that discovers the names and addresses of BT devices within range and submits their values to a webservice. BT devices have not b
What context does this method need to be placed within to function properly.
To put it simply, you should use startDiscovery()
when you want your application to discover local Bluetooth devices... for instance, if you wanted to implement a ListActivity
that scans and dynamically adds nearby Bluetooth devices to a ListView
(see DeviceListActivity).
Your usage of the startDiscovery()
method should look something like this:
Define a class variable representing the local Bluetooth adapter.
BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
Check to see if your device is already "discovering". If it is, then cancel discovery.
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
Immediately after checking (and possibly canceling) discovery-mode, start discovery by calling,
mBtAdapter.startDiscovery();
Be very careful in general about accidentally leaving your device in discovery-mode. Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources. For instance, you want to make sure you check/cancel discovery prior to attempting to make a connection. You most likely want to cancel discovery in your onDestroy
method too.
Let me know if this helped... and if you are still having trouble, update your answer with your logcat output and/or any error messages you are getting, and maybe I can help you out a bit more.