【发布时间】:2018-07-03 16:06:30
【问题描述】:
按照指南here,我创建了一个应用程序,用于查找附近的蓝牙设备并将它们的名称(如果可用)、地址和 rssi 记录到一个简单的列表视图中。
我的目标是过滤结果以查找所有可发现的附近手机及其 RSSI。但是,我没有在列表中看到我的 iPhone 与附近的其他设备。我通过设置->常规->关于我的手机的MAC地址知道我的手机有bluetooth enabled and is discoverable。
我的手机距离设备大约一英尺远,因此设备应该能够接听它(它可以从房间的另一头发现我的笔记本电脑)。如何确保找到我的手机和其他手机?
注意:我正在为 Android 版本 6.0.1 使用 c# 和 Xamarin.Android 开发。
更新: 这是我用来进行发现的代码-
static int REQUEST_ENABLE_BT = 1;
BluetoothBroadcastReceiver btReceiver;
BluetoothAdapter btAdapter = BluetoothAdapter.DefaultAdapter
//check status of bluetooth and enable if possible
...
//Discover bluetooth devices
btReceiver = new BluetoothBroadcastReceiver(this);
IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound);
RegisterReceiver(btReceiver, filter);
btAdapter.StartDiscovery();
//custom BroadcastReceiver to take ActionFound
public class BluetoothBroadcastReceiver : BroadcastReceiver
{
Activity activity;
public BluetoothBroadcastReceiver(Activity activity)
{
this.activity = activity;
}
public override void OnReceive(Context context, Intent intent)
{
string action = intent.Action;
if (BluetoothDevice.ActionFound.Equals(action))
{
//Discovery has found a device. Get info
BluetoothDevice device = (Android.Bluetooth.BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
string deviceName = device.Name;
string deviceAddress = device.Address;
short RSSI = intent.GetShortExtra(BluetoothDevice.ExtraRssi,(short)0);
string result = deviceName + " " + deviceAddress + " " + RSSI;
newDevicesArrayAdapter.Add(result);
}
}
}
谢谢!
【问题讨论】:
-
请发布您的代码以进行发现尝试
-
@Amritkumar 请见上文!请注意,这是在一个活动中,并且 OnReceive 输出添加到列表视图中的设备,但我没有包含该部分代码。
-
这个article 说手机只能在其蓝牙设置页面中被发现。
-
我用我的手机试了一下,“当蓝牙设置打开时,附近的设备可以看到 Nexus 5X。”
标签: c# bluetooth xamarin.android android-bluetooth