我可以通过手机在“设置”中发现附近的蓝牙设备。另外,在AndroidManifest.xml文件中,我授予了'BLUETOOTH_ADMIN'和'BLUETOOTH'权限。 Logcat和Build中没有错误。我也有最新版本的Android Studio。我正在使用Android 8.0 Oreo API 26在Moto G6上测试该应用程序。
我的Java代码
package com.example.gaurangadas.bluetoothfinder;
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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ListView mylist;
TextView mytext;
Button mybutton;
BluetoothAdapter bluetoothAdapter;
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("ACTION",action);
}
};
public void searchDevices(View view) {
mytext.setText("Searching...");
mybutton.setEnabled(false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mylist = (ListView) findViewById(R.id.listView);
mytext = (TextView) findViewById(R.id.statusView);
mybutton = (Button) findViewById(R.id.searchButton);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(broadcastReceiver,intentFilter);
bluetoothAdapter.startDiscovery();
}
}
Logcat消息
07-09 15:52:42.176 7348-7348/? I/zygote: Late-enabling -Xcheck:jni
07-09 15:52:42.434 7348-7348/com.example.gaurangadas.bluetoothfinder I/InstantRun: starting instant run server: is main process
07-09 15:52:42.878 7348-7375/com.example.gaurangadas.bluetoothfinder D/OpenGLRenderer: HWUI GL Pipeline
07-09 15:52:42.888 7348-7348/com.example.gaurangadas.bluetoothfinder I/ACTION: android.bluetooth.adapter.action.DISCOVERY_STARTED
07-09 15:52:42.943 7348-7375/com.example.gaurangadas.bluetoothfinder I/Adreno: QUALCOMM build : 368094a, I26dffed9a4
Build Date : 12/03/17
OpenGL ES Shader Compiler Version: EV031.22.00.01
Local Branch :
Remote Branch : quic/gfx-adreno.lnx.1.9.r3-rel
Remote Branch : NONE
Reconstruct Branch : NOTHING
07-09 15:52:42.952 7348-7375/com.example.gaurangadas.bluetoothfinder I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
07-09 15:52:42.957 7348-7375/com.example.gaurangadas.bluetoothfinder I/OpenGLRenderer: Initialized EGL, version 1.4
07-09 15:52:42.957 7348-7375/com.example.gaurangadas.bluetoothfinder D/OpenGLRenderer: Swap behavior 2
07-09 15:52:56.944 7348-7348/com.example.gaurangadas.bluetoothfinder I/ACTION: android.bluetooth.adapter.action.DISCOVERY_FINISHED
07-09 15:59:49.663 7348-7348/com.example.gaurangadas.bluetoothfinder I/ACTION: android.bluetooth.adapter.action.DISCOVERY_STARTED
07-09 15:59:54.727 7348-7348/com.example.gaurangadas.bluetoothfinder I/ACTION: android.bluetooth.adapter.action.DISCOVERY_FINISHED