Most Android devices support Bluetooth, and most Android ROMs has built-in bluetooth device picker, which is available to other system apps to select a bluetooth device. Theoretically the Bluetooth Device Picker could be reuse in any apps. But for some reasons, the API is not documented, and not published to everyone.
But it is possible to reuse such resources, so I wrote the following code. But due to the using of undocumented API, so it is not garenteed to work on all android devices.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.timnew.bluetooth; | |
import android.bluetooth.BluetoothDevice; | |
import android.bluetooth.BluetoothDevicePicker; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import com.googlecode.androidannotations.annotations.EBean; | |
import com.googlecode.androidannotations.annotations.RootContext; | |
@EBean | |
public class BluetoothDeviceManager implements BluetoothDevicePicker { | |
@RootContext | |
protected Context context; | |
public void pickDevice(BluetoothDevicePickResultHandler handler) { | |
context.registerReceiver(new BluetoothDeviceManagerReceiver(handler), new IntentFilter(ACTION_DEVICE_SELECTED)); | |
context.startActivity(new Intent(ACTION_LAUNCH) | |
.putExtra(EXTRA_NEED_AUTH, false) | |
.putExtra(EXTRA_FILTER_TYPE, FILTER_TYPE_ALL) | |
.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)); | |
} | |
public static interface BluetoothDevicePickResultHandler { | |
void onDevicePicked(BluetoothDevice device); | |
} | |
private static class BluetoothDeviceManagerReceiver extends BroadcastReceiver { | |
private final BluetoothDevicePickResultHandler handler; | |
public BluetoothDeviceManagerReceiver(BluetoothDevicePickResultHandler handler) { | |
this.handler = handler; | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
context.unregisterReceiver(this); | |
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); | |
handler.onDevicePicked(device); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2009 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package android.bluetooth; | |
/** | |
* A helper to show a system "Device Picker" activity to the user. | |
* | |
* @hide | |
*/ | |
public interface BluetoothDevicePicker { | |
public static final String EXTRA_NEED_AUTH = "android.bluetooth.devicepicker.extra.NEED_AUTH"; | |
public static final String EXTRA_FILTER_TYPE = "android.bluetooth.devicepicker.extra.FILTER_TYPE"; | |
public static final String EXTRA_LAUNCH_PACKAGE = "android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE"; | |
public static final String EXTRA_LAUNCH_CLASS = "android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS"; | |
/** | |
* Broadcast when one BT device is selected from BT device picker screen. | |
* Selected {@link BluetoothDevice} is returned in extra data named | |
* {@link BluetoothDevice#EXTRA_DEVICE}. | |
*/ | |
public static final String ACTION_DEVICE_SELECTED = "android.bluetooth.devicepicker.action.DEVICE_SELECTED"; | |
/** | |
* Broadcast when someone want to select one BT device from devices list. | |
* This intent contains below extra data: | |
* - {@link #EXTRA_NEED_AUTH} (boolean): if need authentication | |
* - {@link #EXTRA_FILTER_TYPE} (int): what kinds of device should be | |
* listed | |
* - {@link #EXTRA_LAUNCH_PACKAGE} (string): where(which package) this | |
* intent come from | |
* - {@link #EXTRA_LAUNCH_CLASS} (string): where(which class) this intent | |
* come from | |
*/ | |
public static final String ACTION_LAUNCH = "android.bluetooth.devicepicker.action.LAUNCH"; | |
/** | |
* Ask device picker to show all kinds of BT devices | |
*/ | |
public static final int FILTER_TYPE_ALL = 0; | |
/** | |
* Ask device picker to show BT devices that support AUDIO profiles | |
*/ | |
public static final int FILTER_TYPE_AUDIO = 1; | |
/** | |
* Ask device picker to show BT devices that support Object Transfer | |
*/ | |
public static final int FILTER_TYPE_TRANSFER = 2; | |
/** | |
* Ask device picker to show BT devices that support | |
* Personal Area Networking User (PANU) profile | |
*/ | |
public static final int FILTER_TYPE_PANU = 3; | |
/** | |
* Ask device picker to show BT devices that support Network Access Point (NAP) profile | |
*/ | |
public static final int FILTER_TYPE_NAP = 4; | |
} |
In the code I uses the Android Annotations, but it should be easy to remove the dependency on Android Annotations
by adding a constructor to BluetoothDeviceManager that accepts Context
.