Domanda

I'm trying to have my app's service listen for Bluetooth connection and disconnection attempts, so I can dynamically check/support Bluetooth tethering network communication.

First I have two Samsung S4s (running CyanogenMod 10.2, which is Android 4.3.1 based) which I can pair just fine. If I set one device to Bluetooth tether, when the other connects, a new bt-pan network interface is created and DHCP is used to assign IPs. I confirmed this using iwconfig and ifconfig in shell.

I have the following perms in my app: (there's more, I'm just pointing out the BT perms I added)

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

Here's my Service's onCreate where I set my IntentFilters: (note I've got Toasts here, but I was working with Logging originally)

@Override
public void onCreate() {
    super.onCreate();
    ...     
    this.mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);

    mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);
}

Here's my BroadcastReceiver implementation:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equals(BluetoothDevice.ACTION_FOUND)) {
            Toast.makeText(getApplicationContext(), "BT found", Toast.LENGTH_SHORT).show();
        } else if(action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
            Toast.makeText(getApplicationContext(), "BT Connected", 
        } else if(action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
            Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(getApplicationContext(), "BT Disconnect requested", Toast.LENGTH_SHORT).show();
        }
    }
}

Now, when I turn Bluetooth off/on, connect/disconnect to a paired device, nothing fires. I've payed around with the devices from both ends. Nothing is broadcast.

Anyone have a suggestion? I really need to receive these bluetooth events. Please don't point to another post/site with the same perms and intent filters. Thanks.

È stato utile?

Soluzione

I have a very similar code which works, the only major difference i found was this:

mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);

i beleive that registerReceiver should be called from the context you want to get intents to. try calling the method from this. i.e remove the mLocalBroadcastManager like:

registerReceiver(mMessageReceiver, filter);

Altri suggerimenti

If you Register the Receiver on the Manifest instead of the Activity, you can receive broadcasts even if you do not have the app opened, you can do this:

    <receiver android:name=".BluetoothReceiver" >
        <intent-filter>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.BOND_STATE_CHANGED" />
        </intent-filter>
    </receiver>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top