2016-04-13 4 views
0

利用可能なBluetoothデバイス(ボンディングされたデバイスのみ)をすべて検索したい。 だから私はこのコードを書いた:ブルートゥースデバイスを検出できないXamarin Android

using System; 
using Android.App; 
using Android.Widget; 
using Snackbar = Android.Support.Design.Widget.Snackbar; 
using v7 = Android.Support.V7.App; 
using Android.OS; 
using Android.Views; 
using Android.Bluetooth; 
using Android.Content; 

namespace Morpion 
{ 
    [Activity(Label = "HomeActivity")] 
    public class HomeActivity : Activity 
    { 
     BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter; 
     static TextView connectionStateText; 
     bool connected = false; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      SetContentView(Resource.Layout.Home); 
      Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds); 

      var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar); 
      SetActionBar(toolbar); 
     } 

     public override bool OnCreateOptionsMenu(IMenu menu) 
     { 
      MenuInflater.Inflate(Resource.Menu.OptionMenu, menu); 
      return true; 
     } 

     public override bool OnOptionsItemSelected(IMenuItem item) 
     { 
      RelativeLayout layout = FindViewById<RelativeLayout>(Resource.Id.homeLayout); 
      connectionStateText = FindViewById<TextView>(Resource.Id.connectionStateText); 
      ProgressBar connectionBar = FindViewById<ProgressBar>(Resource.Id.connectionBar); 

      if (item.ItemId == Resource.Id.about) 
      { 
       var about = new v7.AlertDialog.Builder(this); 
       about.SetTitle("About"); 
       about.SetMessage(Resource.String.about); 
       about.SetNeutralButton("Close", delegate { }); 
       about.Show(); 
      } 
      else if (item.ItemId == Resource.Id.connect) 
      { 
       if (!connected && !bluetoothAdapter.IsEnabled) 
       { 
        Snackbar.Make(layout, "Please activate bluetooth", Snackbar.LengthLong) 
          .SetAction("Activate", delegate 
          { 
           bluetoothAdapter.Enable(); 
          }) 
          .Show(); 
       } 
       else if (!connected) 
       { 
        connectionStateText.Text = "Connection"; 
        connectionBar.Indeterminate = true; 
        connectionBar.Visibility = ViewStates.Visible; 
        System.Collections.Generic.ICollection<BluetoothDevice> devices = bluetoothAdapter.BondedDevices; 

        var filter = new IntentFilter(BluetoothDevice.ActionFound); 
        var receiver = new Receiver(); 
        if (bluetoothAdapter.IsDiscovering) 
        { 
         bluetoothAdapter.CancelDiscovery(); 
        } 
        RegisterReceiver(receiver, filter); 
        bluetoothAdapter.StartDiscovery(); 
       } 
       else 
       { 
        connectionStateText.Text = "Not connected"; 
        item.SetTitle("Connect"); 
        connected = false; 
       } 
      } 
      return base.OnOptionsItemSelected(item); 
     } 

     public class Receiver : BroadcastReceiver 
     { 
      public override void OnReceive(Context context, Intent intent) 
      { 
       string action = intent.Action; 

       if (action == BluetoothDevice.ActionFound) 
       { 
        Console.WriteLine("found"); 
       } 
      } 
     } 
    } 
} 

私の問題は、OnReceiveメソッドが起動しないことであり、私は本当にそれが動作しない理由を知りません。

私は、Android API 23を使用していると私はあなたがあなたの受信機にBroadcastReceiver属性を追加する必要がありますアンドロイド6.0.1

答えて

3

上で実行されているHuawei社ネクサス6Pの上に自分のコードをテストしています。 Android 6では、実行時にACCESS_FINE_LOCATIONとACCESS_COARSE_LOCATION権限を要求する必要があります。Request permission

+0

さらに、参照/使用するAPIに応じて、対応する権限を有効にする必要があります。それは最初の使用について尋ねるべきではありませんが、それは起こらなかったし、私はそれを理解しようといくつかの時間を費やしました... – nurchi

+0

私は[BroadcastReceiver(Enabled = true、Label = "receiver")]をレシーバの前に追加しましたしかし、それは何も変わらない。 権限については、私はbluetoothとbluetooth_adminを持っています – Creends

+0

@クレーム:必要な権限がありますか? http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id – Giorgi

関連する問題