2016-03-22 7 views
7

Bluetoothが6.01で動作していないようですが、Kitkat 4.4.4のupdate + appCompatのコードと権限で期待通りに動作していないようです。Android Marshmallow 6.0.1 Bluetoothスキャン結果が返されない

返される結果はありません。また、近くに複数の発見可能なデバイスがあります。

誰もがなぜそのような洞察力を持っていますか?私はあなたがMarshmallowいるので、あなたがあなたの仕事のためにこれらの権限が必要であることを知っているネクサス上5

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

package bluetoothscanneractivity; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 

public class BluetoothScannerActivity extends Activity { 
    //private final BroadcastReceiver FoundReceiver = null; 
    protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>(); 
    private ListView foundDevicesListView; 
    private ArrayAdapter<BluetoothDevice> btArrayAdapter; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetooth_scanner); 
     final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     final Button scanb = (Button) findViewById(R.id.button); 
     final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1); 

     btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this, 
       android.R.layout.simple_list_item_1, foundDevices); 
     foundDevicesListView.setAdapter(btArrayAdapter); 

     //Turn on Bluetooth 
     if (myBlueToothAdapter == null) 
      Toast.makeText(BluetoothScannerActivity.this, "Your device doesnot support Bluetooth", Toast.LENGTH_LONG).show(); 
     else if (!myBlueToothAdapter.isEnabled()) { 
      Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(BtIntent, 0); 
      Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show(); 
     } 
     //scan 
     scanb.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       btArrayAdapter.clear(); 
       myBlueToothAdapter.startDiscovery(); 
       Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
     IntentFilter filter = new IntentFilter(
       BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(FoundReceiver, filter); 

    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     unregisterReceiver(FoundReceiver); 
    } 

    private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      String action = intent.getAction(); 

      // When discovery finds a new device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       if (!foundDevices.contains(device)) { 
        foundDevices.add(device); 
        btArrayAdapter.notifyDataSetChanged(); 
       } 
      } 

      // When discovery cycle finished 
      if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       if (foundDevices == null || foundDevices.isEmpty()) { 
        Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show(); 
       } 
      } 

     } 
    }; 


} 
+0

startDiscovery()メソッドのコードを貼り付けてください。 –

+0

標準のAndroid BLコールである必要はありません。 – mcdoomington

+0

あなたはtoast-toast.makeText(BluetoothScannerActivity.this、 "Scanning Devices"、Toast.LENGTH_LONG).show()を見ることができますか? –

答えて

4

、 - それは、以下で動作:

package bluetoothscanneractivity; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.Manifest; 

import java.util.ArrayList; 

public class BluetoothScannerActivity extends AppCompatActivity { 
    //private final BroadcastReceiver FoundReceiver = null; 
    protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>(); 
    private ListView foundDevicesListView; 
    private ArrayAdapter<BluetoothDevice> btArrayAdapter; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetooth_scanner); 
     final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     final Button scanb = (Button) findViewById(R.id.button); 
     final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1); 

     btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this, 
       android.R.layout.simple_list_item_1, foundDevices); 

     foundDevicesListView.setAdapter(btArrayAdapter); 

     //Turn on Bluetooth 
     if (myBlueToothAdapter == null) 
      Toast.makeText(BluetoothScannerActivity.this, "Your device doesnt support Bluetooth", Toast.LENGTH_LONG).show(); 
     else if (!myBlueToothAdapter.isEnabled()) { 
      Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(BtIntent, 0); 
      Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show(); 
     } 

     // Quick permission check 
     int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION"); 
     permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION"); 
     if (permissionCheck != 0) { 

      this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 
     } 


     scanb.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       btArrayAdapter.clear(); 

       myBlueToothAdapter.startDiscovery(); 

       Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
     IntentFilter filter = new IntentFilter(
       BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(FoundReceiver, filter); 

    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     unregisterReceiver(FoundReceiver); 
    } 


    private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      String action = intent.getAction(); 

      // When discovery finds a new device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       if (!foundDevices.contains(device)) { 
        foundDevices.add(device); 
        Toast.makeText(BluetoothScannerActivity.this, "name: " + device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show(); 
        btArrayAdapter.notifyDataSetChanged(); 
       } 

      } 

      // When discovery cycle finished 
      if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       if (foundDevices == null || foundDevices.isEmpty()) { 
        Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show(); 
       } 
      } 

     } 
    }; 


} 
+0

私はアプリの開発者ではありませんが、私の携帯電話がAndroid 6.0.1にアップグレードされて以来、私の "blueterm"アプリは私の組み込み機器に接続しなくなっています。このようなアプリケーションに当てはまる問題ですか? –

+0

私はそれが疑わしいと思う - これは、迅速かつ汚れた実行だったので、許可の処理を変更することができます。 – mcdoomington

+0

ありがとう、私はそれが "blueterm"アプリのソースコードにアクセスする必要があると思います。 –

13

を実行しています -

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

またMarshmallowいるので、あなたはプログラムでも、あなたかかわらず、権限を要求しなければなりませんマニフェストファイルでそれらを宣言しました。

だからあなたはあなたstartDiscovery()

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 

、ユーザーがstartDiscovery()することができます、これらの権限を受け入れた後、前の場所のアクセス権を要求しなければなりません。そして彼/彼女が否定するならば、あなたはその装置を発見することができません。 ユーザーアクションは

onRequestPermissionsResult()コールバックで確認できます。 23 SDKバージョンをminに変更を加え権限を持つ

+0

私はプログラムで許可を要求する必要があるかどうかわかりませんでした。 – mcdoomington

+0

Ahh私はプログラムでアクセス許可を要求する必要があるのか​​分からなかったが、それでも動作しません。私はさらに、アプリケーションの情報許可ページを通じて許可を有効にしました。 – mcdoomington

+0

これは私の問題を解決したことを確認できます。私は主な活動でそれを行います。 – Dayan

2

SDKのバージョンがある場合は、以下の方法でのみ> LOLLIPOPを実行します。私はこの制限なしでそれを使用しようとしたとき、私のアプリはクラッシュした。 .startDiscovery()を使用する直前にこのメソッドを呼び出すだけです。

public void checkBTPermissions(){ 
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){ 
      int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION"); 
     permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION"); 
     if (permissionCheck != 0) { 

      this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 
     } 
    }else{ 
     Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP."); 
    } 
} 
関連する問題