开发者

Bluetooth on Android: startDiscovery doesn't work and I have bluetooth enabled

I am having troubles using startDiscovery(). Here i开发者_StackOverflow中文版s the problem, I have registered the following Intents to my BroadcastReceiver like this:

IntentFilter filterFound = new IntentFilter(BluetoothDevice.ACTION_FOUND);
IntentFilter filterStart = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
IntentFilter filterStop = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(myReceiver, filterFound);
registerReceiver(myReceiver, filterStart);
registerReceiver(myReceiver, filterStop);

Here is my broadcast receiver:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "onReceive (myReceiver)");
    String action = intent.getAction();
    // When discovery finds a device
    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 ListView
        Log.d(TAG,device.getName() + " " + device.getAddress());
        deviceListAdapter.add(device.getName() + "\n" + device.getAddress());
    }
    if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
        Log.d(TAG, "Started discovery");
    }
    if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        Log.d(TAG, "Finished discovery");
    }
}
};

When I call startDiscovery on my bluetooth adapter (bluetooth existing and enabled), nothing happens and I can't see anything in the logcat regarding the BroadcastReceiver.

Did you already experience that kind of problem? Any help is appreciated :)

Thanks


Make sure you have the appropriate permissions on your AndroidManifest.xml. In particular, to perform a discovery you need <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />.


As mentioned by Brad Hein make sure AndroidManifest has bluetooth permission.

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

For registering bluetooth scan result broadcast receiver follow the below method.

IntentFilter bluetoothFilter = new IntentFilter();
bluetoothFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
bluetoothFilter.addAction(BluetoothDevice.ACTION_FOUND);
bluetoothFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(myReceiver, bluetoothFilter);

Start bluetooth discovery as follows.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (bluetoothAdapter == null) {
   // No bluetooth Hardware found.
}
if (!bluetoothAdapter.isEnabled()) {
    // Bluetooth is OFF
}

if (bluetoothAdapter.isDiscovering()) {
    // cancel the discovery if it has already started
   bluetoothAdapter.cancelDiscovery();
}

if (bluetoothAdapter.startDiscovery()) {
    // bluetooth has started discovery
}


Try to declare the receiver as follows :

private class SingBroadcastReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction(); 

        if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
            log(getTimeStamp() + ":discovery started");
        }

And here is the order needed in the function calls :

1- cancelDiscovery() + getAdapter()

2- start the receiver as follows :

        mReceiver = new SingBroadcastReceiver();

        IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        ifilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        ifilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        this.registerReceiver(mReceiver, ifilter);

3- startDiscovery()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜