Xamarin蓝牙。在弹出菜单中显示扫描并连接
问题描述:
我正在为android编程蓝牙控制器。我已经能够打开和关闭蓝牙,但现在我需要扫描附近的设备。这些我想在扫描完成后尽快显示为弹出式菜单。然后你应该点击你想要的设备并连接它。Xamarin蓝牙。在弹出菜单中显示扫描并连接
请注意,它也应该连接到不配对的设备。 并在弹出菜单中应该是“大”之类涵盖了大部分屏幕...(不喜欢按钮,弹出只)
namespace BluetoothController
{
[Activity(Label = "BluetoothController", MainLauncher = true)]
public class MainActivity : Activity, View.IOnTouchListener
{
BluetoothAdapter BTAdapter;
Button button_OnOff, button_Forward, button_Fire, button_Backward, button_Left, button_Right;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Initialize Components
Initialize();
// Get local Bluetooth Adapter
BTAdapter = BluetoothAdapter.DefaultAdapter;
}
public void Initialize()
{
button_OnOff = (Button)FindViewById(Resource.Id.button_onoff);
button_Forward = (Button)FindViewById(Resource.Id.button_forward);
button_Fire = (Button)FindViewById(Resource.Id.button_fire);
button_Backward = (Button)FindViewById(Resource.Id.button_backward);
button_Left = (Button)FindViewById(Resource.Id.button_left);
button_Right = (Button)FindViewById(Resource.Id.button_right);
button_OnOff.SetOnTouchListener(this);
button_Forward.SetOnTouchListener(this);
button_Backward.SetOnTouchListener(this);
button_Left.SetOnTouchListener(this);
button_Right.SetOnTouchListener(this);
button_Fire.SetOnTouchListener(this);
}
// Event handler for Buttons
public bool OnTouch(View v, MotionEvent e)
{
// Handle if Buttons is pressed or released
switch (e.Action)
{
case MotionEventActions.Down:
// Do if pressed
switch (v.Id)
{
case Resource.Id.button_forward:
break;
case Resource.Id.button_backward:
break;
case Resource.Id.button_left:
break;
case Resource.Id.button_right:
break;
case Resource.Id.button_fire:
break;
case Resource.Id.button_onoff:
break;
}
break;
case MotionEventActions.Up:
// Do if released
switch (v.Id)
{
case Resource.Id.button_forward:
break;
case Resource.Id.button_backward:
break;
case Resource.Id.button_left:
break;
case Resource.Id.button_right:
break;
case Resource.Id.button_fire:
break;
case Resource.Id.button_onoff:
if (BTAdapter != null)
{
if (!BTAdapter.IsEnabled)
{
BTAdapter.Enable();
Toast.MakeText(this, "Bluetooth enabled", ToastLength.Short).Show();
System.Threading.Thread.Sleep(5000);
// Display Devices and connect to the clicked one
}
else
{
BTAdapter.Disable();
Toast.MakeText(this, "Bluetooth disabled", ToastLength.Short).Show();
}
}
else
{
Toast.MakeText(this, "Bluetooth not supported", ToastLength.Long).Show();
}
break;
}
break;
}
return true;
}
}
}
我在事件处理程序开启蓝牙后,它应该等待并显示可选设备...这我用System.Threading.Thread.sleep()
我怎么能发现BT设备,并在弹出菜单中显示它们? 我如何选择其中一个连接到它?
答
我认为这可以帮助您识别蓝牙设备并对其进行管理。
[BroadcastReceiver]
public class AndroidBluetooth : BroadcastReceiver
{
private static BluetoothDevice _bluetoothDevice;
private readonly BluetoothAdapter _bluetoothAdapter;
public BluetoothSocket _bluetoothSocket;
public readonly IntentFilter _intentFilter;
public AndroidBluetooth()
{
_bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
_intentFilter = new IntentFilter();
_intentFilter.AddAction(BluetoothDevice.ActionFound);
_intentFilter.AddAction(BluetoothDevice.ActionNameChanged);
_intentFilter.AddAction(BluetoothDevice.ActionBondStateChanged);
_intentFilter.AddAction(BluetoothDevice.ActionPairingRequest);
_intentFilter.AddAction(BluetoothDevice.ActionAclConnected);
_intentFilter.AddAction(BluetoothDevice.ActionAclDisconnected);
_intentFilter.AddAction(BluetoothDevice.ActionAclDisconnectRequested);
_intentFilter.AddAction(BluetoothAdapter.ActionDiscoveryStarted);
_intentFilter.AddAction(BluetoothAdapter.ActionDiscoveryFinished);
_intentFilter.AddAction(BluetoothAdapter.ActionRequestEnable);
Xamarin.Forms.Forms.Context.RegisterReceiver(this, _intentFilter);
}
public override void OnReceive(Context context, Intent intent)
{
try
{
BluetoothDevice item = null;
string action = intent.Action;
switch (action)
{
case BluetoothDevice.ActionPairingRequest:
try
{
//You can set device pin by default to avoid asking.
int pin = intent.GetIntExtra(BluetoothDevice.ExtraPairingKey, 1234);
_bluetoothDevice.SetPin(ASCIIEncoding.UTF8.GetBytes(pin.ToString()));
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
break;
case BluetoothDevice.ActionBondStateChanged:
break;
case BluetoothDevice.ActionFound:
//Bluetooth device found
/*/
/Here you can make a list for preparing popup
/*/
break;
case BluetoothDevice.ActionNameChanged:
break;
case BluetoothAdapter.ActionDiscoveryFinished:
//Occurs when scan is finished
/*/
/Launch desired popup
/*/
break;
case BluetoothDevice.ActionAclConnected:
//Bluettoth device is connected
break;
case BluetoothDevice.ActionAclDisconnected:
//Device is disconnected
break;
case BluetoothDevice.ActionAclDisconnectRequested:
break;
}
}
catch (Exception ex)
{
Logger.WriteException(ex);
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
}