Cel: Stwórz aplikację na Androida, która odkryje nazwy i adresy urządzeń BT w zasięgu i prześle ich wartości do serwisu internetowego. Urządzenia BT nie były wcześniej połączone z urządzeniem hosta, chcę tylko sondować wszystko, gdy chodzę.Wykrywanie urządzeń Bluetooth w systemie Android - startDiscovery()
Co robiłem:
- porach nad dokumentacją.
- Zaimplementowano lokalne wystąpienie adaptera BT urządzenia hosta.
- Wprowadzono powiadomienie, aby włączyć BT, jeśli nie jest włączone.
- Zarejestrowane odbiorniki i intencje do analizowania
ACTION_FOUNDs
zejścia z startDiscovery(). - Zarejestrowany BLUETOOTH i BLUETOOTH_ADMIN uprawnienia w manifeście.
Rzeczy działają (testowane z przyrostowym rejestrowaniem konsoli) do startDiscovery()
.
Frustracja:
- startDiscovery() - Podejrzewam, że to ja przechodząc w niewłaściwym kontekście. W jakim kontekście należy umieścić tę metodę, aby działała prawidłowo?
Jeśli byłbyś w stanie sprawić, by ta metoda działała, byłbym bardzo wdzięczny za twoją mądrość.
UPDATE - tutaj jest uproszczona wersja uproszczonego kodu, który wywołuje we mnie smutek; to uproszczenie podsumowuje mój błąd. Ten kod działa, nie generuje błędów ani innych błędów, po prostu nie daje żadnego wyniku.
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);
}
}
}
}
Pomoże to poznać błąd, który otrzymujesz ... lub przynajmniej to, co dokładnie prowadzi cię do przekonania, że 'startDiscovery()' działa niepoprawnie. –