2017-09-26 2 views
0

私はアンドロイド用のブルートゥースコントローラをプログラミングしています。私はBluetoothをオンとオフに切り替えることができましたが、近くのデバイスをスキャンする必要があります。これらは、スキャンが終わるとすぐにポップアップメニューとして表示したいです。それから、あなたが望むデバイスをクリックしてそれに接続する必要があります。Xamarin Bluetooth。ポップアップメニューにスキャンを表示し、接続してください

ペアリングされていないデバイスにも接続する必要があることに注意してください。 、ポップアップメニューが画面の大部分をカバーするような「ビッグ」でなければなりません...(ボタンのみ-ポップアップが好きではありません)

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; 
     } 
    } 
} 

私はイベントハンドラでのBluetoothをオンにした後、それを待つ必要があります私はSystem.Threading.Thread.sleep()でこれをしました。

どのように私は発見されたBTデバイスを取ってポップアップメニューに表示することができますか? それに接続するにはどうしたらいいですか?

答えて

0

これは、Bluetoothデバイスの認識と管理に役立つと思います。

[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()); 
     } 
    } 




} 
関連する問題