如何在android中扫描范围内可用的蓝牙设备?
我需要使用谷歌安卓2.1获取该地区可用蓝牙设备的列表 问题是,我不仅仅需要这些设备的列表,我需要找到的每个设备的唯一id,我需要一个指示器,信号接收的“好”程度(比如android.wifi.ScanResult中的“级别”)。。。我该怎么做?查看下面的代码: 开始搜索如何在android中扫描范围内可用的蓝牙设备?,android,bluetooth,scanning,Android,Bluetooth,Scanning,我需要使用谷歌安卓2.1获取该地区可用蓝牙设备的列表 问题是,我不仅仅需要这些设备的列表,我需要找到的每个设备的唯一id,我需要一个指示器,信号接收的“好”程度(比如android.wifi.ScanResult中的“级别”)。。。我该怎么做?查看下面的代码: 开始搜索 mBluetoothAdapter.startDiscovery(); mReceiver = new BroadcastReceiver() { public void onReceive(Context context,
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);
}
}
};
结果
名称:乐百色旋转+声音链接
deviceHardwareAddress:MAC…..此代码使用BeaconManager,它不断扫描新的蓝牙设备并返回一个信标列表对象,您可以使用该对象获取所需的任何信息 确保导入BeaconManager
private BeaconManager beaconManager;
//In onCreate method
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
//use these out of the onCreate method
public void onScanStart(View view) {
stopScanButton.setEnabled(true);
scanningButton.setEnabled(false);
beaconManager.bind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.removeAllRangeNotifiers();
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon b : beacons) {
System.out.println(String.format("%s: %f: %d", b.getBluetoothName(), b.getDistance(), b.getRssi()));
});
try {
//Tells the BeaconService to start looking for beacons that match the passed.
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
专用BeaconManager BeaconManager;
//在onCreate方法中
beaconManager=beaconManager.getInstanceForApplication(此应用程序);
beaconManager.GetBeaconParser().add(新建BeaconParser()。
立根布置(“m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24”);
//从onCreate方法中使用这些
公共void onScanStart(视图){
stopScanButton.setEnabled(真);
scanningButton.setEnabled(错误);
beaconManager.bind(这个);
}
@凌驾
beacerviceconnect()上的公共无效{
beaconManager.removeAllRangeNotifiers();
beaconManager.addRangeNotifier(新的RangeNotifier(){
@凌驾
公共无效范围信标区域(收集信标,区域){
用于(信标b:信标){
System.out.println(String.format(“%s:%f:%d”、b.getBluetoothName()、b.getDistance()、b.getRssi());
});
试一试{
//告诉BeaconService开始寻找与所传递信息匹配的信标。
beaconManager.startrangbeaconregion(新区域(“MyRangUniqueId”,null,null,null));
}捕获(远程异常){
Toast.makeText(例如toString(),Toast.LENGTH_LONG).show();
}
}
让我知道这对你是否有效!代码清晰,这应该是正确的答案。@SaharMillis:那它对你不起作用呢?它工作正常,但你的其他设备需要是可发现的,而不仅仅是在蓝牙打开的情况下。如果在范围内找到多个蓝牙设备,这是如何工作的?接收器是否触发multiple times?@Zapnologica
BroadcastReceiver
每个设备都会收到一次通知;因此必须将它们保存在ArrayList
字段中,或者类似的字段中……而在BluetoothDevice旁边还有两个可用的操作。找到的操作
,其中一个表示扫描已完成。@SaharMillis确保您已经完成了将权限放入位置和蓝牙的清单中