J'ai besoin d'obtenir une liste des périphériques Bluetooth disponibles dans la zone en utilisant google android 2.1. Ce n'est pas juste besoin d'une liste de ces appareils, j'ai besoin d'un identifiant unique pour chaque appareil trouvé et j'ai besoin d'un indicateur, comment "bon" le signal est reçu (comme le "niveau" dans android .wifi.ScanResult) ... Comment je fais ça?Comment numériser pour les appareils Bluetooth disponibles dans la gamme Android?
9
A
Répondre
7
35
Découvrez le code ci-dessous:
Lancement de la recherche
mBluetoothAdapter.startDiscovery();
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Finding devices
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
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
1
méthode d'appel bluetoothScanning, le contexte est nécessaire
void bluetoothScanning(){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
context.registerReceiver(mReceiver, filter);
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
}
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Log.i("Device Name: " , "device " + deviceName);
Log.i("deviceHardwareAddress " , "hard" + deviceHardwareAddress);
}
}
};
Résultat
Nom: LE Bose Revolve + SoundLink deviceHardwareAddress : MAC 04: 52: C7: D1: B2: 76
.....
quelqu'un a de l'expérience sur cette chose RSSI? je suis un peu précaire ici, puisque cela est défini comme une constante? – xenonite
Votre premier lien - http://developer.android.com/guide/topics/wireless/bluetooth.html#FindingDevices est mort – Dayan
Lien a actuellement cassé – Charlie