/ / Problema di connettività del dispositivo Bluetooth: java, android, bluetooth, android-bluetooth

Problema di connettività del dispositivo Bluetooth: java, android, bluetooth, android-bluetooth

Sto sviluppando un'app Android in cui sto controllando se due dispositivi sono collegati tramite bluetooth

Sto registrando il Broadcast Reciever utilizzando il codice seguente.

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);

this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);

Il BroadcastReceiver si presenta così.

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device

if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
{
Log.e("bluetooth connected","bluetooth connected");
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action))
{
Log.e("bluetooth not connected","bluetooth not connected");
}
}
};

Come mai non funziona. Non so dove sbaglio. Per favore aiuto! Grazie!

risposte:

2 per risposta № 1

Hai l'autorizzazione BLUETOOTH nel tuo manifest?

<uses-permission android:name="android.permission.BLUETOOTH" />

anche invece di registrare due volte un ricevitore e usare due filtri che potresti fare

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

this.registerReceiver(mReceiver, filter);

0 per risposta № 2

hai provato l'autorizzazione bluetooth-admin?

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

0 per risposta № 3

La documentazione di Android afferma che "Le connessioni ACL sono gestite automaticamente dallo stack Bluetooth Android". Probabilmente, non si prevede che saranno gestiti a livello di applicazione; BluetoothDevice.ACTION_ACL_CONNECTED e BluetoothDevice.ACTION_ACL_DISCONNECTED l'invio dipende dal dispositivo e dalla versione del firmware (ad esempio, ho riscontrato che il Nexus S li invia correttamente, mentre il vecchio GT-I5800 non lo fa).