Descubrimiento de dispositivos Bluetooth en Android - startDiscovery ()
Gol: Cree una aplicación para Android que descubra los nombres y las direcciones de los dispositivos de BT dentro del alcance y envíe sus valores a un servicio web. Los dispositivos BT no se han enlazado previamente al dispositivo host, solo quiero sondear todo a medida que camino.
Qué he hecho:
Poreado sobre la documentación.Implementé una instancia local del adaptador BT del dispositivo host.Se implementó una notificación para habilitar BT si no está habilitada.Receptores de transmisión registrados e intenciones de analizar elACTION_FOUNDs
saliendo deinicioDiscovery ().RegistradoBLUETOOTH yBLUETOOTH_ADMIN Permisos en el manifiesto.Las cosas funcionan (según lo probado con el registro de consola incremental) hastastartDiscovery()
.
Frustración:
Si hubiera podido hacer funcionar este método, apreciaría mucho su sabiduría.
ACTUALIZAR - Aquí hay una versión simplificada simplificada del código que me está causando dolor. Esta simplificación recapitula mi error. Este código corre, no arrojacat.log
Errores u otros errores, simplemente no da ningún resultado.
<code>package aqu.bttest; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.widget.Toast; public class BT2Activity extends Activity { private BluetoothAdapter mBTA; private SingBroadcastReceiver mReceiver; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //register local BT adapter mBTA = BluetoothAdapter.getDefaultAdapter(); //check to see if there is BT on the Android device at all if (mBTA == null){ int duration = Toast.LENGTH_SHORT; Toast.makeText(this, "No Bluetooth on this handset", duration).show(); } //let's make the user enable BT if it isn't already if (!mBTA.isEnabled()){ Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBT, 0xDEADBEEF); } //cancel any prior BT device discovery if (mBTA.isDiscovering()){ mBTA.cancelDiscovery(); } //re-start discovery mBTA.startDiscovery(); //let's make a broadcast receiver to register our things mReceiver = new SingBroadcastReceiver(); IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(mReceiver, ifilter); } private class SingBroadcastReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //may need to chain this to a recognizing function if (BluetoothDevice.ACTION_FOUND.equals(action)){ // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a Toast String derp = device.getName() + " - " + device.getAddress(); Toast.makeText(context, derp, Toast.LENGTH_LONG); } } } </code>
}