android7.0 蓝牙定位,BluetoothAdapter在Android6.0/7.0+以上startDiscovery不能发现蓝牙设备问题...
BluetoothAdapter在Android6.0+以上startDiscovery不能發現藍牙設備問題
問題的重要原因之一是Android 6.0+,Android 7.0+的權限問題引起的。在Android 4.0+上運行良好的藍牙代碼,在高版本運行異常。比如BluetoothAdapter的startDiscovery雖然啟動了發現藍牙任務,但是不能發現藍牙設備。解決問題是針對最新高版本的Android系統增加權限申請。現在給出一個完整例子。
activity_main.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="zhangphil.bluetooth.MainActivity">
android:id="@+id/init"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="初始化藍牙設備" />
android:id="@+id/discovery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發現設備" />
android:id="@+id/enable_discovery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使自身可被其他藍牙設備發現" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView">
測試的MainActivity.java:
package zhangphil.bluetooth;
import android.Manifest;
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.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity implements View.OnClickListener {
private final int REQUEST_ENABLE_BT = 0xa01;
private final int PERMISSION_REQUEST_COARSE_LOCATION = 0xb01;
private String TAG = "zhangphil";
private ArrayAdapter mAdapter;
private BluetoothAdapter mBluetoothAdapter;
// 廣播接收發現藍牙設備
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.d(TAG, "開始掃描...");
}
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device != null) {
// 添加到ListView的Adapter。
mAdapter.add("設備名:" + device.getName() + "\n設備地址:" + device.getAddress());
mAdapter.notifyDataSetChanged();
}
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.d(TAG, "掃描結束.");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
}
// 注冊廣播接收器。
// 接收藍牙發現
IntentFilter filterFound = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filterFound);
IntentFilter filterStart = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(mReceiver, filterStart);
IntentFilter filterFinish = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filterFinish);
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1);
((ListView) findViewById(R.id.listView)).setAdapter(mAdapter);
findViewById(R.id.init).setOnClickListener(this);
findViewById(R.id.discovery).setOnClickListener(this);
findViewById(R.id.enable_discovery).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.init:
init();
case R.id.discovery:
discovery();
case R.id.enable_discovery:
enable_discovery();
}
}
// 初始化藍牙設備
private void init() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 檢查設備是否支持藍牙設備
if (mBluetoothAdapter == null) {
Log.d(TAG, "設備不支持藍牙");
// 不支持藍牙,退出。
return;
}
// 如果用戶的設備沒有開啟藍牙,則彈出開啟藍牙設備的對話框,讓用戶開啟藍牙
if (!mBluetoothAdapter.isEnabled()) {
Log.d(TAG, "請求用戶打開藍牙");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
// 接下去,在onActivityResult回調判斷
}
}
// 啟動藍牙發現...
private void discovery() {
if (mBluetoothAdapter == null) {
init();
}
mBluetoothAdapter.startDiscovery();
}
// 可選方法,非必需
// 此方法使自身的藍牙設備可以被其他藍牙設備掃描到,
// 注意時間閾值。0 - 3600 秒。
// 通常設置時間為120秒。
private void enable_discovery() {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
// 第二個參數可設置的范圍是0~3600秒,在此時間區間(窗口期)內可被發現
// 任何不在此區間的值都將被自動設置成120秒。
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
startActivity(discoverableIntent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == RESULT_OK) {
Log.d(TAG, "打開藍牙成功!");
}
if (resultCode == RESULT_CANCELED) {
Log.d(TAG, "放棄打開藍牙!");
}
} else {
Log.d(TAG, "藍牙異常!");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
break;
}
}
}
不要忘記增加權限:
代碼運行結果:
以上代碼運行測試環境:硬件設備是三星S7Edge,Android版本:7.0
總結
以上是生活随笔為你收集整理的android7.0 蓝牙定位,BluetoothAdapter在Android6.0/7.0+以上startDiscovery不能发现蓝牙设备问题...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL的乱码解决方案
- 下一篇: android novate乱码,Nov