C# 正在Xamarin.Android中查找蓝牙设备列表
在多个论坛和网站讲述了关于如何发现蓝牙设备(配对或未配对)的相同内容之后,我写下了这段代码C# 正在Xamarin.Android中查找蓝牙设备列表,c#,android,xamarin,bluetooth,C#,Android,Xamarin,Bluetooth,在多个论坛和网站讲述了关于如何发现蓝牙设备(配对或未配对)的相同内容之后,我写下了这段代码 class MainActivity: Activity { BluetoothAdapter btAdapter; static ArrayAdapter<string> newDevicesArrayAdapter; public static List<string> mDeviceList = new List<string>();
class MainActivity: Activity
{
BluetoothAdapter btAdapter;
static ArrayAdapter<string> newDevicesArrayAdapter;
public static List<string> mDeviceList = new List<string>();
DeviceDiscoveredReceiver receiver;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
receiver = new DeviceDiscoveredReceiver(this);
IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound);
RegisterReceiver(receiver, filter);
btAdapter = BluetoothAdapter.DefaultAdapter;
}
class DeviceDiscoveredReceiver : BroadcastReceiver
{
Activity mainActivity;
public DeviceDiscoveredReceiver(Activity activity)
{
this.mainActivity = activity;
}
public override void OnReceive(Context context, Intent intent)
{
String action = intent.Action;
if (BluetoothDevice.ActionFound.Equals(action))
{
BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
mDeviceList.Add(device.Name + ";" + device.Address);
var path = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "Download";
string filename = Path.Combine(path, "myfile.txt");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
using (StreamWriter objStreamWriter = new StreamWriter(filename, true))
{
objStreamWriter.WriteLine(mDeviceList.Last());
objStreamWriter.Close();
}
}
}
}
}
class main活动:活动
{
蓝牙适配器;
静态阵列适配器新设备阵列适配器;
public static List mDeviceList=new List();
设备发现接收机;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
接收器=新设备发现接收器(此);
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ActionFound);
寄存器接收器(接收器、过滤器);
btAdapter=BluetoothAdapter.DefaultAdapter;
}
类设备发现接收器:BroadcastReceiver
{
活动主活动;
公共设备发现接收器(活动)
{
this.main活动=活动;
}
公共覆盖void OnReceive(上下文、意图)
{
字符串action=intent.action;
if(BluetoothDevice.ActionFound.Equals(action))
{
BluetoothDevice=(BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
mDeviceList.Add(device.Name+“;”+device.Address);
var path=Android.OS.Environment.ExternalStorageDirectory+Java.IO.File.Separator+“下载”;
字符串文件名=Path.Combine(路径“myfile.txt”);
如果(!Directory.Exists(path))
{
CreateDirectory(路径);
}
使用(StreamWriter objStreamWriter=newstreamwriter(文件名,true))
{
objStreamWriter.WriteLine(mDeviceList.Last());
objStreamWriter.Close();
}
}
}
}
}
这里,Activity类有一个BroadcastReceiver类,在这个类中,任何发现的带有MAC地址的蓝牙设备的名称都会被写进一个.txt文件。我对Xamarin和android都很陌生,以下是我感到困惑的事情:
我在这里的主要目标是刚刚开始发现蓝牙设备,但是如何发现呢?好的,这很容易,但因为我是新手,所以花了一些时间才弄清楚。我错过了
OnCreate
方法中的SetContentView(Resources.Layout.Main)
,因此只需将OnCreate方法更改为:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resources.Layout.Main); //add this line
receiver = new DeviceDiscoveredReceiver(this);
IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound);
RegisterReceiver(receiver, filter);
btAdapter = BluetoothAdapter.DefaultAdapter;
}