如何在android中扫描可用的蓝牙设备?
问题描述:
我需要获得该地区可用的蓝牙设备列表使用谷歌Android 2.1。如何在android中扫描可用的蓝牙设备?
事情是,我不只需要这些设备的列表,我需要为每个设备找到一个唯一的ID,我需要一个指标,如何“好”的信号接收(如“水平”在Android .wifi.ScanResult)...我该怎么做?
答
退房代码:
开始搜索
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);
答
bluetoothScanning,需要调用方法方面
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);
}
}
};
结果
名称:LE-百色旋转+的SoundLink deviceHardwareAddress :MAC 04:52:C7:D1:B2:76
.....
有人对此RSSI有经验吗?我在这里有点不安全,因为这被定义为一个常量? – xenonite 2010-07-03 10:35:53
你的第一个链接 - http://developer.android.com/guide/topics/wireless/bluetooth.html#FindingDevices已经死了 – Dayan 2016-05-24 21:01:22
链接已经打破了 – Charlie 2016-07-24 21:14:14